CREATE TABLE IF NOT EXISTS loyalty_settings (
  id SERIAL PRIMARY KEY,
  "agencyId" integer NULL,
  settings jsonb NOT NULL DEFAULT '{}'::jsonb,
  "createdAt" timestamp NOT NULL DEFAULT now(),
  "updatedAt" timestamp NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX IF NOT EXISTS ux_loyalty_settings_agency ON loyalty_settings (COALESCE("agencyId", 0));

ALTER TABLE customers ADD COLUMN IF NOT EXISTS "loyaltyPoints" integer NOT NULL DEFAULT 0;
ALTER TABLE customers ADD COLUMN IF NOT EXISTS "loyaltyTier" varchar(50) NOT NULL DEFAULT 'Standard';
ALTER TABLE customers ADD COLUMN IF NOT EXISTS "vipDiscountPercent" numeric(5,2) NOT NULL DEFAULT 0;
ALTER TABLE customers ADD COLUMN IF NOT EXISTS "vipSince" date NULL;
ALTER TABLE customers ADD COLUMN IF NOT EXISTS "vipBenefits" text NULL;
ALTER TABLE customers ADD COLUMN IF NOT EXISTS "loyaltyHistory" text NULL;
