<?php

namespace Modules\SystemCore\Database\Seeders;

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

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

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

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

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

        $suppliers = [
            [
                'name' => 'ABC Wholesalers',
                'company_name' => 'ABC Wholesalers Ltd.',
                'status' => 'active',
                'email' => 'sales@abcwholesalers.com',
                'phone_number' => '+1-555-1001',
                'mobile' => '+1-555-1002',
                'website' => 'https://abcwholesalers.com',
                'tax_number' => 'TAX-ABC-WH-001',
                'vat_number' => 'VAT-ABC-WH-001',
                'payment_term_id' => $defaultPaymentTerm?->id,
                'credit_limit' => 75000.00,
                'is_active' => true,
                'notes' => 'Primary wholesale supplier for general merchandise',
            ],
            [
                'name' => 'Global Supplies Inc',
                'company_name' => 'Global Supplies Incorporated',
                'status' => 'active',
                'email' => 'procurement@globalsupplies.com',
                'phone_number' => '+1-555-2001',
                'mobile' => '+1-555-2002',
                'website' => 'https://globalsupplies.com',
                'tax_number' => 'TAX-GS-002',
                'vat_number' => 'VAT-GS-002',
                'payment_term_id' => $paymentTerms->skip(1)->first()?->id,
                'credit_limit' => 100000.00,
                'is_active' => true,
                'notes' => 'International supplier with competitive pricing',
            ],
            [
                'name' => 'Prime Distributors LLC',
                'company_name' => 'Prime Distributors Limited Liability Company',
                'status' => 'active',
                'email' => 'orders@primedist.com',
                'phone_number' => '+1-555-3001',
                'mobile' => '+1-555-3002',
                'website' => 'https://primedist.com',
                'tax_number' => 'TAX-PD-003',
                'vat_number' => 'VAT-PD-003',
                'payment_term_id' => $paymentTerms->skip(2)->first()?->id,
                'credit_limit' => 50000.00,
                'is_active' => true,
                'notes' => 'Fast delivery and reliable service',
            ],
            [
                'name' => 'Mega Trade Partners',
                'company_name' => 'Mega Trade Partners Corporation',
                'status' => 'active',
                'email' => 'contact@megatrade.com',
                'phone_number' => '+1-555-4001',
                'mobile' => '+1-555-4002',
                'website' => 'https://megatrade.com',
                'tax_number' => 'TAX-MT-004',
                'vat_number' => 'VAT-MT-004',
                'payment_term_id' => $defaultPaymentTerm?->id,
                'credit_limit' => 80000.00,
                'is_active' => true,
                'notes' => 'Bulk supplier with volume discounts',
            ],
            [
                'name' => 'Quality Source Co',
                'company_name' => 'Quality Source Company',
                'status' => 'pending',
                'email' => 'info@qualitysource.com',
                'phone_number' => '+1-555-5001',
                'mobile' => '+1-555-5002',
                'website' => 'https://qualitysource.com',
                'tax_number' => 'TAX-QS-005',
                'vat_number' => 'VAT-QS-005',
                'payment_term_id' => $paymentTerms->skip(1)->first()?->id,
                'credit_limit' => 40000.00,
                'is_active' => true,
                'notes' => 'New supplier under evaluation - premium quality products',
            ],
        ];

        foreach ($suppliers as $supplierData) {
            CoreSupplier::create($supplierData);
        }

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