<?php

namespace Tenant\Database\Seeds;

use CodeIgniter\Database\Seeder;

/**
 * Local-dev/demo data only — invoked by `php spark tenant:create --demo`,
 * never automatically. Gives a freshly-provisioned hotel a few room types,
 * a handful of rooms, and a front-desk login to log in and try the app
 * with (the manager login is created by TenantProvisioner regardless of
 * --demo, since every real hotel needs at least one admin).
 */
class DemoHotelSeeder extends Seeder
{
    public const FRONT_DESK_EMAIL    = 'frontdesk@ahenfie-demo.test';
    public const FRONT_DESK_PASSWORD = 'FrontDesk123!';

    public function run()
    {
        $now = date('Y-m-d H:i:s');

        $roomTypes = [
            [
                'name'        => 'Standard Room',
                'description' => 'Comfortable room with queen bed, en-suite bathroom, and air conditioning.',
                'base_rate'   => 450.00,
                'capacity'    => 2,
                'amenities'   => json_encode(['Air conditioning', 'En-suite bathroom', 'Free Wi-Fi', 'TV']),
            ],
            [
                'name'        => 'Deluxe Room',
                'description' => 'Larger room with king bed, seating area, and city view.',
                'base_rate'   => 750.00,
                'capacity'    => 2,
                'amenities'   => json_encode(['Air conditioning', 'City view', 'Free Wi-Fi', 'TV', 'Mini fridge']),
            ],
            [
                'name'        => 'Executive Suite',
                'description' => 'Suite with separate living area, ideal for extended stays or business travelers.',
                'base_rate'   => 1200.00,
                'capacity'    => 3,
                'amenities'   => json_encode(['Air conditioning', 'Living area', 'Free Wi-Fi', 'TV', 'Mini fridge', 'Work desk']),
            ],
        ];

        foreach ($roomTypes as &$roomType) {
            $roomType['created_at'] = $now;
            $roomType['updated_at'] = $now;
        }
        unset($roomType);

        $this->db->table('room_types')->insertBatch($roomTypes);

        $typeIds = $this->db->table('room_types')->select('id, name')->get()->getResultArray();
        $typeIdByName = array_column($typeIds, 'id', 'name');

        $rooms = [];
        $layout = [
            'Standard Room'   => ['floor' => '1', 'numbers' => ['101', '102', '103', '104']],
            'Deluxe Room'     => ['floor' => '2', 'numbers' => ['201', '202', '203']],
            'Executive Suite' => ['floor' => '3', 'numbers' => ['301', '302']],
        ];

        foreach ($layout as $typeName => $config) {
            foreach ($config['numbers'] as $roomNumber) {
                $rooms[] = [
                    'room_number'  => $roomNumber,
                    'room_type_id' => $typeIdByName[$typeName],
                    'floor'        => $config['floor'],
                    'status'       => 'clean',
                    'created_at'   => $now,
                    'updated_at'   => $now,
                ];
            }
        }

        $this->db->table('rooms')->insertBatch($rooms);

        $this->db->table('users')->insert([
            'full_name'     => 'Front Desk',
            'email'         => self::FRONT_DESK_EMAIL,
            'password_hash' => password_hash(self::FRONT_DESK_PASSWORD, PASSWORD_DEFAULT),
            'role'          => 'front_desk',
            'is_active'     => 1,
            'created_at'    => $now,
            'updated_at'    => $now,
        ]);
    }
}
