<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\InventoryItem;
use App\Models\Room;
use App\Models\ScanActivity;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        User::updateOrCreate(
            ['email' => 'admin@example.com'],
            [
                'nama' => 'Admin Biro Umum',
                'peran' => User::ROLE_ADMIN,
                'aktif' => true,
                'kata_sandi' => Hash::make('password'),
            ],
        );

        User::updateOrCreate(
            ['email' => 'petugas@example.com'],
            [
                'nama' => 'Petugas Lapangan',
                'peran' => User::ROLE_PETUGAS,
                'aktif' => true,
                'kata_sandi' => Hash::make('password'),
            ],
        );

        $categories = collect([
            ['nama_kategori' => 'Furniture', 'kode_kategori' => 'FUR', 'deskripsi' => 'Meja, kursi, lemari, dan perlengkapan kerja ruangan.'],
            ['nama_kategori' => 'Elektronik', 'kode_kategori' => 'ELK', 'deskripsi' => 'Perangkat kerja elektronik dan alat pendukung operasional.'],
            ['nama_kategori' => 'Senjata', 'kode_kategori' => 'SNJ', 'deskripsi' => 'Aset pengamanan yang memerlukan kontrol khusus.'],
            ['nama_kategori' => 'Sarana Klinik', 'kode_kategori' => 'KLN', 'deskripsi' => 'Peralatan dan fasilitas klinik internal.'],
        ])->mapWithKeys(fn (array $category) => [
            $category['kode_kategori'] => Category::updateOrCreate(
                ['kode_kategori' => $category['kode_kategori']],
                $category,
            ),
        ]);

        $rooms = collect([
            ['nama_ruangan' => 'Ruang Kepegawaian', 'kode_ruangan' => 'RKG-01', 'lantai' => 'Lantai 1', 'penanggung_jawab' => 'Siti Aminah'],
            ['nama_ruangan' => 'Ruang Pelayanan', 'kode_ruangan' => 'RPL-01', 'lantai' => 'Lantai 1', 'penanggung_jawab' => 'Andi Prasetyo'],
            ['nama_ruangan' => 'Gudang Arsip', 'kode_ruangan' => 'GAR-02', 'lantai' => 'Lantai 2', 'penanggung_jawab' => 'Rina Lestari'],
            ['nama_ruangan' => 'Ruang Pengamanan', 'kode_ruangan' => 'RPA-01', 'lantai' => 'Lantai Dasar', 'penanggung_jawab' => 'Dedi Hartono'],
            ['nama_ruangan' => 'Klinik Pratama', 'kode_ruangan' => 'KLP-01', 'lantai' => 'Lantai 1', 'penanggung_jawab' => 'dr. Maya Putri'],
        ])->mapWithKeys(fn (array $room) => [
            $room['kode_ruangan'] => Room::updateOrCreate(
                ['kode_ruangan' => $room['kode_ruangan']],
                [
                    ...$room,
                    'kode_qr' => 'QR-' . $room['kode_ruangan'],
                    'status_ruangan' => 'active',
                ],
            ),
        ]);

        $items = [
            ['name' => 'Kursi Kerja Ergonomis', 'asset_code' => 'INV-FUR-02491', 'category' => 'FUR', 'room' => 'RKG-01', 'year' => 2022, 'life' => 8, 'status' => 'good', 'due' => '2026-06-15'],
            ['name' => 'Printer Laser A4', 'asset_code' => 'INV-ELK-01877', 'category' => 'ELK', 'room' => 'RPL-01', 'year' => 2020, 'life' => 5, 'status' => 'maintenance', 'due' => '2026-05-10'],
            ['name' => 'Lemari Arsip Besi', 'asset_code' => 'INV-FUR-01134', 'category' => 'FUR', 'room' => 'GAR-02', 'year' => 2018, 'life' => 7, 'status' => 'replace', 'due' => null, 'replacement' => true],
            ['name' => 'Brankas Senjata Pendek', 'asset_code' => 'INV-SNJ-00042', 'category' => 'SNJ', 'room' => 'RPA-01', 'year' => 2021, 'life' => 10, 'status' => 'good', 'due' => '2026-07-01'],
            ['name' => 'Tensimeter Digital', 'asset_code' => 'INV-KLN-00518', 'category' => 'KLN', 'room' => 'KLP-01', 'year' => 2019, 'life' => 5, 'status' => 'broken', 'due' => '2026-05-12'],
            ['name' => 'AC Split 1 PK', 'asset_code' => 'INV-ELK-02240', 'category' => 'ELK', 'room' => 'RPL-01', 'year' => 2019, 'life' => 6, 'status' => 'maintenance', 'due' => '2026-05-20'],
            ['name' => 'Meja Layanan Front Office', 'asset_code' => 'INV-FUR-03015', 'category' => 'FUR', 'room' => 'RPL-01', 'year' => 2024, 'life' => 8, 'status' => 'good', 'due' => '2026-09-03'],
        ];

        $createdItems = collect($items)->mapWithKeys(function (array $item) use ($categories, $rooms) {
            return [
                $item['asset_code'] => InventoryItem::updateOrCreate(
                    ['kode_aset' => $item['asset_code']],
                    [
                        'id_kategori' => $categories[$item['category']]->id,
                        'id_ruangan' => $rooms[$item['room']]->id,
                        'nama_barang' => $item['name'],
                        'tahun_perolehan' => $item['year'],
                        'umur_manfaat_tahun' => $item['life'],
                        'status_barang' => $item['status'],
                        'tanggal_jadwal_pemeliharaan' => $item['due'],
                        'pengajuan_penggantian' => $item['replacement'] ?? false,
                    ],
                ),
            ];
        });

        $scanRows = [
            ['room' => 'RPL-01', 'item' => 'INV-ELK-01877', 'officer' => 'Andi', 'action' => 'scan', 'description' => 'Printer Laser A4 dipindai oleh Andi', 'minutes' => 4],
            ['room' => 'KLP-01', 'item' => 'INV-KLN-00518', 'officer' => 'Maya', 'action' => 'status_update', 'description' => 'Tensimeter Digital dilaporkan rusak', 'minutes' => 21],
            ['room' => 'GAR-02', 'item' => 'INV-FUR-01134', 'officer' => 'Rina', 'action' => 'replacement_request', 'description' => 'Lemari Arsip Besi diajukan penggantian', 'minutes' => 45],
        ];

        foreach ($scanRows as $scan) {
            ScanActivity::updateOrCreate(
                [
                    'id_ruangan' => $rooms[$scan['room']]->id,
                    'id_barang' => $createdItems[$scan['item']]->id,
                    'aktivitas' => $scan['action'],
                    'deskripsi' => $scan['description'],
                ],
                [
                    'nama_petugas' => $scan['officer'],
                    'dipindai_pada' => Carbon::now()->subMinutes($scan['minutes']),
                ],
            );
        }
    }
}
