<?php

namespace Database\Seeders;

use App\Models\SuratTugas;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SuratTugasSeeder extends Seeder
{
    /**
     * Run the database seeds.
     * Import ST data from JSON file generated by extract_st.py
     */
    public function run(): void
    {
        $jsonPath = storage_path('st_data.json');

        if (!file_exists($jsonPath)) {
            $this->command->error("File not found: {$jsonPath}");
            $this->command->info("Please run 'python3 extract_st.py' first to generate the data.");
            return;
        }

        $data = json_decode(file_get_contents($jsonPath), true);

        if (empty($data)) {
            $this->command->error("No data found in {$jsonPath}");
            return;
        }

        $this->command->info("Importing " . count($data) . " ST records...");

        // Get company_id (assuming single company or use first company)
        $companyId = DB::table('companies')->first()?->id ?? 1;

        $imported = 0;
        $updated = 0;
        $skipped = 0;

        foreach ($data as $row) {
            // Check if ST already exists (same no_st, tanggal_mulai, tanggal_selesai)
            $existingST = SuratTugas::where('company_id', $companyId)
                ->where('no_st', $row['no_st'])
                ->where('tanggal_mulai', $row['tanggal_mulai'])
                ->where('tanggal_selesai', $row['tanggal_selesai'])
                ->first();

            if ($existingST) {
                // Update if coordinates are missing but now available
                if (empty($existingST->custom_latitude) && !empty($row['custom_latitude'])) {
                    $existingST->update([
                        'custom_location_name' => $row['custom_location_name'],
                        'custom_latitude' => $row['custom_latitude'],
                        'custom_longitude' => $row['custom_longitude'],
                        'custom_address' => $row['custom_address'],
                    ]);
                    $updated++;
                } else {
                    $skipped++;
                }
                continue;
            }

            SuratTugas::create([
                'company_id' => $companyId,
                'no_st' => $row['no_st'],
                'tanggal_mulai' => $row['tanggal_mulai'],
                'tanggal_selesai' => $row['tanggal_selesai'],
                'location_id' => null, // Not using location_id as requested
                'custom_location_name' => $row['custom_location_name'],
                'custom_latitude' => $row['custom_latitude'],
                'custom_longitude' => $row['custom_longitude'],
                'custom_address' => $row['custom_address'],
                'keterangan' => $row['keterangan'],
                'is_active' => true,
                'created_by' => null,
            ]);

            $imported++;
        }

        $this->command->info("Import completed!");
        $this->command->info("  - Imported: {$imported}");
        $this->command->info("  - Updated (coordinates): {$updated}");
        $this->command->info("  - Skipped (duplicates): {$skipped}");
    }
}
