CREATE TABLE "deal_categories" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"deal_type" text NOT NULL,
	"name_he" text NOT NULL,
	"name_en" text NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "deal_tag_assignments" (
	"deal_id" uuid NOT NULL,
	"tag_id" uuid NOT NULL,
	CONSTRAINT "deal_tag_assignments_deal_id_tag_id_pk" PRIMARY KEY("deal_id","tag_id")
);
--> statement-breakpoint
CREATE TABLE "deal_tags" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"category_id" uuid NOT NULL,
	"name_he" text NOT NULL,
	"name_en" text NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "category_id" uuid;--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "is_voucher" boolean DEFAULT false NOT NULL;--> statement-breakpoint
UPDATE "deals" SET "is_voucher" = true WHERE "deal_type" = 'VOUCHER';--> statement-breakpoint
ALTER TABLE "deals" ALTER COLUMN "deal_type" SET DATA TYPE text;--> statement-breakpoint
UPDATE "deals" SET "deal_type" = 'COUPON' WHERE "deal_type" NOT IN ('GROUP');--> statement-breakpoint
DROP TYPE "public"."deal_type";--> statement-breakpoint
CREATE TYPE "public"."deal_type" AS ENUM('COUPON', 'GROUP');--> statement-breakpoint
ALTER TABLE "deals" ALTER COLUMN "deal_type" SET DATA TYPE "public"."deal_type" USING "deal_type"::"public"."deal_type";--> statement-breakpoint
ALTER TABLE "deal_tag_assignments" ADD CONSTRAINT "deal_tag_assignments_deal_id_deals_id_fk" FOREIGN KEY ("deal_id") REFERENCES "public"."deals"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deal_tag_assignments" ADD CONSTRAINT "deal_tag_assignments_tag_id_deal_tags_id_fk" FOREIGN KEY ("tag_id") REFERENCES "public"."deal_tags"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deal_tags" ADD CONSTRAINT "deal_tags_category_id_deal_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."deal_categories"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "deal_categories_type_active_idx" ON "deal_categories" USING btree ("deal_type","is_active","sort_order");--> statement-breakpoint
CREATE INDEX "deal_tag_assignments_tag_idx" ON "deal_tag_assignments" USING btree ("tag_id");--> statement-breakpoint
CREATE INDEX "deal_tag_assignments_deal_idx" ON "deal_tag_assignments" USING btree ("deal_id");--> statement-breakpoint
CREATE INDEX "deal_tags_category_active_idx" ON "deal_tags" USING btree ("category_id","is_active","sort_order");--> statement-breakpoint
ALTER TABLE "deals" ADD CONSTRAINT "deals_category_id_deal_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."deal_categories"("id") ON DELETE no action ON UPDATE no action;