<?php

namespace Database\Seeders;

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

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

        $this->command->info('✓ User sales created');

        // Buat customer
        $customer = Customer::updateOrCreate(
            ['phone' => '08123456789'],
            [
                'name' => 'Toko Sinar Jaya',
                'address' => 'Jl. Raya No. 123, Jakarta',
                'phone' => '08123456789',
                'plafon' => 15000000,
            ]
        );

        $this->command->info('✓ Customer created');

        // Buat 10 kategori
        $categories = [
            ['name' => 'Elektronik', 'sequence' => 1],
            ['name' => 'Fashion Pria', 'sequence' => 2],
            ['name' => 'Fashion Wanita', 'sequence' => 3],
            ['name' => 'Makanan', 'sequence' => 4],
            ['name' => 'Minuman', 'sequence' => 5],
            ['name' => 'Kesehatan', 'sequence' => 6],
            ['name' => 'Kecantikan', 'sequence' => 7],
            ['name' => 'Olahraga', 'sequence' => 8],
            ['name' => 'Otomotif', 'sequence' => 9],
            ['name' => 'Perabotan', 'sequence' => 10],
        ];

        foreach ($categories as $cat) {
            Category::updateOrCreate(['name' => $cat['name']], $cat);
        }

        $this->command->info('✓ 10 Categories created');

        // Ambil semua kategori yang sudah dibuat
        $categoriesList = Category::all();

        // Buat produk untuk setiap kategori
        $productsData = [
            ['name' => 'Smartphone Samsung', 'sku' => 'ELEC001', 'price' => 3500000, 'stock' => 10],
            ['name' => 'Kemeja Pria', 'sku' => 'FASH001', 'price' => 150000, 'stock' => 50],
            ['name' => 'Dress Wanita', 'sku' => 'FASH002', 'price' => 200000, 'stock' => 30],
            ['name' => 'Indomie Goreng', 'sku' => 'FOOD001', 'price' => 3500, 'stock' => 500],
            ['name' => 'Teh Botol', 'sku' => 'DRINK001', 'price' => 5000, 'stock' => 200],
            ['name' => 'Vitamin C', 'sku' => 'HEAL001', 'price' => 25000, 'stock' => 100],
            ['name' => 'Lipstik', 'sku' => 'BEAU001', 'price' => 45000, 'stock' => 75],
            ['name' => 'Raket Badminton', 'sku' => 'SPOR001', 'price' => 350000, 'stock' => 20],
            ['name' => 'Oli Mesin', 'sku' => 'AUTO001', 'price' => 55000, 'stock' => 150],
            ['name' => 'Meja Kayu', 'sku' => 'FURN001', 'price' => 850000, 'stock' => 5],
        ];

        foreach ($categoriesList as $index => $category) {
            if (isset($productsData[$index])) {
                Product::updateOrCreate(
                    ['sku' => $productsData[$index]['sku']],
                    array_merge($productsData[$index], ['category_id' => $category->id])
                );
            }
        }

        $this->command->info('✓ Products created');

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

        $this->command->info('✓ Visit plan created');
        $this->command->newLine();
        $this->command->info('═══════════════════════════════════');
        $this->command->info('✅ TEST DATA BERHASIL DIISI!');
        $this->command->info('═══════════════════════════════════');
        $this->command->info('Email    : sales@test.com');
        $this->command->info('Password : password');
        $this->command->info('═══════════════════════════════════');
    }
}
