<?php

namespace Database\Seeders;

use App\Models\Asset;
use App\Models\AssetType;
use App\Models\Company;
use App\Models\User;
use Illuminate\Database\Seeder;

class AssetSeeder extends Seeder
{
    public function run(): void
    {
        // Target company_id = 2 (Kiyo Solutions Pvt Ltd — info@kiyosolutions.com)
        // Using the ID directly avoids fuzzy-name matching the wrong company.
        $companyId = 2;

        $company = Company::withoutGlobalScopes()->find($companyId);
        if (! $company) {
            $this->command->warn('Company ID 2 not found — skipping AssetSeeder.');
            return;
        }

        // Fetch staff members from this company for assignment
        $staffIds = User::withoutGlobalScopes()
            ->where('company_id', $companyId)
            ->forTenantDirectory()
            ->pluck('id')
            ->toArray();

        // Resolve asset type IDs (seeded under company_id = 2)
        $typeMap = AssetType::withoutGlobalScopes()
            ->where('company_id', $companyId)
            ->pluck('id', 'name')
            ->toArray();

        $pick = fn(string $name) => $typeMap[$name] ?? null;
        $staff = fn(int $i) => $staffIds[$i % max(count($staffIds), 1)] ?? null;

        $assets = [
            // --- Laptops ---
            [
                'name'          => 'Dell XPS 15 — #001',
                'asset_type_id' => $pick('Laptop'),
                'serial_number' => 'DL-XPS-2023-001',
                'assigned_to'   => $staff(0),
                'status'        => 'assigned',
                'purchase_date' => '2023-04-10',
                'notes'         => 'Primary dev machine — engineering team.',
            ],
            [
                'name'          => 'MacBook Pro M2 — #002',
                'asset_type_id' => $pick('Laptop'),
                'serial_number' => 'AP-MBP-M2-002',
                'assigned_to'   => $staff(1),
                'status'        => 'assigned',
                'purchase_date' => '2023-07-15',
                'notes'         => 'Assigned to design & UI team.',
            ],
            [
                'name'          => 'Lenovo ThinkPad E14 — #003',
                'asset_type_id' => $pick('Laptop'),
                'serial_number' => 'LN-TPE14-2024-003',
                'assigned_to'   => null,
                'status'        => 'available',
                'purchase_date' => '2024-01-20',
                'notes'         => 'Spare laptop kept in IT inventory.',
            ],
            [
                'name'          => 'HP EliteBook 840 — #004',
                'asset_type_id' => $pick('Laptop'),
                'serial_number' => 'HP-EB840-004',
                'assigned_to'   => null,
                'status'        => 'maintenance',
                'purchase_date' => '2022-09-05',
                'notes'         => 'Sent for battery replacement.',
            ],

            // --- Desktop Computers ---
            [
                'name'          => 'Dell OptiPlex 5090 — Desktop #01',
                'asset_type_id' => $pick('Desktop Computer'),
                'serial_number' => 'DL-OPX5090-01',
                'assigned_to'   => $staff(2),
                'status'        => 'assigned',
                'purchase_date' => '2023-02-01',
                'notes'         => 'Accounts department workstation.',
            ],
            [
                'name'          => 'Dell OptiPlex 5090 — Desktop #02',
                'asset_type_id' => $pick('Desktop Computer'),
                'serial_number' => 'DL-OPX5090-02',
                'assigned_to'   => $staff(3),
                'status'        => 'assigned',
                'purchase_date' => '2023-02-01',
                'notes'         => 'HR department workstation.',
            ],

            // --- Monitors ---
            [
                'name'          => 'LG UltraWide 34" — #M01',
                'asset_type_id' => $pick('Monitor'),
                'serial_number' => 'LG-UW34-M01',
                'assigned_to'   => $staff(0),
                'status'        => 'assigned',
                'purchase_date' => '2023-04-10',
                'notes'         => 'Secondary display for dev laptop.',
            ],
            [
                'name'          => 'Dell P2422H 24" — #M02',
                'asset_type_id' => $pick('Monitor'),
                'serial_number' => 'DL-P2422H-M02',
                'assigned_to'   => null,
                'status'        => 'available',
                'purchase_date' => '2024-03-08',
                'notes'         => 'Unboxed and ready for deployment.',
            ],

            // --- Mobile Phones ---
            [
                'name'          => 'iPhone 14 — #P01',
                'asset_type_id' => $pick('Mobile Phone'),
                'serial_number' => 'AP-IP14-P01',
                'assigned_to'   => $staff(1),
                'status'        => 'assigned',
                'purchase_date' => '2023-10-01',
                'notes'         => 'Sales & BD team travel phone.',
            ],
            [
                'name'          => 'Samsung Galaxy A55 — #P02',
                'asset_type_id' => $pick('Mobile Phone'),
                'serial_number' => 'SS-GA55-P02',
                'assigned_to'   => null,
                'status'        => 'available',
                'purchase_date' => '2024-02-12',
                'notes'         => 'Standby device for field support.',
            ],

            // --- Headsets ---
            [
                'name'          => 'Jabra Evolve2 55 — #H01',
                'asset_type_id' => $pick('Headset / Headphone'),
                'serial_number' => 'JB-EV255-H01',
                'assigned_to'   => $staff(4),
                'status'        => 'assigned',
                'purchase_date' => '2023-06-20',
                'notes'         => 'Customer support agent — noise-cancelling.',
            ],
            [
                'name'          => 'Sony WH-1000XM5 — #H02',
                'asset_type_id' => $pick('Headset / Headphone'),
                'serial_number' => 'SN-WH1000XM5-H02',
                'assigned_to'   => $staff(5),
                'status'        => 'assigned',
                'purchase_date' => '2023-08-14',
                'notes'         => 'Management office.',
            ],

            // --- Networking Equipment ---
            [
                'name'          => 'Cisco Catalyst 2960 Switch',
                'asset_type_id' => $pick('Networking Equipment'),
                'serial_number' => 'CS-CAT2960-NET01',
                'assigned_to'   => null,
                'status'        => 'available',
                'purchase_date' => '2022-11-15',
                'notes'         => 'Core L2 switch for server room.',
            ],
            [
                'name'          => 'TP-Link EAP670 Access Point — #AP01',
                'asset_type_id' => $pick('Networking Equipment'),
                'serial_number' => 'TP-EAP670-AP01',
                'assigned_to'   => null,
                'status'        => 'maintenance',
                'purchase_date' => '2023-03-10',
                'notes'         => 'Firmware update & reconfiguration pending.',
            ],

            // --- UPS ---
            [
                'name'          => 'APC Smart-UPS 1500VA — #U01',
                'asset_type_id' => $pick('UPS / Power Backup'),
                'serial_number' => 'APC-SU1500-U01',
                'assigned_to'   => null,
                'status'        => 'available',
                'purchase_date' => '2023-01-05',
                'notes'         => 'Protecting server room equipment.',
            ],

            // --- Server ---
            [
                'name'          => 'Dell PowerEdge R640 Server',
                'asset_type_id' => $pick('Server'),
                'serial_number' => 'DL-PE-R640-SRV01',
                'assigned_to'   => null,
                'status'        => 'assigned',
                'purchase_date' => '2022-07-20',
                'notes'         => 'Primary application & DB server.',
            ],

            // --- Software Licenses ---
            [
                'name'          => 'Microsoft 365 Business — 25 Seats',
                'asset_type_id' => $pick('Software License'),
                'serial_number' => 'MS-365BIZ-25-2024',
                'assigned_to'   => null,
                'status'        => 'assigned',
                'purchase_date' => '2024-01-01',
                'notes'         => 'Annual subscription — renewal Jan 2025.',
            ],
            [
                'name'          => 'Adobe Creative Cloud — 5 Seats',
                'asset_type_id' => $pick('Software License'),
                'serial_number' => 'AD-CC-5-2023',
                'assigned_to'   => null,
                'status'        => 'available',
                'purchase_date' => '2023-04-01',
                'notes'         => 'Design team license pool.',
            ],

            // --- Office Furniture ---
            [
                'name'          => 'Ergonomic Chair — Batch A (10 units)',
                'asset_type_id' => $pick('Office Furniture'),
                'serial_number' => 'OFC-CHAIR-A-10',
                'assigned_to'   => null,
                'status'        => 'assigned',
                'purchase_date' => '2023-05-12',
                'notes'         => 'Main office floor — all 10 deployed.',
            ],
            [
                'name'          => 'Height-Adjustable Desk — #D01',
                'asset_type_id' => $pick('Office Furniture'),
                'serial_number' => 'OFC-DESK-ADJD01',
                'assigned_to'   => $staff(0),
                'status'        => 'retired',
                'purchase_date' => '2021-06-01',
                'notes'         => 'Old model — decommissioned after office renovation.',
            ],
        ];

        foreach ($assets as $data) {
            Asset::withoutGlobalScopes()->firstOrCreate(
                ['serial_number' => $data['serial_number'], 'company_id' => $companyId],
                array_merge($data, ['company_id' => $companyId])
            );
        }

        $this->command->info('AssetSeeder: seeded ' . count($assets) . ' assets for company "' . $company->name . '".');
    }
}
