CREATE TABLE "mobilepay_agreement" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"agreement_id" text NOT NULL,
	"state" uuid NOT NULL,
	"subscription_id" uuid NOT NULL,
	"plan" text NOT NULL,
	"seats" integer NOT NULL,
	"amount" integer NOT NULL,
	"status" text DEFAULT 'PENDING' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "mobilepay_charge" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"agreement_id" text NOT NULL,
	"mobilepay_agreement_id" uuid NOT NULL,
	"subscription_id" uuid NOT NULL,
	"amount" integer NOT NULL,
	"currency" text DEFAULT 'DKK' NOT NULL,
	"provider_charge_id" text,
	"status" varchar(45) DEFAULT 'CREATED' NOT NULL,
	"due" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "stripe_customer_id" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "period_start" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "period_end" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "trial_start" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "trial_end" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "canceled_at" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "created_at" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "created_at" SET DEFAULT now();--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "updated_at" SET DATA TYPE timestamp with time zone;--> statement-breakpoint
ALTER TABLE "subscription" ALTER COLUMN "updated_at" SET DEFAULT now();--> statement-breakpoint
ALTER TABLE "subscription" ADD COLUMN "provider" varchar(15) DEFAULT 'stripe' NOT NULL;--> statement-breakpoint
ALTER TABLE "mobilepay_agreement" ADD CONSTRAINT "mobilepay_agreement_subscription_id_subscription_id_fk" FOREIGN KEY ("subscription_id") REFERENCES "public"."subscription"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "mobilepay_charge" ADD CONSTRAINT "mobilepay_charge_mobilepay_agreement_id_mobilepay_agreement_id_fk" FOREIGN KEY ("mobilepay_agreement_id") REFERENCES "public"."mobilepay_agreement"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "mobilepay_charge" ADD CONSTRAINT "mobilepay_charge_subscription_id_subscription_id_fk" FOREIGN KEY ("subscription_id") REFERENCES "public"."subscription"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "mobilepay_agreement_agreement_id_index" ON "mobilepay_agreement" USING btree ("agreement_id");--> statement-breakpoint
CREATE UNIQUE INDEX "mobilepay_agreement_state_index" ON "mobilepay_agreement" USING btree ("state");--> statement-breakpoint
CREATE UNIQUE INDEX "mobilepay_agreement_subscription_id_index" ON "mobilepay_agreement" USING btree ("subscription_id");--> statement-breakpoint
CREATE INDEX "mp_charge_agreement_id_idx" ON "mobilepay_charge" USING btree ("agreement_id");--> statement-breakpoint
CREATE INDEX "mp_charge_subscription_id_idx" ON "mobilepay_charge" USING btree ("subscription_id");--> statement-breakpoint
CREATE UNIQUE INDEX "mp_charge_provider_charge_id_uq" ON "mobilepay_charge" USING btree ("provider_charge_id");--> statement-breakpoint