<?php

namespace Database\Seeders;

use App\Models\Company;
use App\Models\Office;
use App\Models\User;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Run Role and Permission Seeder first
        $this->call(RoleAndPermissionSeeder::class);

        // Run Holiday Seeder (creates national holidays and weekends for 2026)
        $this->call(HolidaySeeder::class);

        // Run Shift Seeder (creates shifts with 08:00 - 16:00, skip for Contoh perusahaan)
        $this->call(ShiftSeeder::class);

        // Run Quota Seeder (mengisi kuota untuk koreksi dan pengajuan)
        $this->call(QuotaSeeder::class);

        // Buat Company
        $company = Company::create([
            'name' => 'PT Contoh Perusahaan',
            'code' => 'PCP001',
            'address' => 'Jl. Sudirman No. 1, Jakarta Pusat',
            'phone' => '021-1234567',
            'email' => 'info@contohperusahaan.com',
            'latitude' => -6.2088, // Jakarta
            'longitude' => 106.8456,
            'radius' => 100,
            'work_start_time' => '08:00:00',
            'work_end_time' => '17:00:00',
            'is_active' => true,
        ]);

        // Buat Office Headquarters
        Office::create([
            'company_id' => $company->id,
            'name' => 'Kantor Pusat Jakarta',
            'code' => 'HQ-JKT',
            'address' => 'Jl. Sudirman No. 1, Jakarta Pusat',
            'city' => 'Jakarta Pusat',
            'province' => 'DKI Jakarta',
            'latitude' => -6.2088,
            'longitude' => 106.8456,
            'radius' => 100,
            'type' => 'headquarters',
            'is_active' => true,
        ]);

        // Buat beberapa Office BPKP (per provinsi) untuk rekomendasi perjalanan dinas
        $bpkpOffices = [
            [
                'name' => 'BPKP Perwakilan Jawa Barat',
                'code' => 'BPKP-JABAR',
                'address' => 'Jl. Asia Afrika No. 81, Bandung',
                'city' => 'Bandung',
                'province' => 'Jawa Barat',
                'latitude' => -6.9211,
                'longitude' => 107.6069,
            ],
            [
                'name' => 'BPKP Perwakilan Jawa Tengah',
                'code' => 'BPKP-JATENG',
                'address' => 'Jl. Pemuda No. 135, Semarang',
                'city' => 'Semarang',
                'province' => 'Jawa Tengah',
                'latitude' => -6.9666,
                'longitude' => 110.4194,
            ],
            [
                'name' => 'BPKP Perwakilan Jawa Timur',
                'code' => 'BPKP-JATIM',
                'address' => 'Jl. Raya Juanda, Sidoarjo',
                'city' => 'Surabaya',
                'province' => 'Jawa Timur',
                'latitude' => -7.2575,
                'longitude' => 112.7521,
            ],
            [
                'name' => 'BPKP Perwakilan DKI Jakarta',
                'code' => 'BPKP-DKI',
                'address' => 'Jl. Pramuka No. 33, Jakarta Timur',
                'city' => 'Jakarta Timur',
                'province' => 'DKI Jakarta',
                'latitude' => -6.1934,
                'longitude' => 106.8713,
            ],
        ];

        foreach ($bpkpOffices as $office) {
            Office::create([
                'company_id' => null, // BPKP bersifat umum, tidak terikat company
                'name' => $office['name'],
                'code' => $office['code'],
                'address' => $office['address'],
                'city' => $office['city'],
                'province' => $office['province'],
                'latitude' => $office['latitude'],
                'longitude' => $office['longitude'],
                'radius' => 100,
                'type' => 'bpkp',
                'is_active' => true,
            ]);
        }

        // Buat beberapa Office Pemda (per kota/kabupaten) untuk rekomendasi perjalanan dinas
        $pemdaOffices = [
            [
                'name' => 'Pemda Kota Bandung',
                'code' => 'PEMDA-BDG',
                'address' => 'Jl. Wastukancana No. 2, Bandung',
                'city' => 'Bandung',
                'province' => 'Jawa Barat',
                'latitude' => -6.9147,
                'longitude' => 107.6098,
            ],
            [
                'name' => 'Pemda Kabupaten Bandung',
                'code' => 'PEMDA-KABBDG',
                'address' => 'Jl. Raya Soreang No. 141, Soreang',
                'city' => 'Kabupaten Bandung',
                'province' => 'Jawa Barat',
                'latitude' => -7.0295,
                'longitude' => 107.5228,
            ],
            [
                'name' => 'Pemda Kota Semarang',
                'code' => 'PEMDA-SMG',
                'address' => 'Jl. Pemuda No. 148, Semarang',
                'city' => 'Semarang',
                'province' => 'Jawa Tengah',
                'latitude' => -6.9667,
                'longitude' => 110.4196,
            ],
            [
                'name' => 'Pemda Kota Surabaya',
                'code' => 'PEMDA-SBY',
                'address' => 'Jl. Taman Surya No. 1, Surabaya',
                'city' => 'Surabaya',
                'province' => 'Jawa Timur',
                'latitude' => -7.2458,
                'longitude' => 112.7378,
            ],
            [
                'name' => 'Pemda DKI Jakarta',
                'code' => 'PEMDA-DKI',
                'address' => 'Jl. Medan Merdeka Selatan No. 8-9, Jakarta Pusat',
                'city' => 'Jakarta Pusat',
                'province' => 'DKI Jakarta',
                'latitude' => -6.1751,
                'longitude' => 106.8272,
            ],
        ];

        foreach ($pemdaOffices as $office) {
            Office::create([
                'company_id' => null, // Pemda bersifat umum, tidak terikat company
                'name' => $office['name'],
                'code' => $office['code'],
                'address' => $office['address'],
                'city' => $office['city'],
                'province' => $office['province'],
                'latitude' => $office['latitude'],
                'longitude' => $office['longitude'],
                'radius' => 100,
                'type' => 'pemda',
                'is_active' => true,
            ]);
        }

        // Buat Super Admin User
        $superAdmin = User::create([
            'company_id' => $company->id,
            'employee_id' => 'SA001',
            'nip' => '199001012020011001',
            'name' => 'Super Administrator',
            'email' => 'superadmin@contohperusahaan.com',
            'password' => 'password',
            'phone' => '08123456788',
            'role' => 'admin',
            'department' => 'IT',
            'position' => 'Super Administrator',
            'address' => 'Jakarta',
            'home_latitude' => -6.2000,
            'home_longitude' => 106.8500,
            'is_active' => true,
            'approval_status' => 'approved',
        ]);
        $superAdmin->assignRole('super_admin');

        // Buat Admin User
        $admin = User::create([
            'company_id' => $company->id,
            'employee_id' => 'ADM001',
            'nip' => '199001012020011002',
            'name' => 'Administrator',
            'email' => 'admin@contohperusahaan.com',
            'password' => 'password',
            'phone' => '08123456789',
            'role' => 'admin',
            'department' => 'IT',
            'position' => 'System Administrator',
            'address' => 'Jakarta',
            'home_latitude' => -6.2000,
            'home_longitude' => 106.8500,
            'is_active' => true,
            'approval_status' => 'approved',
        ]);
        $admin->assignRole('admin');

        // Buat Employee dengan permission approve (Approver)
        $approver = User::create([
            'company_id' => $company->id,
            'employee_id' => 'MGR001',
            'nip' => '199001012020011003',
            'name' => 'Manager HRD',
            'email' => 'manager@contohperusahaan.com',
            'password' => 'password',
            'phone' => '08123456790',
            'role' => 'employee',
            'department' => 'HRD',
            'position' => 'HR Manager',
            'address' => 'Jakarta',
            'home_latitude' => -6.2100,
            'home_longitude' => 106.8400,
            'is_active' => true,
            'approval_status' => 'approved',
        ]);
        $approver->assignRole('employee');
        // Give approver additional permissions
        $approver->givePermissionTo([
            'approve_users',
            'approve_documents',
            'approve_business_trips',
            'receive_hardcopy',
            'view_all_attendances',
            'view_all_business_trips',
            'view_all_documents',
        ]);

        // Buat Employee User biasa
        $employee = User::create([
            'company_id' => $company->id,
            'employee_id' => 'EMP001',
            'nip' => '199001012020011004',
            'name' => 'Karyawan Demo',
            'email' => 'karyawan@contohperusahaan.com',
            'password' => 'password',
            'phone' => '08123456791',
            'role' => 'employee',
            'department' => 'Operasional',
            'position' => 'Staff',
            'address' => 'Jakarta Selatan',
            'home_latitude' => -6.2600,
            'home_longitude' => 106.8100,
            'is_active' => true,
            'approval_status' => 'approved',
        ]);
        $employee->assignRole('employee');
    }
}
