CREATE TABLE minibar_stock (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id UUID NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  item_id UUID NOT NULL REFERENCES minibar_items(id) ON DELETE CASCADE,
  quantity NUMERIC(10,3) NOT NULL DEFAULT 0,
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  UNIQUE(hotel_id, item_id)
);

CREATE TABLE minibar_receipts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id UUID NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  supplier TEXT,
  notes TEXT,
  created_by UUID REFERENCES users(id),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE minibar_receipt_items (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  receipt_id UUID NOT NULL REFERENCES minibar_receipts(id) ON DELETE CASCADE,
  item_id UUID NOT NULL REFERENCES minibar_items(id),
  quantity NUMERIC(10,3) NOT NULL,
  cost_price NUMERIC(10,2)
);

CREATE TABLE minibar_writeoffs (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id UUID NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  reason TEXT NOT NULL,
  notes TEXT,
  created_by UUID REFERENCES users(id),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE minibar_writeoff_items (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  writeoff_id UUID NOT NULL REFERENCES minibar_writeoffs(id) ON DELETE CASCADE,
  item_id UUID NOT NULL REFERENCES minibar_items(id),
  quantity NUMERIC(10,3) NOT NULL
);

CREATE TABLE minibar_inventory_checks (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  hotel_id UUID NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  checked_at DATE NOT NULL DEFAULT CURRENT_DATE,
  notes TEXT,
  is_complete BOOLEAN NOT NULL DEFAULT false,
  created_by UUID REFERENCES users(id),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE minibar_inventory_items (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  check_id UUID NOT NULL REFERENCES minibar_inventory_checks(id) ON DELETE CASCADE,
  item_id UUID NOT NULL REFERENCES minibar_items(id),
  expected_qty NUMERIC(10,3) NOT NULL DEFAULT 0,
  actual_qty NUMERIC(10,3) NOT NULL DEFAULT 0
);

CREATE INDEX ON minibar_stock(hotel_id);
CREATE INDEX ON minibar_receipts(hotel_id);
CREATE INDEX ON minibar_writeoffs(hotel_id);
CREATE INDEX ON minibar_inventory_checks(hotel_id);
