CREATE TYPE "public"."product_type" AS ENUM('credit', 'subscription', 'other');--> statement-breakpoint
CREATE TABLE "identities" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" text NOT NULL,
	"provider" text NOT NULL,
	"provider_user_id" text NOT NULL,
	"provider_data" jsonb DEFAULT '{}'::jsonb,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
ALTER TABLE "credit_packages" RENAME TO "products";--> statement-breakpoint
ALTER TABLE "sys_notifications" RENAME TO "notifications";--> statement-breakpoint
ALTER TABLE "user_devices" RENAME TO "devices";--> statement-breakpoint
ALTER TABLE "user_orders" RENAME TO "orders";--> statement-breakpoint
ALTER TABLE "user_payments" RENAME TO "payments";--> statement-breakpoint
ALTER TABLE "user_profiles" RENAME TO "users";--> statement-breakpoint
ALTER TABLE "orders" RENAME COLUMN "credit_package_id" TO "product_id";--> statement-breakpoint
ALTER TABLE "users" RENAME COLUMN "user_id" TO "logto_id";--> statement-breakpoint
ALTER TABLE "users" RENAME COLUMN "email" TO "primary_email";--> statement-breakpoint
ALTER TABLE "payment_provider_transactions" DROP CONSTRAINT "payment_provider_transactions_payment_id_user_payments_id_fk";
--> statement-breakpoint
ALTER TABLE "orders" DROP CONSTRAINT "user_orders_credit_package_id_credit_packages_id_fk";
--> statement-breakpoint
ALTER TABLE "payments" DROP CONSTRAINT "user_payments_order_id_user_orders_id_fk";
--> statement-breakpoint
ALTER TABLE "products" ADD COLUMN "type" "product_type" DEFAULT 'credit' NOT NULL;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "username" text;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "name" text;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "primary_phone" text;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "avatar" text;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "custom_data" jsonb DEFAULT '{}'::jsonb;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "last_sign_in_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "is_suspended" boolean DEFAULT false;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "has_password" boolean DEFAULT false;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "updated_at" timestamp with time zone DEFAULT now();--> statement-breakpoint
ALTER TABLE "payment_provider_transactions" ADD CONSTRAINT "payment_provider_transactions_payment_id_payments_id_fk" FOREIGN KEY ("payment_id") REFERENCES "public"."payments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "orders" ADD CONSTRAINT "orders_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "payments" ADD CONSTRAINT "payments_order_id_orders_id_fk" FOREIGN KEY ("order_id") REFERENCES "public"."orders"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_logto_id_unique" UNIQUE("logto_id");