<?php
class Guesthouse
{
    private $conn;
    private $table_name = "guesthouses";

    public $id;
    public $user_id;
    public $name;
    public $accommodation_type;
    public $responsible_name;
    public $phone;
    public $email;
    public $city;
    public $neighborhood;
    public $address;
    public $gps_coordinates;
    public $total_rooms;
    public $capacity;
    public $price_per_hour;
    public $price_per_2_hours;
    public $price_per_night;
    public $price;
    public $accepts_mpesa;
    public $accepts_emola;
    public $accepts_visa;
    public $preferred_contact_method;
    public $accepts_online_reserves;
    public $accepts_commission;
    public $description;
    public $has_wifi;
    public $has_parking;
    public $has_ac;
    public $has_security;
    public $has_cleaning;
    public $created_at;

    public function __construct($db)
    {
        $this->conn = $db;
    }

    public function create()
    {
        $query = "INSERT INTO " . $this->table_name . " SET 
                user_id=:user_id, 
                name=:name, 
                accommodation_type=:accommodation_type,
                responsible_name=:responsible_name,
                phone=:phone,
                email=:email,
                city=:city,
                neighborhood=:neighborhood,
                address=:address, 
                gps_coordinates=:gps_coordinates,
                total_rooms=:total_rooms,
                capacity=:capacity,
                price_per_hour=:price_per_hour,
                price_per_2_hours=:price_per_2_hours,
                price_per_night=:price_per_night,
                price=:price, 
                accepts_mpesa=:accepts_mpesa,
                accepts_emola=:accepts_emola,
                accepts_visa=:accepts_visa,
                preferred_contact_method=:preferred_contact_method,
                accepts_online_reserves=:accepts_online_reserves,
                accepts_commission=:accepts_commission,
                description=:description, 
                has_wifi=:has_wifi,
                has_parking=:has_parking,
                has_ac=:has_ac,
                has_security=:has_security,
                has_cleaning=:has_cleaning,
                created_at=:created_at";

        $stmt = $this->conn->prepare($query);

        // Sanitize
        $this->user_id = htmlspecialchars(strip_tags(trim($this->user_id ?? '')));
        $this->name = htmlspecialchars(strip_tags(trim($this->name ?? '')));

        // Ensure accommodation_type is valid or default to 'Guest House'
        $validTypes = ['Hotel', 'Guest House', 'Lodge', 'Apartamento'];
        $this->accommodation_type = trim($this->accommodation_type ?? '');
        if (!in_array($this->accommodation_type, $validTypes)) {
            $this->accommodation_type = 'Guest House';
        }

        $this->responsible_name = htmlspecialchars(strip_tags(trim($this->responsible_name ?? '')));
        $this->phone = htmlspecialchars(strip_tags(trim($this->phone ?? '')));
        $this->email = htmlspecialchars(strip_tags(trim($this->email ?? '')));
        $this->city = htmlspecialchars(strip_tags(trim($this->city ?? '')));
        $this->neighborhood = htmlspecialchars(strip_tags(trim($this->neighborhood ?? '')));
        $this->address = htmlspecialchars(strip_tags(trim($this->address ?? '')));
        $this->gps_coordinates = htmlspecialchars(strip_tags(trim($this->gps_coordinates ?? '')));

        // Handle optional numerical fields
        $this->total_rooms = !empty($this->total_rooms) ? htmlspecialchars(strip_tags(trim($this->total_rooms))) : null;
        $this->capacity = !empty($this->capacity) ? htmlspecialchars(strip_tags(trim($this->capacity))) : null;
        $this->price_per_hour = !empty($this->price_per_hour) ? htmlspecialchars(strip_tags(trim($this->price_per_hour))) : null;
        $this->price_per_2_hours = !empty($this->price_per_2_hours) ? htmlspecialchars(strip_tags(trim($this->price_per_2_hours))) : null;
        $this->price_per_night = !empty($this->price_per_night) ? htmlspecialchars(strip_tags(trim($this->price_per_night))) : null;

        $this->price = htmlspecialchars(strip_tags(trim($this->price ?? '0')));

        // Ensure preferred_contact_method is valid
        $validMethods = ['Telefone', 'WhatsApp', 'Email'];
        $this->preferred_contact_method = trim($this->preferred_contact_method ?? '');
        if (!in_array($this->preferred_contact_method, $validMethods)) {
            $this->preferred_contact_method = 'Telefone';
        }

        $this->description = htmlspecialchars(strip_tags(trim($this->description ?? '')));
        $this->created_at = date('Y-m-d H:i:s');

        // Bind values
        $stmt->bindValue(":user_id", $this->user_id);
        $stmt->bindValue(":name", $this->name);
        $stmt->bindValue(":accommodation_type", $this->accommodation_type);
        $stmt->bindValue(":responsible_name", $this->responsible_name);
        $stmt->bindValue(":phone", $this->phone);
        $stmt->bindValue(":email", $this->email);
        $stmt->bindValue(":city", $this->city);
        $stmt->bindValue(":neighborhood", $this->neighborhood);
        $stmt->bindValue(":address", $this->address);
        $stmt->bindValue(":gps_coordinates", $this->gps_coordinates);
        $stmt->bindValue(":total_rooms", $this->total_rooms);
        $stmt->bindValue(":capacity", $this->capacity);
        $stmt->bindValue(":price_per_hour", $this->price_per_hour);
        $stmt->bindValue(":price_per_2_hours", $this->price_per_2_hours);
        $stmt->bindValue(":price_per_night", $this->price_per_night);
        $stmt->bindValue(":price", $this->price);
        $stmt->bindValue(":accepts_mpesa", $this->accepts_mpesa ? 1 : 0);
        $stmt->bindValue(":accepts_emola", $this->accepts_emola ? 1 : 0);
        $stmt->bindValue(":accepts_visa", $this->accepts_visa ? 1 : 0);
        $stmt->bindValue(":preferred_contact_method", $this->preferred_contact_method);
        $stmt->bindValue(":accepts_online_reserves", $this->accepts_online_reserves ? 1 : 0);
        $stmt->bindValue(":accepts_commission", $this->accepts_commission ? 1 : 0);
        $stmt->bindValue(":description", $this->description);
        $stmt->bindValue(":has_wifi", $this->has_wifi ? 1 : 0);
        $stmt->bindValue(":has_parking", $this->has_parking ? 1 : 0);
        $stmt->bindValue(":has_ac", $this->has_ac ? 1 : 0);
        $stmt->bindValue(":has_security", $this->has_security ? 1 : 0);
        $stmt->bindValue(":has_cleaning", $this->has_cleaning ? 1 : 0);
        $stmt->bindValue(":created_at", $this->created_at);

        if ($stmt->execute()) {
            $this->id = $this->conn->lastInsertId();
            return true;
        }
        return false;
    }

    public function getAll()
    {
        $query = "SELECT g.*, 
                         (SELECT AVG(rating) FROM reviews WHERE guesthouse_id = g.id) as avg_rating,
                         (SELECT COUNT(*) FROM reviews WHERE guesthouse_id = g.id) as reviews_count,
                         (SELECT photo_url FROM guesthouse_photos WHERE guesthouse_id = g.id ORDER BY is_main DESC, id ASC LIMIT 1) as photo_url
                  FROM " . $this->table_name . " g 
                  ORDER BY g.created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    public function getByUserId($user_id)
    {
        $query = "SELECT g.*, 
                         (SELECT AVG(rating) FROM reviews WHERE guesthouse_id = g.id) as avg_rating,
                         (SELECT COUNT(*) FROM reviews WHERE guesthouse_id = g.id) as reviews_count,
                         (SELECT photo_url FROM guesthouse_photos WHERE guesthouse_id = g.id ORDER BY is_main DESC, id ASC LIMIT 1) as photo_url
                  FROM " . $this->table_name . " g 
                  WHERE g.user_id = ? 
                  ORDER BY g.created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();
        return $stmt;
    }

    public function getById($id)
    {
        $query = "SELECT g.*, 
                         (SELECT AVG(rating) FROM reviews WHERE guesthouse_id = g.id) as avg_rating,
                         (SELECT COUNT(*) FROM reviews WHERE guesthouse_id = g.id) as reviews_count,
                         (SELECT photo_url FROM guesthouse_photos WHERE guesthouse_id = g.id ORDER BY is_main DESC, id ASC LIMIT 1) as photo_url
                  FROM " . $this->table_name . " g 
                  WHERE g.id = ? LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $id);
        $stmt->execute();
        return $stmt;
    }

    public function update()
    {
        $query = "UPDATE " . $this->table_name . " 
                SET 
                    name=:name, 
                    accommodation_type=:accommodation_type,
                    responsible_name=:responsible_name,
                    phone=:phone,
                    email=:email,
                    city=:city,
                    neighborhood=:neighborhood,
                    address=:address, 
                    gps_coordinates=:gps_coordinates,
                    total_rooms=:total_rooms,
                    capacity=:capacity,
                    price_per_hour=:price_per_hour,
                    price_per_2_hours=:price_per_2_hours,
                    price_per_night=:price_per_night,
                    price=:price, 
                    accepts_mpesa=:accepts_mpesa,
                    accepts_emola=:accepts_emola,
                    accepts_visa=:accepts_visa,
                    preferred_contact_method=:preferred_contact_method,
                    accepts_online_reserves=:accepts_online_reserves,
                    accepts_commission=:accepts_commission,
                    description=:description,
                    has_wifi=:has_wifi,
                    has_parking=:has_parking,
                    has_ac=:has_ac,
                    has_security=:has_security,
                    has_cleaning=:has_cleaning
                WHERE id=:id";

        $stmt = $this->conn->prepare($query);

        // Sanitize
        $this->name = htmlspecialchars(strip_tags(trim($this->name ?? '')));

        $validTypes = ['Hotel', 'Guest House', 'Lodge', 'Apartamento'];
        $this->accommodation_type = trim($this->accommodation_type ?? '');
        if (!in_array($this->accommodation_type, $validTypes)) {
            $this->accommodation_type = 'Guest House';
        }

        $this->responsible_name = htmlspecialchars(strip_tags(trim($this->responsible_name ?? '')));
        $this->phone = htmlspecialchars(strip_tags(trim($this->phone ?? '')));
        $this->email = htmlspecialchars(strip_tags(trim($this->email ?? '')));
        $this->city = htmlspecialchars(strip_tags(trim($this->city ?? '')));
        $this->neighborhood = htmlspecialchars(strip_tags(trim($this->neighborhood ?? '')));
        $this->address = htmlspecialchars(strip_tags(trim($this->address ?? '')));
        $this->gps_coordinates = htmlspecialchars(strip_tags(trim($this->gps_coordinates ?? '')));

        $this->total_rooms = !empty($this->total_rooms) ? htmlspecialchars(strip_tags(trim($this->total_rooms))) : null;
        $this->capacity = !empty($this->capacity) ? htmlspecialchars(strip_tags(trim($this->capacity))) : null;
        $this->price_per_hour = !empty($this->price_per_hour) ? htmlspecialchars(strip_tags(trim($this->price_per_hour))) : null;
        $this->price_per_2_hours = !empty($this->price_per_2_hours) ? htmlspecialchars(strip_tags(trim($this->price_per_2_hours))) : null;
        $this->price_per_night = !empty($this->price_per_night) ? htmlspecialchars(strip_tags(trim($this->price_per_night))) : null;

        $this->price = htmlspecialchars(strip_tags(trim($this->price ?? '0')));

        $validMethods = ['Telefone', 'WhatsApp', 'Email'];
        $this->preferred_contact_method = trim($this->preferred_contact_method ?? '');
        if (!in_array($this->preferred_contact_method, $validMethods)) {
            $this->preferred_contact_method = 'Telefone';
        }

        $this->description = htmlspecialchars(strip_tags(trim($this->description ?? '')));
        $this->id = htmlspecialchars(strip_tags(trim($this->id)));

        // Bind values
        $stmt->bindValue(":name", $this->name);
        $stmt->bindValue(":accommodation_type", $this->accommodation_type);
        $stmt->bindValue(":responsible_name", $this->responsible_name);
        $stmt->bindValue(":phone", $this->phone);
        $stmt->bindValue(":email", $this->email);
        $stmt->bindValue(":city", $this->city);
        $stmt->bindValue(":neighborhood", $this->neighborhood);
        $stmt->bindValue(":address", $this->address);
        $stmt->bindValue(":gps_coordinates", $this->gps_coordinates);
        $stmt->bindValue(":total_rooms", $this->total_rooms);
        $stmt->bindValue(":capacity", $this->capacity);
        $stmt->bindValue(":price_per_hour", $this->price_per_hour);
        $stmt->bindValue(":price_per_2_hours", $this->price_per_2_hours);
        $stmt->bindValue(":price_per_night", $this->price_per_night);
        $stmt->bindValue(":price", $this->price);
        $stmt->bindValue(":accepts_mpesa", $this->accepts_mpesa ? 1 : 0);
        $stmt->bindValue(":accepts_emola", $this->accepts_emola ? 1 : 0);
        $stmt->bindValue(":accepts_visa", $this->accepts_visa ? 1 : 0);
        $stmt->bindValue(":preferred_contact_method", $this->preferred_contact_method);
        $stmt->bindValue(":accepts_online_reserves", $this->accepts_online_reserves ? 1 : 0);
        $stmt->bindValue(":accepts_commission", $this->accepts_commission ? 1 : 0);
        $stmt->bindValue(":description", $this->description);
        $stmt->bindValue(":has_wifi", $this->has_wifi ? 1 : 0);
        $stmt->bindValue(":has_parking", $this->has_parking ? 1 : 0);
        $stmt->bindValue(":has_ac", $this->has_ac ? 1 : 0);
        $stmt->bindValue(":has_security", $this->has_security ? 1 : 0);
        $stmt->bindValue(":has_cleaning", $this->has_cleaning ? 1 : 0);
        $stmt->bindValue(":id", $this->id);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    public function delete()
    {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }
}
?>