CREATE TABLE accommodations (
                                id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -- Informação do alojamento
                                name VARCHAR(255) NOT NULL,                 -- "Casa Baleia Azul"[page:49]
                                license_number VARCHAR(50),                 -- "139489"[page:49]
                                license_type VARCHAR(10),                   -- "AL"[page:49]

                                address VARCHAR(255),                       -- "Rua São Sebastião, 20"[page:49]
                                postal_code VARCHAR(20),                    -- "9200-045"[page:49]
                                city VARCHAR(100),                          -- "Caniçal"[page:49]
                                region VARCHAR(100),                        -- "Madeira"[page:49]
                                country VARCHAR(100),                       -- "Portugal"[page:49]

    -- Layout do alojamento
                                property_type VARCHAR(50),                  -- "Apartamento"[page:49]
                                is_multi_unit TINYINT(1) NOT NULL DEFAULT 0,
                                unit_count INT UNSIGNED DEFAULT 1,

                                total_capacity INT UNSIGNED,                -- "Capacidade total" = 4[page:49]
                                extra_capacity INT UNSIGNED DEFAULT 0,      -- o segundo número (0)[page:49]
                                area_sqm DECIMAL(7,2),                      -- "Área" = 63 m2[page:49]
                                bathroom_count INT UNSIGNED,                -- "Casas de banho" = 1[page:49]

                                bedroom_count INT UNSIGNED,                 -- "*Quartos" = 1[page:49]

    -- Detalhe do quarto principal e áreas comuns
                                main_bedroom_bed_type VARCHAR(100),         -- "1 Cama king size"[page:49]
                                main_bedroom_private_bathroom TINYINT(1) DEFAULT 0,   -- checkbox "Casa de banho privativa"[page:49]

                                common_area_bed_type VARCHAR(100),          -- "1 Sofá-cama"[page:49]
                                allow_bed_type_selection TINYINT(1) DEFAULT 0,  -- checkbox "Permite seleção do tipo de cama"[page:49]

    -- Conteúdo textual multilíngua
                                default_language_code CHAR(2) DEFAULT 'pt', -- "Português" como principal[page:49]

                                latitude DECIMAL(10,8),                     -- 32.73810959[page:49]
                                longitude DECIMAL(11,8),                    -- -16.73908997[page:49]

                                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

                                INDEX idx_city (city),
                                INDEX idx_region (region),
                                INDEX idx_country (country)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================
-- ACCOMMODATIONS TABLE - SIMPLIFIED
-- ============================================
-- Remove campos antigos relacionados com quartos e camas
-- que agora estão nas tabelas normalizadas

ALTER TABLE accommodations
    DROP COLUMN bedroom_count,
    DROP COLUMN main_bedroom_bed_type,
    DROP COLUMN main_bedroom_private_bathroom,
    DROP COLUMN common_area_bed_type,
    DROP COLUMN allow_bed_type_selection;

-- Adicionar campo para permitir seleção de tipo de cama
ALTER TABLE accommodations
    ADD COLUMN allow_bed_type_selection TINYINT(1) DEFAULT 0 AFTER area_sqm;


-- ============================================
-- ROOM TYPES (Tipos de Quartos)
-- ============================================

CREATE TABLE room_types (
                            id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                            name VARCHAR(100) NOT NULL,
                            code VARCHAR(10) NOT NULL,
                            display_order INT UNSIGNED DEFAULT 0,
                            UNIQUE KEY uniq_room_type_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO room_types (id, name, code, display_order) VALUES
                                                           (1, 'Individual', 'SGL', 1),
                                                           (2, 'Duplo', 'DBL', 2),
                                                           (3, 'Twin', 'TWN', 3),
                                                           (4, 'Triplo', 'TPL', 4),
                                                           (5, 'Quádruplo', 'QAD', 5),
                                                           (6, 'Suite', 'SUITE', 6);

-- ============================================
-- BED TYPES (Tipos de Cama)
-- ============================================

CREATE TABLE bed_types (
                           id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                           name VARCHAR(100) NOT NULL,
                           display_order INT UNSIGNED DEFAULT 0,
                           UNIQUE KEY uniq_bed_type_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO bed_types (id, name, display_order) VALUES
                                                    (1, 'Cama de solteiro', 1),
                                                    (2, 'Cama dupla', 2),
                                                    (3, 'Cama king size', 3),
                                                    (4, 'Sofá-cama', 4),
                                                    (5, 'Berço', 5),
                                                    (6, 'Cama de criança', 6),
                                                    (7, 'Beliche', 7);

-- ============================================
-- ACCOMMODATION ROOMS (Quartos do Alojamento)
-- ============================================

CREATE TABLE accommodation_rooms (
                                     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                     accommodation_id INT UNSIGNED NOT NULL,
                                     room_type_id INT UNSIGNED NULL,             -- FK to room_types (SGL, DBL, etc.)

                                     room_number VARCHAR(50),                     -- "Quarto 1", "Quarto 2", etc.
                                     has_private_bathroom TINYINT(1) DEFAULT 0,   -- Casa de banho privativa

                                     display_order INT UNSIGNED DEFAULT 0,

                                     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                                     updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

                                     CONSTRAINT fk_acc_rooms_accommodation
                                         FOREIGN KEY (accommodation_id) REFERENCES accommodations(id)
                                             ON DELETE CASCADE,
                                     CONSTRAINT fk_acc_rooms_room_type
                                         FOREIGN KEY (room_type_id) REFERENCES room_types(id)
                                             ON DELETE SET NULL,

                                     INDEX idx_acc_rooms_accommodation (accommodation_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================
-- ROOM BEDS (Camas em cada Quarto)
-- ============================================

CREATE TABLE room_beds (
                           id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                           room_id INT UNSIGNED NOT NULL,              -- FK to accommodation_rooms
                           bed_type_id INT UNSIGNED NOT NULL,          -- FK to bed_types

                           quantity INT UNSIGNED NOT NULL DEFAULT 1,   -- número de camas deste tipo

                           CONSTRAINT fk_room_beds_room
                               FOREIGN KEY (room_id) REFERENCES accommodation_rooms(id)
                                   ON DELETE CASCADE,
                           CONSTRAINT fk_room_beds_bed_type
                               FOREIGN KEY (bed_type_id) REFERENCES bed_types(id)
                                   ON DELETE CASCADE,

                           UNIQUE KEY uniq_room_bed_type (room_id, bed_type_id),
                           INDEX idx_room_beds_room (room_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================
-- ACCOMMODATION COMMON AREA BEDS (Áreas Comuns)
-- ============================================

CREATE TABLE accommodation_common_area_beds (
                                                id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                                accommodation_id INT UNSIGNED NOT NULL,
                                                bed_type_id INT UNSIGNED NOT NULL,

                                                quantity INT UNSIGNED NOT NULL DEFAULT 1,

                                                CONSTRAINT fk_acc_common_beds_accommodation
                                                    FOREIGN KEY (accommodation_id) REFERENCES accommodations(id)
                                                        ON DELETE CASCADE,
                                                CONSTRAINT fk_acc_common_beds_bed_type
                                                    FOREIGN KEY (bed_type_id) REFERENCES bed_types(id)
                                                        ON DELETE CASCADE,

                                                UNIQUE KEY uniq_acc_common_bed_type (accommodation_id, bed_type_id),
                                                INDEX idx_acc_common_beds_accommodation (accommodation_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



CREATE TABLE amenity_categories (
                                    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                    name VARCHAR(100) NOT NULL,
                                    display_order INT UNSIGNED DEFAULT 0,
                                    UNIQUE KEY uniq_amenity_category_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE amenities (
                           id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                           category_id INT UNSIGNED NOT NULL,
                           name VARCHAR(150) NOT NULL,
                           icon VARCHAR(100),                  -- se usares ícones
                           display_order INT UNSIGNED DEFAULT 0,
                           is_active TINYINT(1) NOT NULL DEFAULT 1,
                           CONSTRAINT fk_amenities_category
                               FOREIGN KEY (category_id) REFERENCES amenity_categories(id)
                                   ON DELETE CASCADE,
                           UNIQUE KEY uniq_amenity_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE accommodation_amenities (
                                         id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                         accommodation_id INT UNSIGNED NOT NULL,
                                         amenity_id INT UNSIGNED NOT NULL,
                                         CONSTRAINT fk_acc_amenities_accommodation
                                             FOREIGN KEY (accommodation_id) REFERENCES accommodations(id)
                                                 ON DELETE CASCADE,
                                         CONSTRAINT fk_acc_amenities_amenity
                                             FOREIGN KEY (amenity_id) REFERENCES amenities(id)
                                                 ON DELETE CASCADE,
                                         UNIQUE KEY uniq_acc_amenity (accommodation_id, amenity_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================
-- RESTRICTIONS - BASIC
-- ============================================

-- Tabela principal de restrições básicas do alojamento
CREATE TABLE accommodation_restrictions (
                                            id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                            accommodation_id INT UNSIGNED NOT NULL,

    -- Check-in/Check-out times
                                            check_in_time_from TIME,                    -- "14:00"
                                            check_in_time_to TIME,                      -- "23:59"
                                            check_out_time TIME,                        -- "11:00"

    -- Boolean restrictions
                                            has_automatic_checkin TINYINT(1) DEFAULT 0, -- "Check-in automático por teclado"
                                            allows_smoking TINYINT(1) DEFAULT 0,        -- "É permitido fumar"
                                            allows_pets TINYINT(1) DEFAULT 0,           -- "São permitidos animais"
                                            allows_parties TINYINT(1) DEFAULT 0,        -- "São permitidas festas e eventos"

    -- Time between reservations
                                            days_between_reservations INT UNSIGNED DEFAULT 0,  -- "0 dias", "1 dia", etc.

                                            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                                            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

                                            CONSTRAINT fk_restrictions_accommodation
                                                FOREIGN KEY (accommodation_id) REFERENCES accommodations(id)
                                                    ON DELETE CASCADE,
                                            UNIQUE KEY uniq_acc_restrictions (accommodation_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================
-- PRICING CONFIGURATIONS (Ages & Prices)
-- ============================================

CREATE TABLE accommodation_pricing_config (
                                              id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                              accommodation_id INT UNSIGNED NOT NULL,

    -- Override flag
                                              has_custom_pricing TINYINT(1) DEFAULT 0,    -- "tem configurações adicionais de preços diferentes"

    -- Extra guest pricing
                                              adjust_price_by_guests TINYINT(1) DEFAULT 0,     -- "Ajustar preço em função do número de hóspedes"
                                              extra_guest_is_increase TINYINT(1) DEFAULT 1,    -- TRUE = Aumento, FALSE = Desconto
                                              extra_guest_amount DECIMAL(10,2),                -- "10,00"
                                              extra_guest_currency CHAR(3) DEFAULT 'EUR',      -- "€"
                                              extra_guest_base_count INT UNSIGNED,             -- "2 hóspedes"

    -- Children age range & pricing
                                              consider_children TINYINT(1) DEFAULT 0,
                                              children_age_from INT UNSIGNED,                  -- "2"
                                              children_age_to INT UNSIGNED,                    -- "0" (parece erro no UI, mas fica registado)

    -- Babies age range & pricing
                                              consider_babies TINYINT(1) DEFAULT 0,
                                              babies_age_from INT UNSIGNED,                    -- "0"
                                              babies_age_to INT UNSIGNED,                      -- "0"

                                              created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                                              updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

                                              CONSTRAINT fk_pricing_config_accommodation
                                                  FOREIGN KEY (accommodation_id) REFERENCES accommodations(id)
                                                      ON DELETE CASCADE,
                                              UNIQUE KEY uniq_acc_pricing_config (accommodation_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================
-- IMAGE CATEGORIES (Divisions/Rooms)
-- ============================================

CREATE TABLE image_categories (
                                  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                  name VARCHAR(100) NOT NULL,
                                  display_order INT UNSIGNED DEFAULT 0,
                                  UNIQUE KEY uniq_image_category_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- INSERTs das categorias visíveis na página
INSERT INTO image_categories (id, name, display_order) VALUES
                                                           (1, 'FOTOS DO QUARTO', 1),
                                                           (2, 'FOTOS DA SALA DE ESTAR', 2),
                                                           (3, 'FOTOS DA COZINHA', 3),
                                                           (4, 'FOTOS DA CASA DE BANHO', 4),
                                                           (5, 'FOTOS DO EXTERIOR', 5),
                                                           (6, 'OUTRAS FOTOS', 6);

-- ============================================
-- AMENITY CATEGORIES & AMENITIES - COMPLETE
-- ============================================

-- Categories
INSERT INTO amenity_categories (id, name, display_order) VALUES
                                                             (1, 'Casa de banho', 1),
                                                             (2, 'Quarto', 2),
                                                             (3, 'Cozinha', 3),
                                                             (4, 'Segurança', 4),
                                                             (5, 'Outros', 5);

-- Casa de banho amenities
INSERT INTO amenities (category_id, name, display_order) VALUES
                                                             (1, 'Produtos de higiene pessoal', 1),
                                                             (1, 'Toalhas', 2),
                                                             (1, 'Roupão', 3),
                                                             (1, 'Secador de cabelo', 4),
                                                             (1, 'Duche', 5),
                                                             (1, 'Banheira', 6),
                                                             (1, 'Banheira de hidromassagem', 7),
                                                             (1, 'Bidé', 8),

-- Quarto amenities
                                                             (2, 'Roupa de cama', 1),
                                                             (2, 'Almofadas', 2),
                                                             (2, 'Cabides', 3),
                                                             (2, 'Closet', 4),

-- Cozinha amenities
                                                             (3, 'Utensílios de cozinha', 1),
                                                             (3, 'Máquina de café', 2),
                                                             (3, 'Chaleira elétrica', 3),
                                                             (3, 'Torradeira', 4),
                                                             (3, 'Micro-ondas', 5),
                                                             (3, 'Frigorífico', 6),
                                                             (3, 'Fogão', 7),
                                                             (3, 'Forno', 8),
                                                             (3, 'Máquina de lavar louça', 9),
                                                             (3, 'Máquina de lavar roupa', 10),

-- Segurança amenities
                                                             (4, 'Cofre', 1),
                                                             (4, 'Alarme', 2),
                                                             (4, 'Extintor', 3),
                                                             (4, 'Kit primeiros socorros', 4),
                                                             (4, 'Detetores de fumo', 5),
                                                             (4, 'Videovigilância nas áreas comuns', 6),
                                                             (4, 'Elevador', 7),

-- Outros amenities
                                                             (5, 'Aquecimento', 1),
                                                             (5, 'Ar condicionado', 2),
                                                             (5, 'Wireless', 3),
                                                             (5, 'Secretária', 4),
                                                             (5, 'TV', 5),
                                                             (5, 'Canais por cabo', 6),
                                                             (5, 'Ferro de engomar', 7),
                                                             (5, 'Estendal', 8),
                                                             (5, 'Estacionamento', 9),
                                                             (5, 'Transfers', 10),
                                                             (5, 'Receção 24h', 11),
                                                             (5, 'Berço', 12),
                                                             (5, 'Varanda', 13),
                                                             (5, 'Piscina', 14),
                                                             (5, 'Lareira', 15);

-- Tabela de imagens dos alojamentos
CREATE TABLE accommodation_images (
                                      id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                                      accommodation_id INT UNSIGNED NOT NULL,
                                      image_category_id INT UNSIGNED NULL,

                                      image_url VARCHAR(500) NOT NULL,
                                      image_filename VARCHAR(255),
                                      image_size_kb INT UNSIGNED,

                                      sort_order INT UNSIGNED DEFAULT 0,
                                      is_cover TINYINT(1) NOT NULL DEFAULT 0,

                                      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                                      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

                                      CONSTRAINT fk_acc_images_accommodation
                                          FOREIGN KEY (accommodation_id) REFERENCES accommodations(id)
                                              ON DELETE CASCADE,
                                      CONSTRAINT fk_acc_images_category
                                          FOREIGN KEY (image_category_id) REFERENCES image_categories(id)
                                              ON DELETE SET NULL,

                                      INDEX idx_acc_images_accommodation (accommodation_id),
                                      INDEX idx_acc_images_category (image_category_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
