DO $$ BEGIN CREATE TYPE "public"."sync_operation" AS ENUM('create', 'update', 'delete'); EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
DO $$ BEGIN CREATE TYPE "public"."transaction_scope" AS ENUM('household', 'private', 'child'); EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
CREATE TABLE "children" (
	"id" text PRIMARY KEY NOT NULL,
	"household_id" text NOT NULL,
	"name" text NOT NULL,
	"color" text DEFAULT '#378ADD' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sync_queue" (
	"id" text PRIMARY KEY NOT NULL,
	"household_id" text NOT NULL,
	"user_id" text NOT NULL,
	"operation" "sync_operation" NOT NULL,
	"table_name" text NOT NULL,
	"payload" jsonb NOT NULL,
	"attempts" numeric DEFAULT '0' NOT NULL,
	"last_error" text,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "vacation_entries" (
	"id" text PRIMARY KEY NOT NULL,
	"vacation_id" text NOT NULL,
	"created_by" text NOT NULL,
	"category_id" text,
	"amount" numeric(12, 2) NOT NULL,
	"currency" text DEFAULT 'EUR' NOT NULL,
	"description" text,
	"date" date NOT NULL,
	"synced_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "vacations" (
	"id" text PRIMARY KEY NOT NULL,
	"household_id" text NOT NULL,
	"name" text NOT NULL,
	"budget" numeric(12, 2),
	"currency" text DEFAULT 'EUR' NOT NULL,
	"starts_on" date,
	"ends_on" date,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "budget_contexts" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
DROP TABLE "budget_contexts" CASCADE;--> statement-breakpoint
ALTER TABLE "transactions" DROP CONSTRAINT IF EXISTS "transactions_budget_context_id_budget_contexts_id_fk";
--> statement-breakpoint
ALTER TABLE "transactions" ADD COLUMN "child_id" text;--> statement-breakpoint
ALTER TABLE "transactions" ADD COLUMN "scope" "transaction_scope" DEFAULT 'household' NOT NULL;--> statement-breakpoint
ALTER TABLE "transactions" ADD COLUMN "is_fixed" boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE "transactions" ADD COLUMN "is_carry_over" boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE "transactions" ADD COLUMN "synced_at" timestamp;--> statement-breakpoint
ALTER TABLE "children" ADD CONSTRAINT "children_household_id_households_id_fk" FOREIGN KEY ("household_id") REFERENCES "public"."households"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sync_queue" ADD CONSTRAINT "sync_queue_household_id_households_id_fk" FOREIGN KEY ("household_id") REFERENCES "public"."households"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sync_queue" ADD CONSTRAINT "sync_queue_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vacation_entries" ADD CONSTRAINT "vacation_entries_vacation_id_vacations_id_fk" FOREIGN KEY ("vacation_id") REFERENCES "public"."vacations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vacation_entries" ADD CONSTRAINT "vacation_entries_created_by_user_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vacation_entries" ADD CONSTRAINT "vacation_entries_category_id_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."categories"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vacations" ADD CONSTRAINT "vacations_household_id_households_id_fk" FOREIGN KEY ("household_id") REFERENCES "public"."households"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "children_household_id_idx" ON "children" USING btree ("household_id");--> statement-breakpoint
CREATE INDEX "sync_queue_household_id_idx" ON "sync_queue" USING btree ("household_id");--> statement-breakpoint
CREATE INDEX "vacation_entries_vacation_id_idx" ON "vacation_entries" USING btree ("vacation_id");--> statement-breakpoint
CREATE INDEX "vacations_household_id_idx" ON "vacations" USING btree ("household_id");--> statement-breakpoint
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_child_id_children_id_fk" FOREIGN KEY ("child_id") REFERENCES "public"."children"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "transactions_scope_idx" ON "transactions" USING btree ("scope");--> statement-breakpoint
ALTER TABLE "transactions" DROP COLUMN "budget_context_id";--> statement-breakpoint
DROP TYPE "public"."budget_context_type";