-- Centralized payment gateways (replaces per-module YooKassa config)
CREATE TABLE IF NOT EXISTS hotel_payment_gateways (
  id           UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id     UUID NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  provider     TEXT NOT NULL DEFAULT 'yookassa', -- yookassa | stripe | tinkoff
  label        TEXT NOT NULL DEFAULT 'ЮКасса',
  shop_id      TEXT,
  secret_key   TEXT,
  currency     TEXT NOT NULL DEFAULT 'RUB',
  is_active    BOOLEAN NOT NULL DEFAULT true,
  -- Which modules use this gateway (JSON array)
  modules      JSONB NOT NULL DEFAULT '["deposit","booking-widget","room-service"]'::jsonb,
  created_at   TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Migrate existing YooKassa credentials from hotel_deposit_settings
INSERT INTO hotel_payment_gateways (hotel_id, provider, label, shop_id, secret_key, currency, modules)
SELECT
  hotel_id,
  'yookassa',
  'ЮКасса',
  yookassa_shop_id,
  yookassa_secret_key,
  'RUB',
  '["deposit","booking-widget","room-service"]'::jsonb
FROM hotel_deposit_settings
WHERE yookassa_shop_id IS NOT NULL
ON CONFLICT DO NOTHING;

-- Online bookings table (from widget)
CREATE TABLE IF NOT EXISTS online_bookings (
  id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id        UUID NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  room_id         UUID REFERENCES rooms(id) ON DELETE SET NULL,
  guest_name      TEXT NOT NULL,
  guest_email     TEXT,
  guest_phone     TEXT,
  check_in        DATE NOT NULL,
  check_out       DATE NOT NULL,
  adults          INTEGER NOT NULL DEFAULT 1,
  children        INTEGER NOT NULL DEFAULT 0,
  total_amount    NUMERIC(12,2) NOT NULL DEFAULT 0,
  notes           TEXT,
  services        JSONB DEFAULT '[]'::jsonb,
  status          TEXT NOT NULL DEFAULT 'pending', -- pending | paid | confirmed | cancelled
  payment_method  TEXT DEFAULT 'none',             -- none | yookassa
  yookassa_payment_id TEXT,
  yookassa_confirmation_url TEXT,
  yookassa_status TEXT,
  booking_id      UUID REFERENCES bookings(id) ON DELETE SET NULL,
  created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
