CREATE TABLE IF NOT EXISTS "account" (
	"id" text PRIMARY KEY NOT NULL,
	"accountId" text NOT NULL,
	"providerId" text NOT NULL,
	"userId" text NOT NULL,
	"accessToken" text,
	"refreshToken" text,
	"idToken" text,
	"accessTokenExpiresAt" timestamp with time zone,
	"refreshTokenExpiresAt" timestamp with time zone,
	"scope" text,
	"password" text,
	"createdAt" timestamp with time zone NOT NULL,
	"updatedAt" timestamp with time zone NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "invitation" (
	"id" text PRIMARY KEY NOT NULL,
	"organizationId" text NOT NULL,
	"email" text NOT NULL,
	"role" text,
	"status" text DEFAULT 'pending' NOT NULL,
	"expiresAt" timestamp with time zone NOT NULL,
	"createdAt" timestamp with time zone NOT NULL,
	"inviterId" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "member" (
	"id" text PRIMARY KEY NOT NULL,
	"organizationId" text NOT NULL,
	"userId" text NOT NULL,
	"role" text DEFAULT 'member' NOT NULL,
	"createdAt" timestamp with time zone NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "organization" (
	"id" text PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"slug" text NOT NULL,
	"logo" text,
	"createdAt" timestamp with time zone NOT NULL,
	"metadata" text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "session" (
	"id" text PRIMARY KEY NOT NULL,
	"expiresAt" timestamp with time zone NOT NULL,
	"token" text NOT NULL,
	"createdAt" timestamp with time zone NOT NULL,
	"updatedAt" timestamp with time zone NOT NULL,
	"ipAddress" text,
	"userAgent" text,
	"userId" text NOT NULL,
	"impersonatedBy" text,
	"activeOrganizationId" text
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
	"id" text PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"email" text NOT NULL,
	"emailVerified" boolean DEFAULT false NOT NULL,
	"image" text,
	"createdAt" timestamp with time zone NOT NULL,
	"updatedAt" timestamp with time zone NOT NULL,
	"role" text,
	"banned" boolean DEFAULT false,
	"banReason" text,
	"banExpires" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "verification" (
	"id" text PRIMARY KEY NOT NULL,
	"identifier" text NOT NULL,
	"value" text NOT NULL,
	"expiresAt" timestamp with time zone NOT NULL,
	"createdAt" timestamp with time zone NOT NULL,
	"updatedAt" timestamp with time zone NOT NULL
);
--> statement-breakpoint
DO $$ 
DECLARE
	m record;
BEGIN
	FOR m IN (
		SELECT * FROM (
			VALUES
				('user', 'email_verified', 'emailVerified'),
				('user', 'emailverified', 'emailVerified'),
				('user', 'created_at', 'createdAt'),
				('user', 'createdat', 'createdAt'),
				('user', 'updated_at', 'updatedAt'),
				('user', 'updatedat', 'updatedAt'),
				('user', 'ban_reason', 'banReason'),
				('user', 'banreason', 'banReason'),
				('user', 'ban_expires', 'banExpires'),
				('user', 'banexpires', 'banExpires'),
				('organization', 'created_at', 'createdAt'),
				('organization', 'createdat', 'createdAt'),
				('session', 'expires_at', 'expiresAt'),
				('session', 'expiresat', 'expiresAt'),
				('session', 'created_at', 'createdAt'),
				('session', 'createdat', 'createdAt'),
				('session', 'updated_at', 'updatedAt'),
				('session', 'updatedat', 'updatedAt'),
				('session', 'ip_address', 'ipAddress'),
				('session', 'ipaddress', 'ipAddress'),
				('session', 'user_agent', 'userAgent'),
				('session', 'useragent', 'userAgent'),
				('session', 'user_id', 'userId'),
				('session', 'userid', 'userId'),
				('session', 'impersonated_by', 'impersonatedBy'),
				('session', 'impersonatedby', 'impersonatedBy'),
				('session', 'active_organization_id', 'activeOrganizationId'),
				('session', 'activeorganizationid', 'activeOrganizationId'),
				('account', 'account_id', 'accountId'),
				('account', 'accountid', 'accountId'),
				('account', 'provider_id', 'providerId'),
				('account', 'providerid', 'providerId'),
				('account', 'user_id', 'userId'),
				('account', 'userid', 'userId'),
				('account', 'access_token', 'accessToken'),
				('account', 'accesstoken', 'accessToken'),
				('account', 'refresh_token', 'refreshToken'),
				('account', 'refreshtoken', 'refreshToken'),
				('account', 'id_token', 'idToken'),
				('account', 'idtoken', 'idToken'),
				('account', 'access_token_expires_at', 'accessTokenExpiresAt'),
				('account', 'accesstokenexpiresat', 'accessTokenExpiresAt'),
				('account', 'refresh_token_expires_at', 'refreshTokenExpiresAt'),
				('account', 'refreshtokenexpiresat', 'refreshTokenExpiresAt'),
				('account', 'created_at', 'createdAt'),
				('account', 'createdat', 'createdAt'),
				('account', 'updated_at', 'updatedAt'),
				('account', 'updatedat', 'updatedAt'),
				('verification', 'expires_at', 'expiresAt'),
				('verification', 'expiresat', 'expiresAt'),
				('verification', 'created_at', 'createdAt'),
				('verification', 'createdat', 'createdAt'),
				('verification', 'updated_at', 'updatedAt'),
				('verification', 'updatedat', 'updatedAt'),
				('member', 'organization_id', 'organizationId'),
				('member', 'organizationid', 'organizationId'),
				('member', 'user_id', 'userId'),
				('member', 'userid', 'userId'),
				('member', 'created_at', 'createdAt'),
				('member', 'createdat', 'createdAt'),
				('invitation', 'organization_id', 'organizationId'),
				('invitation', 'organizationid', 'organizationId'),
				('invitation', 'expires_at', 'expiresAt'),
				('invitation', 'expiresat', 'expiresAt'),
				('invitation', 'created_at', 'createdAt'),
				('invitation', 'createdat', 'createdAt'),
				('invitation', 'inviter_id', 'inviterId'),
				('invitation', 'inviterid', 'inviterId')
		) AS rename_map(table_name, old_column, new_column)
	)
	LOOP
		IF EXISTS (
			SELECT 1
			FROM information_schema.columns
			WHERE table_schema = 'public'
				AND table_name = m.table_name
				AND column_name = m.old_column
		)
		AND NOT EXISTS (
			SELECT 1
			FROM information_schema.columns
			WHERE table_schema = 'public'
				AND table_name = m.table_name
				AND column_name = m.new_column
		) THEN
			EXECUTE format(
				'ALTER TABLE %I.%I RENAME COLUMN %I TO %I',
				'public',
				m.table_name,
				m.old_column,
				m.new_column
			);
		END IF;
	END LOOP;
END $$;
--> statement-breakpoint
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'account_userId_user_id_fk') THEN ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; END IF; END $$;--> statement-breakpoint
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'invitation_organizationId_organization_id_fk') THEN ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action; END IF; END $$;--> statement-breakpoint
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'invitation_inviterId_user_id_fk') THEN ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviterId_user_id_fk" FOREIGN KEY ("inviterId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; END IF; END $$;--> statement-breakpoint
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'member_organizationId_organization_id_fk') THEN ALTER TABLE "member" ADD CONSTRAINT "member_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action; END IF; END $$;--> statement-breakpoint
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'member_userId_user_id_fk') THEN ALTER TABLE "member" ADD CONSTRAINT "member_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; END IF; END $$;--> statement-breakpoint
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'session_userId_user_id_fk') THEN ALTER TABLE "session" ADD CONSTRAINT "session_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; END IF; END $$;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "account_userId_idx" ON "account" USING btree ("userId");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "invitation_organizationId_idx" ON "invitation" USING btree ("organizationId");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "invitation_email_idx" ON "invitation" USING btree ("email");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "member_organizationId_idx" ON "member" USING btree ("organizationId");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "member_userId_idx" ON "member" USING btree ("userId");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "organization_slug_uidx" ON "organization" USING btree ("slug");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "session_token_uidx" ON "session" USING btree ("token");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "session_userId_idx" ON "session" USING btree ("userId");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "user_email_uidx" ON "user" USING btree ("email");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "verification_identifier_idx" ON "verification" USING btree ("identifier");
