<?php

namespace App\Console\Commands;

use App\Cawangan;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class InsertSempornaData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'palsu:insert-data {kodcaw=011237 : The kodcaw value to insert data for}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Insert sample data into palsu table for specific kodcaw';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $kodcaw = $this->argument('kodcaw');
        
        $this->info("Inserting data for kodcaw: {$kodcaw}");
        
        // Validate if kodcaw exists in Cawangan table
        $cawangan = DB::connection('mysql7')->table('arrahn_master_db')->where('kodcaw', $kodcaw)->first();
        
        if (!$cawangan) {
            $this->error("Error: kodcaw '{$kodcaw}' not found in Cawangan table!");
            return Command::FAILURE;
        }
        
        // Convert stdClass to array if needed
        $cawangan = (array) $cawangan;
        
        if (!isset($cawangan['mysql']) || empty($cawangan['mysql'])) {
            $this->error("Error: Database connection not found for kodcaw '{$kodcaw}'!");
            return Command::FAILURE;
        }
        
        $this->info("Using database connection: {$cawangan['mysql']}");
        
        $data = [
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240808-0003',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 1748.20,
                'jbg' => 4,
                'untung' => 78.00,
                'untungonepercent' => 52.00,
                'untungcajlain' => 26.00,
                'pembiayaan' => 1300.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0015',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 8499.80,
                'jbg' => 4.5,
                'untung' => 519.30,
                'untungonepercent' => 305.55,
                'untungcajlain' => 213.75,
                'pembiayaan' => 6790.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240806-0011',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 8740.98,
                'jbg' => 4,
                'untung' => 475.40,
                'untungonepercent' => 279.60,
                'untungcajlain' => 195.80,
                'pembiayaan' => 6990.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240828-0001',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 8121.20,
                'jbg' => 3,
                'untung' => 316.65,
                'untungonepercent' => 182.70,
                'untungcajlain' => 133.95,
                'pembiayaan' => 6090.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240904-0007',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 16135.20,
                'jbg' => 3,
                'untung' => 617.10,
                'untungonepercent' => 363.00,
                'untungcajlain' => 254.10,
                'pembiayaan' => 12100.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240904-0008',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 12101.40,
                'jbg' => 3,
                'untung' => 462.60,
                'untungonepercent' => 272.10,
                'untungcajlain' => 190.50,
                'pembiayaan' => 9070.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240904-0009',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 6050.70,
                'jbg' => 3,
                'untung' => 231.00,
                'untungonepercent' => 135.90,
                'untungcajlain' => 95.10,
                'pembiayaan' => 4530.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0023',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 19001.24,
                'jbg' => 4.5,
                'untung' => 149.18,
                'untungonepercent' => 87.75,
                'untungcajlain' => 61.43,
                'pembiayaan' => 1950.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0024',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 8499.80,
                'jbg' => 4.5,
                'untung' => 65.03,
                'untungonepercent' => 38.25,
                'untungcajlain' => 26.78,
                'pembiayaan' => 850.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0025',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 15972.57,
                'jbg' => 4.5,
                'untung' => 122.40,
                'untungonepercent' => 72.00,
                'untungcajlain' => 50.40,
                'pembiayaan' => 1600.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240618-0026',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 8552.90,
                'jbg' => 5.5,
                'untung' => 187.00,
                'untungonepercent' => 110.00,
                'untungcajlain' => 77.00,
                'pembiayaan' => 2000.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0027',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 42923.11,
                'jbg' => 4.5,
                'untung' => 328.95,
                'untungonepercent' => 193.50,
                'untungcajlain' => 135.45,
                'pembiayaan' => 4300.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0028',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 41872.04,
                'jbg' => 4.5,
                'untung' => 321.30,
                'untungonepercent' => 189.00,
                'untungcajlain' => 132.30,
                'pembiayaan' => 4200.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240618-0020',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 15495.86,
                'jbg' => 5.5,
                'untung' => 280.50,
                'untungonepercent' => 165.00,
                'untungcajlain' => 115.50,
                'pembiayaan' => 3000.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240719-0030',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 17483.01,
                'jbg' => 4.5,
                'untung' => 133.88,
                'untungonepercent' => 78.75,
                'untungcajlain' => 55.13,
                'pembiayaan' => 1750.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240618-0029',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 10062.25,
                'jbg' => 5.5,
                'untung' => 280.50,
                'untungonepercent' => 165.00,
                'untungcajlain' => 115.50,
                'pembiayaan' => 3000.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240731-0026',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 10283.50,
                'jbg' => 4.5,
                'untung' => 558.45,
                'untungonepercent' => 328.50,
                'untungcajlain' => 229.95,
                'pembiayaan' => 7300.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240715-0027',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 29624.77,
                'jbg' => 4.5,
                'untung' => 1147.50,
                'untungonepercent' => 675.00,
                'untungcajlain' => 472.50,
                'pembiayaan' => 15000.00,
            ],
            [
                'kodcaw' => $kodcaw,
                'tarikh' => '2024-11-28',
                'norujukan' => '011237240912-0009',
                'teller' => 'DM',
                'nota' => 'Barang Kes Semporna 28/11/24',
                'nilaimarhun' => 12654.35,
                'jbg' => 3,
                'untung' => 428.40,
                'untungonepercent' => 252.00,
                'untungcajlain' => 176.40,
                'pembiayaan' => 8400.00,
            ],
        ];

        $insertedCount = 0;
        $skippedCount = 0;

        // Start database transaction
        DB::connection($cawangan['mysql'])->beginTransaction();
        
        try {
            foreach ($data as $record) {
                // Check if record already exists
                $exists = DB::connection($cawangan['mysql'])->table('palsu')
                    ->where('kodcaw', $record['kodcaw'])
                    ->where('norujukan', $record['norujukan'])
                    ->exists();

                if (!$exists) {
                    DB::connection($cawangan['mysql'])->table('palsu')->insert($record);
                    $insertedCount++;
                    $this->line("✓ Inserted: {$record['norujukan']}");
                } else {
                    $skippedCount++;
                    $this->line("- Skipped: {$record['norujukan']} (already exists)");
                }
            }
            
            // Commit the transaction if everything is successful
            DB::connection($cawangan['mysql'])->commit();
            $this->info("✓ Transaction committed successfully!");
            
        } catch (\Exception $e) {
            // Rollback the transaction if any error occurs
            DB::connection($cawangan['mysql'])->rollback();
            $this->error("✗ Transaction failed and rolled back: " . $e->getMessage());
            return Command::FAILURE;
        }

        $this->info("\nSummary:");
        $this->info("Inserted: {$insertedCount} records");
        $this->info("Skipped: {$skippedCount} records");
        
        return Command::SUCCESS;
    }
}