-- Booking Module Foundation
-- Enums + 14 tables for the booking system

-- Enums
CREATE TYPE "BookingRecordStatus" AS ENUM ('PENDING', 'CONFIRMED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'NO_SHOW');
CREATE TYPE "BookingServiceType" AS ENUM ('INDIVIDUAL', 'GROUP');
CREATE TYPE "BookingMembershipKind" AS ENUM ('VISIT_BASED', 'UNLIMITED', 'HOUR_BASED', 'MIXED');
CREATE TYPE "BookingPaymentMethod" AS ENUM ('CARD', 'SBP', 'CASH', 'MEMBERSHIP', 'DEPOSIT', 'CERTIFICATE');
CREATE TYPE "BookingPaymentStatus" AS ENUM ('PENDING', 'COMPLETED', 'REFUNDED', 'FAILED');

-- 51. BookingServiceCategory
CREATE TABLE "booking_service_categories" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "position" INTEGER NOT NULL DEFAULT 0,
    "parentId" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_service_categories_pkey" PRIMARY KEY ("id")
);

-- 52. BookingService
CREATE TABLE "booking_services" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "categoryId" TEXT,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "duration" INTEGER NOT NULL,
    "bufferBefore" INTEGER NOT NULL DEFAULT 0,
    "bufferAfter" INTEGER NOT NULL DEFAULT 0,
    "price" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "type" "BookingServiceType" NOT NULL DEFAULT 'INDIVIDUAL',
    "capacity" INTEGER,
    "extraPlaces" INTEGER NOT NULL DEFAULT 0,
    "isOnline" BOOLEAN NOT NULL DEFAULT true,
    "membershipOnly" BOOLEAN NOT NULL DEFAULT false,
    "color" TEXT,
    "position" INTEGER NOT NULL DEFAULT 0,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_services_pkey" PRIMARY KEY ("id")
);

-- 53. BookingServiceStaff
CREATE TABLE "booking_service_staff" (
    "serviceId" TEXT NOT NULL,
    "staffId" TEXT NOT NULL,
    CONSTRAINT "booking_service_staff_pkey" PRIMARY KEY ("serviceId","staffId")
);

-- 54. BookingStaffSchedule
CREATE TABLE "booking_staff_schedules" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "staffId" TEXT NOT NULL,
    "dayOfWeek" INTEGER NOT NULL,
    "startTime" TEXT NOT NULL,
    "endTime" TEXT NOT NULL,
    "breakStart" TEXT,
    "breakEnd" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    CONSTRAINT "booking_staff_schedules_pkey" PRIMARY KEY ("id")
);

-- 55. BookingStaffException
CREATE TABLE "booking_staff_exceptions" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "staffId" TEXT NOT NULL,
    "date" DATE NOT NULL,
    "isWorkday" BOOLEAN NOT NULL DEFAULT false,
    "startTime" TEXT,
    "endTime" TEXT,
    "reason" TEXT,
    CONSTRAINT "booking_staff_exceptions_pkey" PRIMARY KEY ("id")
);

-- 56. BookingGroupEvent
CREATE TABLE "booking_group_events" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "serviceId" TEXT NOT NULL,
    "staffId" TEXT NOT NULL,
    "roomId" TEXT,
    "title" TEXT,
    "startAt" TIMESTAMP(3) NOT NULL,
    "endAt" TIMESTAMP(3) NOT NULL,
    "capacity" INTEGER NOT NULL,
    "bookedCount" INTEGER NOT NULL DEFAULT 0,
    "seriesId" TEXT,
    "metadata" JSONB,
    "isCancelled" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_group_events_pkey" PRIMARY KEY ("id")
);

-- 57. BookingRecord
CREATE TABLE "booking_records" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "number" INTEGER NOT NULL,
    "clientId" TEXT,
    "clientName" TEXT NOT NULL,
    "clientPhone" TEXT NOT NULL,
    "clientEmail" TEXT,
    "serviceId" TEXT NOT NULL,
    "staffId" TEXT NOT NULL,
    "groupEventId" TEXT,
    "startAt" TIMESTAMP(3) NOT NULL,
    "endAt" TIMESTAMP(3) NOT NULL,
    "status" "BookingRecordStatus" NOT NULL DEFAULT 'PENDING',
    "source" TEXT NOT NULL DEFAULT 'ADMIN',
    "places" INTEGER NOT NULL DEFAULT 1,
    "price" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "paidAmount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "membershipId" TEXT,
    "notes" TEXT,
    "cancelReason" TEXT,
    "confirmedAt" TIMESTAMP(3),
    "completedAt" TIMESTAMP(3),
    "cancelledAt" TIMESTAMP(3),
    "metadata" JSONB,
    "createdById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "crmDealId" TEXT,
    CONSTRAINT "booking_records_pkey" PRIMARY KEY ("id")
);

-- 58. BookingMembershipType
CREATE TABLE "booking_membership_types" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "kind" "BookingMembershipKind" NOT NULL,
    "totalVisits" INTEGER,
    "totalHours" DECIMAL(8,2),
    "validityDays" INTEGER NOT NULL,
    "price" DECIMAL(12,2) NOT NULL,
    "serviceIds" TEXT[],
    "autoActivate" BOOLEAN NOT NULL DEFAULT true,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_membership_types_pkey" PRIMARY KEY ("id")
);

-- 59. BookingClientMembership
CREATE TABLE "booking_client_memberships" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "clientId" TEXT NOT NULL,
    "typeId" TEXT NOT NULL,
    "remainingVisits" INTEGER,
    "remainingHours" DECIMAL(8,2),
    "activatedAt" TIMESTAMP(3),
    "expiresAt" TIMESTAMP(3),
    "frozenUntil" TIMESTAMP(3),
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_client_memberships_pkey" PRIMARY KEY ("id")
);

-- 60. BookingPayment
CREATE TABLE "booking_payments" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "recordId" TEXT NOT NULL,
    "amount" DECIMAL(12,2) NOT NULL,
    "method" "BookingPaymentMethod" NOT NULL,
    "status" "BookingPaymentStatus" NOT NULL DEFAULT 'PENDING',
    "externalId" TEXT,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "booking_payments_pkey" PRIMARY KEY ("id")
);

-- 61. BookingWaitlist
CREATE TABLE "booking_waitlist" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "clientName" TEXT NOT NULL,
    "clientPhone" TEXT NOT NULL,
    "serviceId" TEXT NOT NULL,
    "staffId" TEXT,
    "preferredDate" TIMESTAMP(3),
    "preferredTime" TEXT,
    "notes" TEXT,
    "convertedRecordId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "booking_waitlist_pkey" PRIMARY KEY ("id")
);

-- 62. BookingWebhookEndpoint
CREATE TABLE "booking_webhook_endpoints" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "name" TEXT,
    "url" TEXT NOT NULL,
    "secret" TEXT NOT NULL,
    "events" TEXT[],
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_webhook_endpoints_pkey" PRIMARY KEY ("id")
);

-- 63. BookingWebhookLog
CREATE TABLE "booking_webhook_logs" (
    "id" TEXT NOT NULL,
    "endpointId" TEXT NOT NULL,
    "event" TEXT NOT NULL,
    "payload" JSONB NOT NULL,
    "statusCode" INTEGER,
    "response" TEXT,
    "attempts" INTEGER NOT NULL DEFAULT 1,
    "nextRetry" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "booking_webhook_logs_pkey" PRIMARY KEY ("id")
);

-- 64. BookingCrmBridge
CREATE TABLE "booking_crm_bridges" (
    "id" TEXT NOT NULL,
    "clubId" TEXT NOT NULL,
    "isEnabled" BOOLEAN NOT NULL DEFAULT false,
    "pipelineId" TEXT,
    "newRecordStageId" TEXT,
    "completedStageId" TEXT,
    "cancelledStageId" TEXT,
    "createDealOnBook" BOOLEAN NOT NULL DEFAULT true,
    "syncClientData" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "booking_crm_bridges_pkey" PRIMARY KEY ("id")
);

-- Unique constraints
ALTER TABLE "booking_staff_schedules" ADD CONSTRAINT "booking_staff_schedules_clubId_staffId_dayOfWeek_key" UNIQUE ("clubId", "staffId", "dayOfWeek");
ALTER TABLE "booking_staff_exceptions" ADD CONSTRAINT "booking_staff_exceptions_clubId_staffId_date_key" UNIQUE ("clubId", "staffId", "date");
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_clubId_number_key" UNIQUE ("clubId", "number");
ALTER TABLE "booking_crm_bridges" ADD CONSTRAINT "booking_crm_bridges_clubId_key" UNIQUE ("clubId");

-- Indexes
CREATE INDEX "booking_service_categories_clubId_idx" ON "booking_service_categories"("clubId");
CREATE INDEX "booking_services_clubId_idx" ON "booking_services"("clubId");
CREATE INDEX "booking_services_categoryId_idx" ON "booking_services"("categoryId");
CREATE INDEX "booking_services_clubId_isActive_idx" ON "booking_services"("clubId", "isActive");
CREATE INDEX "booking_staff_schedules_clubId_idx" ON "booking_staff_schedules"("clubId");
CREATE INDEX "booking_staff_schedules_staffId_idx" ON "booking_staff_schedules"("staffId");
CREATE INDEX "booking_staff_exceptions_clubId_idx" ON "booking_staff_exceptions"("clubId");
CREATE INDEX "booking_group_events_clubId_idx" ON "booking_group_events"("clubId");
CREATE INDEX "booking_group_events_startAt_idx" ON "booking_group_events"("startAt");
CREATE INDEX "booking_group_events_seriesId_idx" ON "booking_group_events"("seriesId");
CREATE INDEX "booking_group_events_clubId_startAt_idx" ON "booking_group_events"("clubId", "startAt");
CREATE INDEX "booking_group_events_staffId_startAt_idx" ON "booking_group_events"("staffId", "startAt");
CREATE INDEX "booking_records_clubId_idx" ON "booking_records"("clubId");
CREATE INDEX "booking_records_staffId_startAt_idx" ON "booking_records"("staffId", "startAt");
CREATE INDEX "booking_records_clientId_idx" ON "booking_records"("clientId");
CREATE INDEX "booking_records_clientPhone_idx" ON "booking_records"("clientPhone");
CREATE INDEX "booking_records_status_idx" ON "booking_records"("status");
CREATE INDEX "booking_records_startAt_idx" ON "booking_records"("startAt");
CREATE INDEX "booking_records_crmDealId_idx" ON "booking_records"("crmDealId");
CREATE INDEX "booking_records_clubId_startAt_idx" ON "booking_records"("clubId", "startAt");
CREATE INDEX "booking_records_clubId_status_idx" ON "booking_records"("clubId", "status");
CREATE INDEX "booking_membership_types_clubId_idx" ON "booking_membership_types"("clubId");
CREATE INDEX "booking_client_memberships_clubId_idx" ON "booking_client_memberships"("clubId");
CREATE INDEX "booking_client_memberships_clientId_idx" ON "booking_client_memberships"("clientId");
CREATE INDEX "booking_client_memberships_typeId_idx" ON "booking_client_memberships"("typeId");
CREATE INDEX "booking_payments_clubId_idx" ON "booking_payments"("clubId");
CREATE INDEX "booking_payments_recordId_idx" ON "booking_payments"("recordId");
CREATE INDEX "booking_waitlist_clubId_idx" ON "booking_waitlist"("clubId");
CREATE INDEX "booking_webhook_endpoints_clubId_idx" ON "booking_webhook_endpoints"("clubId");
CREATE INDEX "booking_webhook_logs_endpointId_idx" ON "booking_webhook_logs"("endpointId");
CREATE INDEX "booking_webhook_logs_createdAt_idx" ON "booking_webhook_logs"("createdAt");

-- Foreign keys
ALTER TABLE "booking_service_categories" ADD CONSTRAINT "booking_service_categories_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_service_categories" ADD CONSTRAINT "booking_service_categories_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "booking_service_categories"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_services" ADD CONSTRAINT "booking_services_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_services" ADD CONSTRAINT "booking_services_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "booking_service_categories"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_service_staff" ADD CONSTRAINT "booking_service_staff_serviceId_fkey" FOREIGN KEY ("serviceId") REFERENCES "booking_services"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_service_staff" ADD CONSTRAINT "booking_service_staff_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_staff_schedules" ADD CONSTRAINT "booking_staff_schedules_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_staff_schedules" ADD CONSTRAINT "booking_staff_schedules_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_staff_exceptions" ADD CONSTRAINT "booking_staff_exceptions_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_staff_exceptions" ADD CONSTRAINT "booking_staff_exceptions_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_group_events" ADD CONSTRAINT "booking_group_events_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_group_events" ADD CONSTRAINT "booking_group_events_serviceId_fkey" FOREIGN KEY ("serviceId") REFERENCES "booking_services"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_group_events" ADD CONSTRAINT "booking_group_events_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_group_events" ADD CONSTRAINT "booking_group_events_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "rooms"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "clients"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_serviceId_fkey" FOREIGN KEY ("serviceId") REFERENCES "booking_services"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_groupEventId_fkey" FOREIGN KEY ("groupEventId") REFERENCES "booking_group_events"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_membershipId_fkey" FOREIGN KEY ("membershipId") REFERENCES "booking_client_memberships"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_records" ADD CONSTRAINT "booking_records_crmDealId_fkey" FOREIGN KEY ("crmDealId") REFERENCES "crm_deals"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "booking_membership_types" ADD CONSTRAINT "booking_membership_types_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_client_memberships" ADD CONSTRAINT "booking_client_memberships_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_client_memberships" ADD CONSTRAINT "booking_client_memberships_clientId_fkey" FOREIGN KEY ("clientId") REFERENCES "clients"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_client_memberships" ADD CONSTRAINT "booking_client_memberships_typeId_fkey" FOREIGN KEY ("typeId") REFERENCES "booking_membership_types"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_payments" ADD CONSTRAINT "booking_payments_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_payments" ADD CONSTRAINT "booking_payments_recordId_fkey" FOREIGN KEY ("recordId") REFERENCES "booking_records"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_waitlist" ADD CONSTRAINT "booking_waitlist_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_webhook_endpoints" ADD CONSTRAINT "booking_webhook_endpoints_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_webhook_logs" ADD CONSTRAINT "booking_webhook_logs_endpointId_fkey" FOREIGN KEY ("endpointId") REFERENCES "booking_webhook_endpoints"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "booking_crm_bridges" ADD CONSTRAINT "booking_crm_bridges_clubId_fkey" FOREIGN KEY ("clubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- RLS policies for booking tables
ALTER TABLE "booking_service_categories" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_services" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_service_staff" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_staff_schedules" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_staff_exceptions" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_group_events" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_records" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_membership_types" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_client_memberships" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_payments" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_waitlist" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_webhook_endpoints" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_webhook_logs" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "booking_crm_bridges" ENABLE ROW LEVEL SECURITY;

-- RLS policies: tenant isolation
DO $$
DECLARE
  tbl TEXT;
BEGIN
  FOR tbl IN
    SELECT unnest(ARRAY[
      'booking_service_categories', 'booking_services',
      'booking_staff_schedules', 'booking_staff_exceptions',
      'booking_group_events', 'booking_records',
      'booking_membership_types', 'booking_client_memberships',
      'booking_payments', 'booking_waitlist',
      'booking_webhook_endpoints', 'booking_crm_bridges'
    ])
  LOOP
    EXECUTE format(
      'CREATE POLICY tenant_isolation ON %I FOR ALL USING ("clubId" = current_setting(''app.club_id'', true))',
      tbl
    );
    EXECUTE format(
      'CREATE POLICY bypass_rls ON %I FOR ALL USING (current_setting(''app.bypass_rls'', true) = ''true'')',
      tbl
    );
  END LOOP;
END;
$$;
