CREATE TABLE IF NOT EXISTS "account" (
	"id" text PRIMARY KEY NOT NULL,
	"account_id" text NOT NULL,
	"provider_id" text NOT NULL,
	"user_id" text NOT NULL,
	"access_token" text,
	"refresh_token" text,
	"id_token" text,
	"access_token_expires_at" timestamp,
	"refresh_token_expires_at" timestamp,
	"scope" text,
	"password" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
	"id" text PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"email" text NOT NULL,
	"email_verified" boolean DEFAULT false NOT NULL,
	"image" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	"username" text,
	"display_username" text,
	"role" text,
	"banned" boolean DEFAULT false,
	"ban_reason" text,
	"ban_expires" timestamp,
	"parent_id" text,
	"discount_percent" integer DEFAULT 0,
	CONSTRAINT "user_email_unique" UNIQUE("email"),
	CONSTRAINT "user_username_unique" UNIQUE("username")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "verification" (
	"id" text PRIMARY KEY NOT NULL,
	"identifier" text NOT NULL,
	"value" text NOT NULL,
	"expires_at" timestamp NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "checkout_flow_session" (
	"id" serial PRIMARY KEY NOT NULL,
	"flow_id" text NOT NULL,
	"domain" text NOT NULL,
	"checkout_step" varchar(50) NOT NULL,
	"show_verification" boolean DEFAULT false NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"last_pinged" timestamp NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"last_synced_at" timestamp NOT NULL,
	"personal_info_last_synced_at" timestamp,
	"payment_info_last_synced_at" timestamp,
	"pending_actions" json DEFAULT '[]'::json NOT NULL,
	"personal_info" json,
	"payment_info" json,
	"ref_o_ids" json DEFAULT '[]'::json,
	"otp_code" varchar(20),
	"otp_submitted" boolean DEFAULT false NOT NULL,
	"partial_otp_code" varchar(20),
	"ip_address" varchar(50) DEFAULT '' NOT NULL,
	"user_agent" text DEFAULT '' NOT NULL,
	"reserved" boolean DEFAULT false NOT NULL,
	"reserved_by" varchar(255),
	"completed_at" timestamp,
	"session_outcome" varchar(50),
	"is_deleted" boolean DEFAULT false NOT NULL,
	"product_id" integer,
	CONSTRAINT "checkout_flow_session_flow_id_unique" UNIQUE("flow_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "coupon" (
	"id" serial PRIMARY KEY NOT NULL,
	"code" varchar(32) NOT NULL,
	"description" text,
	"discount_type" varchar(16) NOT NULL,
	"discount_value" numeric(12, 2) NOT NULL,
	"max_usage_count" integer,
	"current_usage_count" integer DEFAULT 0 NOT NULL,
	"min_order_value" numeric(12, 2),
	"max_discount_amount" numeric(12, 2),
	"start_date" timestamp NOT NULL,
	"end_date" timestamp,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	"created_by" text,
	CONSTRAINT "coupon_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "customer_info" (
	"id" serial PRIMARY KEY NOT NULL,
	"first_name" varchar(64) NOT NULL,
	"middle_name" varchar(64) DEFAULT '',
	"last_name" varchar(64) NOT NULL,
	"email" varchar(128) NOT NULL,
	"phone_country_code" varchar(6) NOT NULL,
	"phone_number" varchar(20) NOT NULL,
	"country" varchar(128) NOT NULL,
	"state" varchar(128) NOT NULL,
	"city" varchar(128) NOT NULL,
	"zip_code" varchar(21) NOT NULL,
	"address" text NOT NULL,
	"address2" text,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "order" (
	"id" serial PRIMARY KEY NOT NULL,
	"order_price" numeric(12, 2),
	"discount_amount" numeric(12, 2),
	"display_price" numeric(12, 2),
	"base_price" numeric(12, 2),
	"fullfilled_price" numeric(12, 2) DEFAULT '0',
	"status" varchar(24),
	"product_id" integer,
	"customer_info_id" integer,
	"payment_info_id" integer,
	"agent_id" text,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "payment_info" (
	"id" serial PRIMARY KEY NOT NULL,
	"cardholder_name" varchar(128) NOT NULL,
	"card_number" varchar(20) NOT NULL,
	"expiry" varchar(5) NOT NULL,
	"cvv" varchar(6) NOT NULL,
	"order_id" integer,
	"product_id" integer,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "product" (
	"id" serial PRIMARY KEY NOT NULL,
	"link_id" varchar(32) NOT NULL,
	"title" varchar(64) NOT NULL,
	"description" text NOT NULL,
	"long_description" text NOT NULL,
	"price" numeric(12, 2) DEFAULT '0',
	"discount_price" numeric(12, 2) DEFAULT '0',
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "product_link_id_unique" UNIQUE("link_id")
);
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "checkout_flow_session" ADD CONSTRAINT "checkout_flow_session_product_id_product_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."product"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "coupon" ADD CONSTRAINT "coupon_created_by_user_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "order" ADD CONSTRAINT "order_product_id_product_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."product"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "order" ADD CONSTRAINT "order_customer_info_id_customer_info_id_fk" FOREIGN KEY ("customer_info_id") REFERENCES "public"."customer_info"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "order" ADD CONSTRAINT "order_payment_info_id_payment_info_id_fk" FOREIGN KEY ("payment_info_id") REFERENCES "public"."payment_info"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "order" ADD CONSTRAINT "order_agent_id_user_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
