-- HotelSync Database Schema
-- Migration 003 — Fix constraints and add missing columns

-- ── Rooms: исправить статус (был HK-статус, должен быть статус доступности) ──
ALTER TABLE rooms DROP CONSTRAINT IF EXISTS rooms_status_check;

UPDATE rooms SET status = CASE
  WHEN status = 'clean'         THEN 'available'
  WHEN status = 'dirty'         THEN 'available'
  WHEN status = 'out_of_order'  THEN 'blocked'
  ELSE status  -- 'maintenance' → 'maintenance'
END WHERE status IN ('clean', 'dirty', 'out_of_order');

ALTER TABLE rooms ADD CONSTRAINT rooms_status_check
  CHECK (status IN ('available', 'occupied', 'maintenance', 'blocked'));

ALTER TABLE rooms ALTER COLUMN status SET DEFAULT 'available';

-- ── Bookings: добавить 'inquiry' в статусы ──────────────────────────────────
ALTER TABLE bookings DROP CONSTRAINT IF EXISTS bookings_status_check;
ALTER TABLE bookings ADD CONSTRAINT bookings_status_check
  CHECK (status IN ('inquiry', 'confirmed', 'checked_in', 'checked_out', 'cancelled', 'no_show'));

-- ── Bookings: добавить 'other' в источники ─────────────────────────────────
ALTER TABLE bookings DROP CONSTRAINT IF EXISTS bookings_source_check;
ALTER TABLE bookings ADD CONSTRAINT bookings_source_check
  CHECK (source IN ('direct', 'booking_com', 'airbnb', 'expedia', 'vrbo', 'other'));

-- ── Bookings: добавить paid_amount ─────────────────────────────────────────
ALTER TABLE bookings ADD COLUMN IF NOT EXISTS paid_amount DECIMAL(10,2) NOT NULL DEFAULT 0;
