<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Customer;
use App\Models\VisitPlan;
use Illuminate\Support\Facades\Hash;

class CustomerVisitSeeder extends Seeder
{
    public function run(): void
    {
        // Pastikan user sales ada
        $sales = User::firstOrCreate(
            ['email' => 'sales@test.com'],
            [
                'name'     => 'Sales Lapangan',
                'password' => Hash::make('password'),
                'role' => 'sales'
            ]
        );

        // Data customer
        $customers = [
            [
                'name' => 'PT. Makmur Jaya Abadi',
                'address' => 'Jl. Sudirman No. 45, Jakarta Pusat',
                'phone' => '081234567890',
                'plafon' => 25000000,
            ],
            [
                'name' => 'CV. Sumber Rezeki',
                'address' => 'Jl. Gatot Subroto Km 5, Bandung',
                'phone' => '082345678901',
                'plafon' => 15000000,
            ],
            [
                'name' => 'UD. Berkah Sentosa',
                'address' => 'Jl. Ahmad Yani No. 12, Surabaya',
                'phone' => '083456789012',
                'plafon' => 10000000,
            ],
            [
                'name' => 'Toko Elektronik Maju',
                'address' => 'Jl. Pahlawan No. 8, Semarang',
                'phone' => '084567890123',
                'plafon' => 5000000,
            ],
            [
                'name' => 'Distro Fashion Style',
                'address' => 'Jl. Raya Darmo No. 99, Surabaya',
                'phone' => '085678901234',
                'plafon' => 7500000,
            ],
            [
                'name' => 'Apotek Sehat Keluarga',
                'address' => 'Jl. Diponegoro No. 23, Yogyakarta',
                'phone' => '086789012345',
                'plafon' => 8000000,
            ],
            [
                'name' => 'Restoran Sederhana',
                'address' => 'Jl. Merdeka No. 7, Malang',
                'phone' => '087890123456',
                'plafon' => 12000000,
            ],
            [
                'name' => 'Sport Station',
                'address' => 'Jl. Veteran No. 34, Solo',
                'phone' => '088901234567',
                'plafon' => 9000000,
            ],
        ];

        // Simpan customer dan buat visit plan untuk hari ini
        $today = now()->toDateString();
        $createdCustomers = [];

        foreach ($customers as $data) {
            $customer = Customer::updateOrCreate(
                ['phone' => $data['phone']],
                $data
            );
            $createdCustomers[] = $customer;

            // Buat visit plan untuk hari ini
            VisitPlan::updateOrCreate(
                [
                    'user_id' => $sales->id,
                    'customer_id' => $customer->id,
                    'visit_date' => $today,
                ],
                ['status' => 'pending']
            );
        }

        // Tambah 2 customer untuk besok (tidak muncul hari ini)
        $tomorrow = now()->addDay()->toDateString();

        $futureCustomers = [
            [
                'name' => 'PT. Future Jaya',
                'address' => 'Jl. Kuningan No. 56, Jakarta Selatan',
                'phone' => '089912345678',
                'plafon' => 20000000,
            ],
            [
                'name' => 'CV. Mega Perkasa',
                'address' => 'Jl. Thamrin No. 78, Jakarta Pusat',
                'phone' => '081023456789',
                'plafon' => 18000000,
            ],
        ];

        foreach ($futureCustomers as $data) {
            $customer = Customer::updateOrCreate(
                ['phone' => $data['phone']],
                $data
            );

            VisitPlan::updateOrCreate(
                [
                    'user_id' => $sales->id,
                    'customer_id' => $customer->id,
                    'visit_date' => $tomorrow,
                ],
                ['status' => 'pending']
            );
        }

        $this->command->info('═══════════════════════════════════');
        $this->command->info('✅ DATA BERHASIL DIISI!');
        $this->command->info('═══════════════════════════════════');
        $this->command->info('Jumlah Customer: ' . count($customers) . ' customer');
        $this->command->info('Kunjungan hari ini: ' . count($customers) . ' customer');
        $this->command->info('Kunjungan besok: 2 customer');
        $this->command->info('═══════════════════════════════════');
    }
}
