ALTER TABLE qipai_stores
  ADD COLUMN longitude DECIMAL(10, 7) NULL AFTER address,
  ADD COLUMN latitude DECIMAL(10, 7) NULL AFTER longitude,
  ADD COLUMN wifi_ssid VARCHAR(128) NOT NULL DEFAULT '' AFTER contact_phone,
  ADD COLUMN wifi_password VARCHAR(255) NOT NULL DEFAULT '' AFTER wifi_ssid,
  ADD COLUMN notification_url VARCHAR(512) NOT NULL DEFAULT '' AFTER wifi_password,
  ADD COLUMN sort_order INT NOT NULL DEFAULT 0 AFTER business_status;

CREATE TABLE IF NOT EXISTS qipai_store_business_hours (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  weekday TINYINT UNSIGNED NOT NULL,
  open_minute SMALLINT UNSIGNED NOT NULL,
  close_minute SMALLINT UNSIGNED NOT NULL,
  is_closed TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_store_hours_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_store_hours_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_store_hours_weekday (tenant_id, store_id, weekday),
  CONSTRAINT chk_qipai_store_hours_weekday CHECK (weekday BETWEEN 1 AND 7),
  CONSTRAINT chk_qipai_store_hours_minute CHECK (open_minute <= 1439 AND close_minute <= 1439)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_room_categories (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  name VARCHAR(128) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_room_categories_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_room_categories_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_room_categories_name (tenant_id, store_id, name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE qipai_rooms
  ADD COLUMN category_id BIGINT UNSIGNED NULL AFTER store_id,
  ADD COLUMN weekday_price_cents INT UNSIGNED NOT NULL DEFAULT 0 AFTER base_price_cents,
  ADD COLUMN holiday_price_cents INT UNSIGNED NOT NULL DEFAULT 0 AFTER weekday_price_cents,
  ADD COLUMN overnight_price_cents INT UNSIGNED NOT NULL DEFAULT 0 AFTER holiday_price_cents,
  ADD COLUMN deposit_cents INT UNSIGNED NOT NULL DEFAULT 0 AFTER overnight_price_cents,
  ADD COLUMN minimum_minutes SMALLINT UNSIGNED NOT NULL DEFAULT 60 AFTER deposit_cents,
  ADD COLUMN max_advance_start_minutes SMALLINT UNSIGNED NOT NULL DEFAULT 0 AFTER minimum_minutes,
  ADD COLUMN max_advance_days SMALLINT UNSIGNED NOT NULL DEFAULT 30 AFTER max_advance_start_minutes,
  ADD COLUMN configuration_status VARCHAR(32) NOT NULL DEFAULT 'ENABLED' AFTER max_advance_days,
  ADD COLUMN operational_status VARCHAR(32) NOT NULL DEFAULT 'AVAILABLE' AFTER configuration_status,
  ADD COLUMN tags JSON NULL AFTER operational_status,
  ADD COLUMN images JSON NULL AFTER tags,
  ADD COLUMN sort_order INT NOT NULL DEFAULT 0 AFTER images,
  ADD CONSTRAINT fk_qipai_rooms_category FOREIGN KEY (category_id) REFERENCES qipai_room_categories(id);

UPDATE qipai_rooms
SET configuration_status = CASE WHEN status = 'DISABLED' THEN 'DISABLED' ELSE 'ENABLED' END,
    operational_status = CASE
      WHEN status IN ('MAINTENANCE', 'RESERVED', 'IN_USE', 'CLEANING_REQUIRED') THEN status
      ELSE 'AVAILABLE'
    END;

CREATE TABLE IF NOT EXISTS qipai_room_disabled_periods (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  room_id BIGINT UNSIGNED NOT NULL,
  starts_at DATETIME(3) NOT NULL,
  ends_at DATETIME(3) NOT NULL,
  reason VARCHAR(255) NOT NULL DEFAULT '',
  created_by BIGINT UNSIGNED NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_room_disabled_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_room_disabled_room FOREIGN KEY (room_id) REFERENCES qipai_rooms(id),
  CONSTRAINT fk_qipai_room_disabled_creator FOREIGN KEY (created_by) REFERENCES qipai_users(id),
  KEY idx_qipai_room_disabled_window (tenant_id, room_id, starts_at, ends_at),
  CONSTRAINT chk_qipai_room_disabled_window CHECK (ends_at > starts_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061807', 'm03a_store_room_domain');
