CREATE TABLE "esg_answer_options" (
	"id" serial PRIMARY KEY NOT NULL,
	"esg_evaluation_item_id" integer NOT NULL,
	"answer_text" text NOT NULL,
	"score" numeric(5, 2) NOT NULL,
	"order_index" integer DEFAULT 0 NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "esg_evaluation_items" (
	"id" serial PRIMARY KEY NOT NULL,
	"esg_evaluation_id" integer NOT NULL,
	"evaluation_item" text NOT NULL,
	"order_index" integer DEFAULT 0 NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "esg_evaluations" (
	"id" serial PRIMARY KEY NOT NULL,
	"serial_number" varchar(50) NOT NULL,
	"category" varchar(100) NOT NULL,
	"inspection_item" text NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "esg_evaluations_serial_number_unique" UNIQUE("serial_number")
);
--> statement-breakpoint
CREATE TABLE "general_evaluations" (
	"id" serial PRIMARY KEY NOT NULL,
	"serial_number" varchar(50) NOT NULL,
	"category" varchar(100) NOT NULL,
	"inspection_item" text NOT NULL,
	"remarks" text,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "general_evaluations_serial_number_unique" UNIQUE("serial_number")
);
--> statement-breakpoint
ALTER TABLE "esg_answer_options" ADD CONSTRAINT "esg_answer_options_esg_evaluation_item_id_esg_evaluation_items_id_fk" FOREIGN KEY ("esg_evaluation_item_id") REFERENCES "public"."esg_evaluation_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "esg_evaluation_items" ADD CONSTRAINT "esg_evaluation_items_esg_evaluation_id_esg_evaluations_id_fk" FOREIGN KEY ("esg_evaluation_id") REFERENCES "public"."esg_evaluations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE VIEW "public"."esg_evaluations_view" AS (select "esg_evaluations"."id", "esg_evaluations"."serial_number", "esg_evaluations"."category", "esg_evaluations"."inspection_item", "esg_evaluations"."is_active", "esg_evaluations"."created_at", "esg_evaluations"."updated_at", count(distinct "esg_evaluation_items"."id") as "total_evaluation_items", count("esg_answer_options"."id") as "total_answer_options", coalesce(sum("esg_answer_options"."score"), 0) as "max_possible_score" from "esg_evaluations" left join "esg_evaluation_items" on "esg_evaluations"."id" = "esg_evaluation_items"."esg_evaluation_id" left join "esg_answer_options" on "esg_evaluation_items"."id" = "esg_answer_options"."esg_evaluation_item_id" group by "esg_evaluations"."id", "esg_evaluations"."serial_number", "esg_evaluations"."category", "esg_evaluations"."inspection_item", "esg_evaluations"."is_active", "esg_evaluations"."created_at", "esg_evaluations"."updated_at");