<?php

namespace Database\Seeders;

use App\Models\Hotel;
use App\Models\HousekeepingTask;
use App\Models\Room;
use App\Models\RoomAmenity;
use App\Models\RoomInspection;
use App\Models\RoomInspectionItem;
use App\Models\User;
use App\Support\Tenancy;
use Illuminate\Database\Seeder;

class RoomInspectionSeeder extends Seeder
{
    public function run(): void
    {
        Tenancy::set(Hotel::first());

        // Every item the user explicitly mentioned + realistic hotel room extras.
        $amenities = [
            // [name, category, type, icon, qty]
            ['Shower', 'bathroom', 'fixed', '🚿', 1],
            ['Bathtub', 'bathroom', 'fixed', '🛁', 1],
            ['Toilet', 'bathroom', 'fixed', '🚽', 1],
            ['Sink & mirror', 'bathroom', 'fixed', '🪞', 1],
            ['Water heater', 'bathroom', 'fixed', '♨️', 1],
            ['Bath towels', 'linen', 'reusable', '🧻', 2],
            ['Hand towels', 'linen', 'reusable', '🧻', 2],
            ['Bath robes', 'linen', 'reusable', '👘', 2],
            ['Foot mats', 'linen', 'reusable', '🥾', 2],
            ['Bed spreads', 'linen', 'reusable', '🛏', 1],
            ['Pillow cases', 'linen', 'reusable', '🛏', 4],
            ['Duvet / blanket', 'linen', 'reusable', '🛏', 1],
            ['Extra pillow', 'linen', 'on_demand', '🛏', 1],
            ['Toiletries kit', 'amenities_kit', 'dedicated', '🧴', 1],
            ['Welcome water bottles', 'amenities_kit', 'dedicated', '💧', 2],
            ['Notepad & pen', 'amenities_kit', 'dedicated', '📝', 1],
            ['Fruit basket', 'amenities_kit', 'on_demand', '🍎', 1],
            ['TV', 'electronics', 'fixed', '📺', 1],
            ['TV remote', 'electronics', 'dedicated', '🎮', 1],
            ['Kettle', 'electronics', 'fixed', '🫖', 1],
            ['Mini fridge', 'electronics', 'fixed', '🧊', 1],
            ['Hairdryer', 'electronics', 'dedicated', '💨', 1],
            ['Wi-Fi router', 'connectivity', 'fixed', '📶', 1],
            ['Dedicated internet line', 'connectivity', 'dedicated', '🌐', 1],
            ['Bed frame', 'furniture', 'fixed', '🛏', 1],
            ['Wardrobe', 'furniture', 'fixed', '🚪', 1],
            ['Work desk', 'furniture', 'fixed', '📐', 1],
            ['Desk chair', 'furniture', 'fixed', '🪑', 1],
            ['Sofa / lounge chair', 'furniture', 'fixed', '🛋', 1],
            ['Bedside lamps', 'lighting', 'fixed', '💡', 2],
            ['Ceiling light', 'lighting', 'fixed', '💡', 1],
            ['Bathroom light', 'lighting', 'fixed', '💡', 1],
            ['Air conditioner', 'climate', 'fixed', '❄️', 1],
            ['Thermostat', 'climate', 'fixed', '🌡', 1],
            ['Standing fan', 'climate', 'on_demand', '💨', 1],
            ['Safe deposit box', 'other', 'fixed', '🔒', 1],
        ];

        $amenityRecords = [];

        foreach ($amenities as [$name, $category, $type, $icon, $qty]) {
            $amenityRecords[$name] = RoomAmenity::create([
                'name' => $name,
                'category' => $category,
                'amenity_type' => $type,
                'icon' => $icon,
                'default_quantity' => $qty,
            ]);
        }

        // Seed one completed inspection with 2 issues for the flagship's first room
        $room = Room::orderBy('number')->first();
        $inspector = User::where('role', 'housekeeper')->first() ?? User::where('role', 'admin')->first();

        if ($room && $inspector) {
            $inspection = RoomInspection::create([
                'room_id' => $room->id,
                'inspected_by' => $inspector->id,
                'inspected_at' => now()->subHour(),
                'overall_status' => 'completed',
                'issues_found' => 2,
                'notes' => 'Post-checkout inspection.',
            ]);

            foreach ($amenityRecords as $name => $amenity) {
                $condition = 'intact';
                $noteText = null;

                if ($name === 'Ceiling light') {
                    $condition = 'needs_maintenance';
                    $noteText = 'Bulb flickering — replace with LED.';
                } elseif ($name === 'Bath towels') {
                    $condition = 'needs_fix';
                    $noteText = 'One towel torn — send to laundry.';
                }

                RoomInspectionItem::create([
                    'room_inspection_id' => $inspection->id,
                    'room_amenity_id' => $amenity->id,
                    'condition' => $condition,
                    'quantity_found' => $amenity->default_quantity,
                    'notes' => $noteText,
                ]);
            }

            // Auto-tasks the workflow would create
            HousekeepingTask::create([
                'room_id' => $room->id, 'task_type' => 'maintenance', 'priority' => 'normal',
                'status' => 'pending', 'scheduled_for' => today(),
                'notes' => 'Auto: Ceiling light — needs_maintenance — Bulb flickering.',
                'assigned_to' => $inspector->id,
            ]);
            HousekeepingTask::create([
                'room_id' => $room->id, 'task_type' => 'cleaning', 'priority' => 'normal',
                'status' => 'pending', 'scheduled_for' => today(),
                'notes' => 'Auto: Bath towels — needs_fix — Send torn towel to laundry.',
                'assigned_to' => $inspector->id,
            ]);
        }
    }
}
