<?php

namespace Modules\SystemCore\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\SystemCore\App\Models\CoreCustomer;
use Modules\SystemCore\App\Models\CorePaymentTerm;

class CustomerSeeder extends Seeder
{
    public function run(): void
    {
        // Disable foreign key checks
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        // Truncate tables
        DB::table('core_customers')->truncate();

        // Re-enable foreign key checks
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        $paymentTerms = CorePaymentTerm::all();
        $defaultPaymentTerm = $paymentTerms->first();

        $customers = [
            [
                'name' => 'ABC Corporation',
                'company_name' => 'ABC Corporation Ltd.',
                'customer_type' => 'b2b',
                'email' => 'contact@abccorp.com',
                'phone' => '+1-555-0101',
                'mobile' => '+1-555-0102',
                'website' => 'https://abccorp.com',
                'tax_id' => 'TAX-ABC-001',
                'vat_number' => 'VAT-ABC-001',
                'payment_term_id' => $defaultPaymentTerm?->id,
                'credit_limit' => 50000.00,
                'status' => 'active',
                'is_active' => true,
                'notes' => 'Premium B2B customer with excellent credit history',
            ],
            [
                'name' => 'XYZ Enterprises',
                'company_name' => 'XYZ Enterprises Inc.',
                'customer_type' => 'b2b',
                'email' => 'info@xyzenterprises.com',
                'phone' => '+1-555-0201',
                'mobile' => '+1-555-0202',
                'website' => 'https://xyzenterprises.com',
                'tax_id' => 'TAX-XYZ-002',
                'vat_number' => 'VAT-XYZ-002',
                'payment_term_id' => $paymentTerms->skip(1)->first()?->id,
                'credit_limit' => 30000.00,
                'status' => 'active',
                'is_active' => true,
                'notes' => 'Regular B2B customer',
            ],
            [
                'name' => 'John Smith',
                'customer_type' => 'b2c',
                'email' => 'john.smith@email.com',
                'phone' => '+1-555-0301',
                'mobile' => '+1-555-0302',
                'payment_term_id' => $defaultPaymentTerm?->id,
                'credit_limit' => 5000.00,
                'status' => 'active',
                'is_active' => true,
                'notes' => 'Individual retail customer',
            ],
            [
                'name' => 'Global Distributors',
                'company_name' => 'Global Distributors LLC',
                'customer_type' => 'distributor',
                'email' => 'sales@globaldist.com',
                'phone' => '+1-555-0401',
                'mobile' => '+1-555-0402',
                'website' => 'https://globaldist.com',
                'tax_id' => 'TAX-GD-003',
                'vat_number' => 'VAT-GD-003',
                'payment_term_id' => $paymentTerms->skip(2)->first()?->id,
                'credit_limit' => 100000.00,
                'status' => 'active',
                'is_active' => true,
                'notes' => 'Major distributor partner',
            ],
            [
                'name' => 'Tech Solutions Inc',
                'company_name' => 'Tech Solutions Incorporated',
                'customer_type' => 'prospect',
                'email' => 'contact@techsolutions.com',
                'phone' => '+1-555-0501',
                'mobile' => '+1-555-0502',
                'website' => 'https://techsolutions.com',
                'status' => 'pending',
                'is_active' => true,
                'notes' => 'Prospective customer - in negotiation phase',
            ],
        ];

        foreach ($customers as $customerData) {
            CoreCustomer::create($customerData);
        }

        $this->command->info('Sample customers created successfully.');
    }
}
