<?php

namespace Database\Seeders\Demo;

use App\Models\Geo\City;
use App\Models\Lookups\PaymentTerm;
use App\Models\Lookups\Status;
use App\Models\Lookups\SupplierAddressType;
use App\Models\Purchasing\Supplier;
use App\Models\Purchasing\SupplierBankAccount;
use App\Models\Purchasing\SupplierContact;
use App\Services\SupplierService;
use Database\Seeders\Demo\Concerns\GeneratesDemoData;
use Illuminate\Database\Seeder;

class SupplierSeeder extends Seeder
{
    use GeneratesDemoData;

    private const SUPPLIER_COUNT = 18;

    public function run(): void
    {
        $paymentTermCodes = [PaymentTerm::COD, PaymentTerm::NET7, PaymentTerm::NET15, PaymentTerm::NET30, PaymentTerm::NET60];
        $cityIds = City::inRandomOrder()->limit(30)->pluck('id')->all();
        $activeStatusId = Status::idOf(Status::CTX_SUPPLIER, Status::SUPPLIER_ACTIVE);
        $banks = ['Commercial Bank', 'Sampath Bank', 'Hatton National Bank', "People's Bank", 'Bank of Ceylon', 'Seylan Bank'];

        $suppliers = [];

        for ($i = 1; $i <= self::SUPPLIER_COUNT; $i++) {
            $name = $this->businessName();

            $supplier = Supplier::create([
                'name' => $name,
                'business_name' => $name,
                'registration_number' => 'PV'.fake()->numerify('#####'),
                'tax_number' => 'VAT'.fake()->numerify('########'),
                'phone' => $this->landline(),
                'email' => strtolower(str_replace([' ', '(', ')'], ['', '', ''], $name)).'@supplier.lk',
                'payment_terms_id' => PaymentTerm::idOf(fake()->randomElement($paymentTermCodes)),
                'credit_limit' => fake()->randomElement([0, 100000, 250000, 500000, 1000000]),
                'outstanding_balance' => 0,
                'status_id' => $activeStatusId,
                'is_blacklisted' => false,
                'notes' => null,
            ]);

            SupplierContact::create([
                'supplier_id' => $supplier->id,
                'name' => fake()->name(),
                'designation' => fake()->randomElement(['Sales Manager', 'Accounts Executive', 'Owner', 'Procurement Officer']),
                'phone' => $this->srPhone(),
                'email' => $supplier->email,
                'is_primary' => true,
            ]);

            $supplier->addresses()->create([
                'address_type_id' => SupplierAddressType::idOf(SupplierAddressType::BILLING),
                'street_line1' => 'No. '.random_int(1, 400).', '.fake()->streetName(),
                'city_id' => fake()->randomElement($cityIds),
                'is_primary' => true,
            ]);

            SupplierBankAccount::create([
                'supplier_id' => $supplier->id,
                'bank_name' => fake()->randomElement($banks),
                'branch_name' => fake()->city(),
                'account_number' => fake()->numerify('##########'),
                'account_holder' => $name,
                'is_primary' => true,
            ]);

            $suppliers[] = $supplier;
        }

        // Blacklist two suppliers for state variety.
        foreach (array_slice($suppliers, -2) as $supplier) {
            app(SupplierService::class)->blacklist($supplier, 'Repeated late deliveries and quality issues.');
        }
    }
}
