<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class UpdateSempornaRecordsStatusToZero extends Migration
{
    /**
     * Semporna norujukan records to update status to 0
     */
    private function getSempornaRecords(): array
    {
        return [
            '011237240808-0003',
            '011237240719-0015',
            '011237240806-0011',
            '011237240828-0001',
            '011237240904-0007',
            '011237240904-0008',
            '011237240904-0009',
            '011237240719-0023',
            '011237240719-0024',
            '011237240719-0025',
            '011237240618-0026',
            '011237240719-0027',
            '011237240719-0028',
            '011237240618-0020',
            '011237240719-0030',
            '011237240618-0029',
            '011237240731-0026',
            '011237240912-0009'
        ];
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $semporna_records = $this->getSempornaRecords();
        
        // Update status to 0 for Semporna records in mysql34 (Semporna branch)
        try {
            $updated = DB::connection('mysql34')->table('palsu')
                ->whereIn('norujukan', $semporna_records)
                ->update(['status' => 0]);
                
            echo "Updated {$updated} Semporna records with status = 0\n";
        } catch (\Exception $e) {
            echo "Error updating Semporna records status: " . $e->getMessage() . "\n";
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $semporna_records = $this->getSempornaRecords();
        
        // Revert status to 1 for Semporna records
        try {
            $reverted = DB::connection('mysql34')->table('palsu')
                ->whereIn('norujukan', $semporna_records)
                ->update(['status' => 1]);
                
            echo "Reverted {$reverted} Semporna records status to 1\n";
        } catch (\Exception $e) {
            echo "Error reverting Semporna records status: " . $e->getMessage() . "\n";
        }
    }
}
