<?php

namespace Database\Seeders;

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

class PartnerSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 1. Partner Categories
        $categories = [
            [
                'name' => 'General',
                'type' => 'both',
                'description' => 'General partners',
            ],
            [
                'name' => 'VIP',
                'type' => 'customer',
                'description' => 'VIP Customers',
            ],
            [
                'name' => 'Wholesale',
                'type' => 'supplier',
                'description' => 'Wholesale Suppliers',
            ],
        ];

        foreach ($categories as $cat) {
            DB::table('partner_categories')->insertOrIgnore(array_merge($cat, [
                'created_at' => now(),
                'updated_at' => now(),
            ]));
        }

        $generalCatId = DB::table('partner_categories')->where('name', 'General')->value('id');

        // 2. Partner Types
        $types = [
            [
                'name' => 'Customer',
                'slug' => 'customer',
                'is_customer_group' => true,
                'is_supplier_group' => false,
                'is_default' => true,
            ],
            [
                'name' => 'Supplier',
                'slug' => 'supplier',
                'is_customer_group' => false,
                'is_supplier_group' => true,
                'is_default' => false,
            ],
        ];

        foreach ($types as $type) {
            DB::table('partner_types')->updateOrInsert(
                ['slug' => $type['slug']],
                array_merge($type, ['created_at' => now(), 'updated_at' => now()])
            );
        }

        $customerTypeId = DB::table('partner_types')->where('slug', 'customer')->value('id');
        $supplierTypeId = DB::table('partner_types')->where('slug', 'supplier')->value('id');

        // 3. Default Walk-in Customer
        DB::table('partners')->insertOrIgnore([
            'partner_category_id' => $generalCatId,
            'partner_type_id' => $customerTypeId,
            'name' => 'Walk-in Client',
            'code' => 'CUST-001',
            'email' => 'walkin@example.com',
            'phone' => '0000000000',
            'current_balance' => 0,
            'is_active' => true,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
