<?php

namespace App\Controllers;
use Config\Database;

class Hotels extends BaseController
{
    public function index()
    {
        $db = Database::connect();
        $city = $this->request->getGet('city');
        
        $builder = $db->table('hotels');
        if ($city) {
            $builder->like('city', $city, 'both');
        }
        
        $data['hotels'] = [];
        
        try {
            $data['hotels'] = $builder->get()->getResult();
        } catch (\Exception $e) {
            // Mock data fallback
            if(empty($data['hotels'])) {
                $data['hotels'] = [
                    (object)[
                        'id' => 1,
                        'name' => 'Taj Mahal Palace',
                        'city' => $city ?: 'Mumbai',
                        'star_rating' => 5,
                        'base_price_per_night' => 12000.00,
                        'description' => 'Iconic sea-facing luxury hotel in South Mumbai.'
                    ],
                    (object)[
                        'id' => 2,
                        'name' => 'The Oberoi',
                        'city' => $city ?: 'Mumbai',
                        'star_rating' => 5,
                        'base_price_per_night' => 14500.00,
                        'description' => 'Contemporary luxury and unmatched marine drive views.'
                    ],
                    (object)[
                        'id' => 3,
                        'name' => 'Budget Inn Express',
                        'city' => $city ?: 'Mumbai',
                        'star_rating' => 3,
                        'base_price_per_night' => 3500.00,
                        'description' => 'Affordable comfort near the airport.'
                    ]
                ];
            }
        }

        $data['title'] = 'Search Hotels';
        return view('hotels/index', $data);
    }
}
