CREATE TYPE "public"."portal_role" AS ENUM('ADMIN', 'EDITORIAL');--> statement-breakpoint CREATE TYPE "public"."report_status" AS ENUM('draft', 'final', 'cancelled');--> statement-breakpoint CREATE TYPE "public"."account_status" AS ENUM('pending_email', 'pending_approval', 'active', 'suspended', 'rejected');--> statement-breakpoint CREATE TYPE "public"."download_status" AS ENUM('authorized', 'streaming', 'completed', 'failed_before_delivery', 'interrupted');--> statement-breakpoint CREATE TYPE "public"."usage_billing_status" AS ENUM('open', 'billed', 'reopened', 'voided');--> statement-breakpoint CREATE TABLE "album_grants" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "editorial_account_id" uuid NOT NULL, "immich_album_id" text NOT NULL, "granted_at" timestamp with time zone DEFAULT now() NOT NULL, "revoked_at" timestamp with time zone, "granted_by_user_id" uuid, CONSTRAINT "album_grants_immich_album_id_uuid_check" CHECK ("album_grants"."immich_album_id" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') ); --> statement-breakpoint CREATE TABLE "audit_events" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "event_type" text NOT NULL, "actor_user_id" uuid, "actor_type" text DEFAULT 'system' NOT NULL, "entity_type" text NOT NULL, "entity_id" text NOT NULL, "previous_values" jsonb, "new_values" jsonb, "occurred_at" timestamp with time zone DEFAULT now() NOT NULL, "request_id" uuid ); --> statement-breakpoint CREATE TABLE "account" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "account_id" text NOT NULL, "provider_id" text NOT NULL, "user_id" uuid NOT NULL, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp with time zone, "refresh_token_expires_at" timestamp with time zone, "scope" text, "password" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "session" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "expires_at" timestamp with time zone NOT NULL, "token" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "ip_address" text, "user_agent" text, "user_id" uuid NOT NULL, CONSTRAINT "session_token_unique" UNIQUE("token") ); --> statement-breakpoint CREATE TABLE "two_factor" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "secret" text NOT NULL, "backup_codes" text, "verified" boolean DEFAULT false NOT NULL, "failed_verification_count" integer DEFAULT 0 NOT NULL, "locked_until" timestamp with time zone, CONSTRAINT "two_factor_user_id_unique" UNIQUE("user_id") ); --> statement-breakpoint CREATE TABLE "user" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "email" text NOT NULL, "email_verified" boolean DEFAULT false NOT NULL, "image" text, "role" "portal_role" DEFAULT 'EDITORIAL' NOT NULL, "two_factor_enabled" boolean DEFAULT false NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "user_email_unique" UNIQUE("email") ); --> statement-breakpoint CREATE TABLE "verification" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "identifier" text NOT NULL, "value" text NOT NULL, "expires_at" timestamp with time zone 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 "album_auto_rules" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "editorial_account_id" uuid NOT NULL, "keyword" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "created_by_user_id" uuid ); --> statement-breakpoint CREATE TABLE "billing_report_items" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "billing_report_id" uuid NOT NULL, "usage_id" uuid NOT NULL, "immich_asset_id_snapshot" text NOT NULL, "filename_snapshot" text NOT NULL, "album_name_snapshot" text NOT NULL, "purpose_snapshot" text NOT NULL, "internal_reference_snapshot" text, "price_cents" integer NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "billing_reports" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "report_number" integer NOT NULL, "editorial_account_id" uuid NOT NULL, "period_starts_at" timestamp with time zone NOT NULL, "period_ends_at" timestamp with time zone NOT NULL, "billing_address_snapshot" jsonb NOT NULL, "invoice_number" text, "net_total_cents" integer NOT NULL, "flatrate_cents_snapshot" integer, "status" "report_status" DEFAULT 'draft' NOT NULL, "pdf" "bytea", "pdf_sha256" varchar(64), "created_by_user_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "finalized_at" timestamp with time zone, "cancelled_at" timestamp with time zone, "cancellation_reason" text, "cancelled_by_user_id" uuid, CONSTRAINT "billing_reports_pdf_sha256_hex_check" CHECK ("billing_reports"."pdf_sha256" ~ '^[0-9a-f]{64}$'), CONSTRAINT "billing_reports_final_pdf_check" CHECK ("billing_reports"."status" <> 'final' OR ("billing_reports"."pdf" IS NOT NULL AND "billing_reports"."pdf_sha256" IS NOT NULL)) ); --> statement-breakpoint CREATE TABLE "billing_address_versions" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "editorial_account_id" uuid NOT NULL, "company_name" text NOT NULL, "contact_name" text NOT NULL, "address_line_1" text NOT NULL, "address_line_2" text, "postal_code" text NOT NULL, "city" text NOT NULL, "country_code" text NOT NULL, "valid_from" timestamp with time zone DEFAULT now() NOT NULL, "superseded_by_id" uuid, "created_by_user_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "editorial_accounts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "name" text NOT NULL, "contact_name" text NOT NULL, "login_email" text NOT NULL, "status" "account_status" DEFAULT 'pending_email' NOT NULL, "current_price_cents" integer, "flatrate_active" boolean DEFAULT false NOT NULL, "flatrate_price_cents" integer, "flatrate_asset_limit" integer, "approval_notice_status" text DEFAULT 'not_requested' NOT NULL, "current_billing_address_version_id" uuid, "email_verified_at" timestamp with time zone, "accepted_terms_version" text, "accepted_terms_at" timestamp with time zone, "privacy_acknowledged_at" timestamp with time zone, "archived_at" timestamp with time zone, "archived_by_user_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "editorial_accounts_user_id_unique" UNIQUE("user_id"), CONSTRAINT "editorial_accounts_login_email_unique" UNIQUE("login_email") ); --> statement-breakpoint CREATE TABLE "lightbox_items" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "editorial_account_id" uuid NOT NULL, "asset_id" text NOT NULL, "added_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "rate_limits" ( "key" text NOT NULL, "window_boundary" integer NOT NULL, "count" integer DEFAULT 1 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "rate_limits_key_window_boundary_pk" PRIMARY KEY("key","window_boundary") ); --> statement-breakpoint CREATE TABLE "download_events" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "editorial_account_id" uuid NOT NULL, "usage_id" uuid NOT NULL, "immich_asset_id" text NOT NULL, "authorized_at" timestamp with time zone DEFAULT now() NOT NULL, "started_at" timestamp with time zone, "completed_at" timestamp with time zone, "status" "download_status" DEFAULT 'authorized' NOT NULL, "transferred_bytes" bigint, "repeat_download_note" text, "request_id" uuid NOT NULL, "idempotency_key" text NOT NULL, "intent_kind" text DEFAULT 'new_usage' NOT NULL, "request_fingerprint" varchar(64) NOT NULL, "legacy_intent" boolean DEFAULT false NOT NULL, CONSTRAINT "download_events_intent_kind_check" CHECK ("download_events"."intent_kind" in ('new_usage', 'repeat_download', 'legacy')), CONSTRAINT "download_events_request_fingerprint_hex_check" CHECK ("download_events"."request_fingerprint" ~ '^[0-9a-f]{64}$'), CONSTRAINT "download_events_canonical_idempotency_key_check" CHECK ("download_events"."legacy_intent" OR "download_events"."idempotency_key" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'), CONSTRAINT "download_events_canonical_immich_asset_id_check" CHECK ("download_events"."immich_asset_id" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') ); --> statement-breakpoint CREATE TABLE "usages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "editorial_account_id" uuid NOT NULL, "immich_asset_id" text NOT NULL, "filename_snapshot" text NOT NULL, "immich_album_id_snapshot" text NOT NULL, "album_name_snapshot" text NOT NULL, "purpose" text NOT NULL, "internal_reference" text, "price_cents" integer NOT NULL, "billing_status" "usage_billing_status" DEFAULT 'open' NOT NULL, "billing_report_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "usages_canonical_immich_asset_id_check" CHECK ("usages"."immich_asset_id" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'), CONSTRAINT "usages_canonical_immich_album_id_check" CHECK ("usages"."immich_album_id_snapshot" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') ); --> statement-breakpoint ALTER TABLE "album_grants" ADD CONSTRAINT "album_grants_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "album_grants" ADD CONSTRAINT "album_grants_granted_by_user_id_user_id_fk" FOREIGN KEY ("granted_by_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "audit_events" ADD CONSTRAINT "audit_events_actor_user_id_user_id_fk" FOREIGN KEY ("actor_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "two_factor" ADD CONSTRAINT "two_factor_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "album_auto_rules" ADD CONSTRAINT "album_auto_rules_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "album_auto_rules" ADD CONSTRAINT "album_auto_rules_created_by_user_id_user_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_report_items" ADD CONSTRAINT "billing_report_items_billing_report_id_billing_reports_id_fk" FOREIGN KEY ("billing_report_id") REFERENCES "public"."billing_reports"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_report_items" ADD CONSTRAINT "billing_report_items_usage_id_usages_id_fk" FOREIGN KEY ("usage_id") REFERENCES "public"."usages"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_reports" ADD CONSTRAINT "billing_reports_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_reports" ADD CONSTRAINT "billing_reports_created_by_user_id_user_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_reports" ADD CONSTRAINT "billing_reports_cancelled_by_user_id_user_id_fk" FOREIGN KEY ("cancelled_by_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_address_versions" ADD CONSTRAINT "billing_address_versions_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_address_versions" ADD CONSTRAINT "billing_address_versions_superseded_by_id_billing_address_versions_id_fk" FOREIGN KEY ("superseded_by_id") REFERENCES "public"."billing_address_versions"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "billing_address_versions" ADD CONSTRAINT "billing_address_versions_created_by_user_id_user_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "editorial_accounts" ADD CONSTRAINT "editorial_accounts_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "editorial_accounts" ADD CONSTRAINT "editorial_accounts_current_billing_address_version_id_billing_address_versions_id_fk" FOREIGN KEY ("current_billing_address_version_id") REFERENCES "public"."billing_address_versions"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "editorial_accounts" ADD CONSTRAINT "editorial_accounts_archived_by_user_id_user_id_fk" FOREIGN KEY ("archived_by_user_id") REFERENCES "public"."user"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lightbox_items" ADD CONSTRAINT "lightbox_items_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "download_events" ADD CONSTRAINT "download_events_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "download_events" ADD CONSTRAINT "download_events_usage_id_usages_id_fk" FOREIGN KEY ("usage_id") REFERENCES "public"."usages"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "usages" ADD CONSTRAINT "usages_editorial_account_id_editorial_accounts_id_fk" FOREIGN KEY ("editorial_account_id") REFERENCES "public"."editorial_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "usages" ADD CONSTRAINT "usages_billing_report_id_billing_reports_id_fk" FOREIGN KEY ("billing_report_id") REFERENCES "public"."billing_reports"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint CREATE INDEX "album_grants_editorial_account_id_idx" ON "album_grants" USING btree ("editorial_account_id");--> statement-breakpoint CREATE INDEX "album_grants_immich_album_id_idx" ON "album_grants" USING btree ("immich_album_id");--> statement-breakpoint CREATE UNIQUE INDEX "album_grants_editorial_active_album_unique" ON "album_grants" USING btree ("editorial_account_id",lower("immich_album_id")) WHERE "album_grants"."revoked_at" is null;--> statement-breakpoint CREATE UNIQUE INDEX "album_auto_rules_editorial_account_id_keyword_idx" ON "album_auto_rules" USING btree ("editorial_account_id",lower("keyword"));--> statement-breakpoint CREATE UNIQUE INDEX "billing_report_items_report_usage_unique" ON "billing_report_items" USING btree ("billing_report_id","usage_id");--> statement-breakpoint CREATE UNIQUE INDEX "billing_reports_report_number_unique" ON "billing_reports" USING btree ("report_number");--> statement-breakpoint CREATE UNIQUE INDEX "billing_reports_invoice_number_unique" ON "billing_reports" USING btree ("invoice_number");--> statement-breakpoint CREATE UNIQUE INDEX "lightbox_items_editorial_asset_idx" ON "lightbox_items" USING btree ("editorial_account_id","asset_id");--> statement-breakpoint CREATE INDEX "download_events_editorial_authorized_id_idx" ON "download_events" USING btree ("editorial_account_id","authorized_at" DESC NULLS LAST,"id" DESC NULLS LAST);--> statement-breakpoint CREATE UNIQUE INDEX "download_events_editorial_idempotency_key_unique" ON "download_events" USING btree ("editorial_account_id","idempotency_key");--> statement-breakpoint CREATE INDEX "usages_editorial_created_id_idx" ON "usages" USING btree ("editorial_account_id","created_at" DESC NULLS LAST,"id" DESC NULLS LAST);--> statement-breakpoint CREATE INDEX "usages_created_at_idx" ON "usages" USING btree ("created_at");