<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Users
        $users = [
            ['name'=>'Administrator','email'=>'admin@jpn.co.id','password'=>Hash::make('Admin@123'),'role'=>'admin','division'=>'JPN','phone'=>'08100000001','is_active'=>1],
            ['name'=>'CS JPN','email'=>'cs@jpn.co.id','password'=>Hash::make('Cs@12345'),'role'=>'cs','division'=>'JPN','phone'=>'08100000002','is_active'=>1],
            ['name'=>'Teknisi 1','email'=>'teknisi1@jpn.co.id','password'=>Hash::make('Teknisi@1'),'role'=>'technician','division'=>'JPN','phone'=>'08100000003','is_active'=>1],
        ];
        foreach ($users as $user) {
            if (!DB::table('users')->where('email',$user['email'])->exists()) {
                DB::table('users')->insert(array_merge($user,['created_at'=>now(),'updated_at'=>now()]));
            }
        }

        // Company Settings
        $settings = [
            ['division'=>'JPN','company_name'=>'PT Jaringan Pintar Nusantara','address'=>'Jakarta','phone'=>'021-0000000','email'=>'info@jpn.co.id','bank_name'=>'BCA','bank_account'=>'1234567890','bank_account_name'=>'PT Jaringan Pintar Nusantara','invoice_footer'=>'Terima kasih atas kepercayaan Anda.'],
            ['division'=>'FixLab','company_name'=>'FixLab by JPN','address'=>'Jakarta','phone'=>'021-0000001','email'=>'fixlab@jpn.co.id','bank_name'=>'BCA','bank_account'=>'0987654321','bank_account_name'=>'FixLab JPN','invoice_footer'=>'Garansi perbaikan 30 hari.'],
            ['division'=>'Havva','company_name'=>'Havva CCTV by JPN','address'=>'Jakarta','phone'=>'021-0000002','email'=>'havva@jpn.co.id','bank_name'=>'Mandiri','bank_account'=>'1122334455','bank_account_name'=>'Havva JPN','invoice_footer'=>'Solusi keamanan terpercaya.'],
        ];
        foreach ($settings as $s) {
            if (!DB::table('company_settings')->where('division',$s['division'])->exists()) {
                DB::table('company_settings')->insert(array_merge($s,['created_at'=>now(),'updated_at'=>now()]));
            }
        }

        // Default SLA
        if (!DB::table('sla_configs')->where('is_default',1)->exists()) {
            DB::table('sla_configs')->insert([
                'name'=>'Default SLA','is_default'=>1,
                'critical_response'=>60,'high_response'=>240,'medium_response'=>480,'low_response'=>1440,
                'critical_solve'=>240,'high_solve'=>480,'medium_solve'=>1440,'low_solve'=>4320,
                'created_at'=>now(),'updated_at'=>now()
            ]);
        }

        // Sample client
        if (!DB::table('clients')->where('email','it@contoh.co.id')->exists()) {
            DB::table('clients')->insert([
                'name'=>'PT Contoh Klien','type'=>'maintenance','division'=>'JPN',
                'company_name'=>'PT Contoh Klien','email'=>'it@contoh.co.id',
                'phone'=>'02100001111','pic_name'=>'Budi Santoso','pic_phone'=>'08111111111',
                'max_visits_per_month'=>4,'monthly_fee'=>3000000,'extra_visit_fee'=>500000,
                'sla_enabled'=>1,'contract_start'=>'2024-01-01','contract_end'=>'2025-12-31',
                'is_active'=>1,'created_at'=>now(),'updated_at'=>now()
            ]);
        }
    }
}
