CREATE TYPE "public"."domain_resolution_status" AS ENUM('pending', 'resolved', 'mismatch', 'unreachable');--> statement-breakpoint
CREATE TYPE "public"."note_category" AS ENUM('general', 'maintenance', 'incident', 'todo');--> statement-breakpoint
CREATE TYPE "public"."server_environment" AS ENUM('development', 'staging', 'production');--> statement-breakpoint
CREATE TYPE "public"."server_status" AS ENUM('healthy', 'warning', 'critical', 'offline');--> statement-breakpoint
CREATE TYPE "public"."service_status" AS ENUM('running', 'degraded', 'stopped', 'maintenance');--> statement-breakpoint
CREATE TYPE "public"."service_type" AS ENUM('frontend', 'backend', 'database', 'worker', 'proxy', 'monitoring', 'other');--> statement-breakpoint
CREATE TABLE "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 "session" (
	"id" text PRIMARY KEY NOT NULL,
	"expires_at" timestamp NOT NULL,
	"token" text NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp NOT NULL,
	"ip_address" text,
	"user_agent" text,
	"user_id" text NOT NULL,
	CONSTRAINT "session_token_unique" UNIQUE("token")
);
--> statement-breakpoint
CREATE TABLE "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,
	CONSTRAINT "user_email_unique" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE "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 "domain" (
	"id" text PRIMARY KEY NOT NULL,
	"server_id" text NOT NULL,
	"service_id" text,
	"name" text NOT NULL,
	"expected_server_ip" text NOT NULL,
	"last_resolved_ipv4" text,
	"last_resolved_ipv6" text,
	"resolution_status" "domain_resolution_status" DEFAULT 'pending' NOT NULL,
	"last_checked_at" timestamp,
	"remarks" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "note" (
	"id" text PRIMARY KEY NOT NULL,
	"server_id" text,
	"service_id" text,
	"domain_id" text,
	"title" text NOT NULL,
	"content" text NOT NULL,
	"category" "note_category" DEFAULT 'general' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "server" (
	"id" text PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"host" text NOT NULL,
	"ip_address" text NOT NULL,
	"provider" text,
	"region" text,
	"environment" "server_environment" DEFAULT 'production' NOT NULL,
	"status" "server_status" DEFAULT 'healthy' NOT NULL,
	"operating_system" text,
	"remarks" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "service" (
	"id" text PRIMARY KEY NOT NULL,
	"server_id" text NOT NULL,
	"name" text NOT NULL,
	"type" "service_type" DEFAULT 'other' NOT NULL,
	"status" "service_status" DEFAULT 'running' NOT NULL,
	"port" text,
	"version" text,
	"health_check_path" text,
	"remarks" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
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;--> statement-breakpoint
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "domain" ADD CONSTRAINT "domain_server_id_server_id_fk" FOREIGN KEY ("server_id") REFERENCES "public"."server"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "domain" ADD CONSTRAINT "domain_service_id_service_id_fk" FOREIGN KEY ("service_id") REFERENCES "public"."service"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "note" ADD CONSTRAINT "note_server_id_server_id_fk" FOREIGN KEY ("server_id") REFERENCES "public"."server"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "note" ADD CONSTRAINT "note_service_id_service_id_fk" FOREIGN KEY ("service_id") REFERENCES "public"."service"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "note" ADD CONSTRAINT "note_domain_id_domain_id_fk" FOREIGN KEY ("domain_id") REFERENCES "public"."domain"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "service" ADD CONSTRAINT "service_server_id_server_id_fk" FOREIGN KEY ("server_id") REFERENCES "public"."server"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "account_userId_idx" ON "account" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "session_userId_idx" ON "session" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "verification_identifier_idx" ON "verification" USING btree ("identifier");--> statement-breakpoint
CREATE UNIQUE INDEX "domain_name_unique" ON "domain" USING btree ("name");--> statement-breakpoint
CREATE INDEX "domain_serverId_idx" ON "domain" USING btree ("server_id");--> statement-breakpoint
CREATE INDEX "domain_serviceId_idx" ON "domain" USING btree ("service_id");--> statement-breakpoint
CREATE INDEX "domain_resolutionStatus_idx" ON "domain" USING btree ("resolution_status");--> statement-breakpoint
CREATE INDEX "note_serverId_idx" ON "note" USING btree ("server_id");--> statement-breakpoint
CREATE INDEX "note_serviceId_idx" ON "note" USING btree ("service_id");--> statement-breakpoint
CREATE INDEX "note_domainId_idx" ON "note" USING btree ("domain_id");--> statement-breakpoint
CREATE INDEX "note_category_idx" ON "note" USING btree ("category");--> statement-breakpoint
CREATE UNIQUE INDEX "server_host_unique" ON "server" USING btree ("host");--> statement-breakpoint
CREATE UNIQUE INDEX "server_ipAddress_unique" ON "server" USING btree ("ip_address");--> statement-breakpoint
CREATE INDEX "server_environment_idx" ON "server" USING btree ("environment");--> statement-breakpoint
CREATE INDEX "server_status_idx" ON "server" USING btree ("status");--> statement-breakpoint
CREATE INDEX "service_serverId_idx" ON "service" USING btree ("server_id");--> statement-breakpoint
CREATE INDEX "service_status_idx" ON "service" USING btree ("status");--> statement-breakpoint
CREATE UNIQUE INDEX "service_serverId_name_unique" ON "service" USING btree ("server_id","name");