<?php

namespace Database\Seeders;

use App\Enums\AssetCategoryEnum;
use App\Enums\AssetStatusEnum;
use App\Enums\PriceUnitEnum;
use App\Models\Asset;
use App\Models\AssetCategory;
use App\Models\ContactPerson;
use App\Models\Opd;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class AssetSeeder extends Seeder
{
    public function run(): void
    {
        $hall = AssetCategory::where('slug', AssetCategoryEnum::HALL->value)->first();
        $field = AssetCategory::where('slug', AssetCategoryEnum::FIELD->value)->first();
        $shop = AssetCategory::where('slug', AssetCategoryEnum::SHOP->value)->first();

        $bpkad = Opd::where('name', 'like', '%Keuangan dan Aset%')->first();
        $dispora = Opd::where('name', 'like', '%Pemuda dan Olahraga%')->first();
        $disdag = Opd::where('name', 'like', '%Perdagangan%')->first();

        $assets = [
            [
                'name' => 'Aula Serbaguna Balai Kota',
                'asset_category_id' => $hall?->id,
                'opd_id' => $bpkad?->id,
                'address' => 'Jl. Merdeka No. 1',
                'latitude' => -6.175392,
                'longitude' => 106.827153,
                'rental_price' => 2500000,
                'price_unit' => PriceUnitEnum::DAY,
                'description' => 'Aula serbaguna dengan kapasitas 500 orang untuk berbagai kegiatan.',
                'facilities' => 'AC, Sound System, Panggung, Kursi 500 unit, Parkir luas',
                'status' => AssetStatusEnum::AVAILABLE,
                'is_featured' => true,
                'contact' => [
                    'name' => 'Budi Santoso',
                    'position' => 'Pengelola Aula',
                    'phone_number' => '081234567890',
                    'email' => 'budi@example.go.id',
                ],
            ],
            [
                'name' => 'Lapangan Futsal Sport Center',
                'asset_category_id' => $field?->id,
                'opd_id' => $dispora?->id,
                'address' => 'Jl. Pemuda No. 10',
                'latitude' => -6.200000,
                'longitude' => 106.816666,
                'rental_price' => 150000,
                'price_unit' => PriceUnitEnum::HOUR,
                'description' => 'Lapangan futsal indoor dengan rumput sintetis standar nasional.',
                'facilities' => 'Rumput sintetis, Ruang ganti, Toilet, Parkir',
                'status' => AssetStatusEnum::AVAILABLE,
                'is_featured' => true,
                'contact' => [
                    'name' => 'Ahmad Rizki',
                    'position' => 'Koordinator Lapangan',
                    'phone_number' => '081298765432',
                    'email' => 'ahmad@example.go.id',
                ],
            ],
            [
                'name' => 'Ruko Pasar Modern Blok A-12',
                'asset_category_id' => $shop?->id,
                'opd_id' => $disdag?->id,
                'address' => 'Jl. Dagang No. 5 Blok A-12',
                'latitude' => -6.180000,
                'longitude' => 106.840000,
                'rental_price' => 3500000,
                'price_unit' => PriceUnitEnum::MONTH,
                'description' => 'Ruko 2 lantai lokasi strategis cocok untuk berbagai usaha.',
                'facilities' => 'Listrik, Air PDAM, Toilet, Area parkir',
                'status' => AssetStatusEnum::AVAILABLE,
                'is_featured' => false,
                'contact' => [
                    'name' => 'Siti Nurhaliza',
                    'position' => 'Admin Pasar',
                    'phone_number' => '081211112222',
                    'email' => 'siti@example.go.id',
                ],
            ],
        ];

        foreach ($assets as $data) {
            $contact = $data['contact'];
            unset($data['contact']);

            $data['slug'] = Str::slug($data['name']);
            $data['published_at'] = now();

            $asset = Asset::create($data);

            ContactPerson::updateOrCreate(
                ['asset_id' => $asset->id, 'name' => $contact['name']],
                array_merge($contact, ['asset_id' => $asset->id, 'is_primary' => true])
            );
        }
    }
}