CREATE TYPE "public"."offshore_hull_work_type" AS ENUM('HA', 'HE', 'HH', 'HM', 'NC');--> statement-breakpoint
CREATE TYPE "public"."offshore_top_work_type" AS ENUM('TM', 'TS', 'TE', 'TP');--> statement-breakpoint
CREATE TABLE "item_offshore_hull" (
	"id" serial PRIMARY KEY NOT NULL,
	"item_id" integer NOT NULL,
	"work_type" "offshore_hull_work_type" NOT NULL,
	"item_list1" text,
	"item_list2" text,
	"item_list3" text,
	"item_list4" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "item_offshore_top" (
	"id" serial PRIMARY KEY NOT NULL,
	"item_id" integer NOT NULL,
	"work_type" "offshore_top_work_type" NOT NULL,
	"item_list1" text,
	"item_list2" text,
	"item_list3" text,
	"item_list4" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "vendor_pq_submissions" (
	"id" serial PRIMARY KEY NOT NULL,
	"vendor_id" integer NOT NULL,
	"project_id" integer,
	"type" varchar(20) NOT NULL,
	"status" varchar(20) DEFAULT 'REQUESTED' NOT NULL,
	"submitted_at" timestamp,
	"approved_at" timestamp,
	"rejected_at" timestamp,
	"reject_reason" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
DROP VIEW "public"."project_approved_vendors";--> statement-breakpoint
ALTER TABLE "item_shipbuilding" ADD COLUMN "created_at" timestamp DEFAULT now() NOT NULL;--> statement-breakpoint
ALTER TABLE "item_shipbuilding" ADD COLUMN "updated_at" timestamp DEFAULT now() NOT NULL;--> statement-breakpoint
ALTER TABLE "item_offshore_hull" ADD CONSTRAINT "item_offshore_hull_item_id_items_id_fk" FOREIGN KEY ("item_id") REFERENCES "public"."items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "item_offshore_top" ADD CONSTRAINT "item_offshore_top_item_id_items_id_fk" FOREIGN KEY ("item_id") REFERENCES "public"."items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_pq_submissions" ADD CONSTRAINT "vendor_pq_submissions_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE "vendor_pq_submissions" ADD CONSTRAINT "vendor_pq_submissions_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
CREATE UNIQUE INDEX "unique_pq_submission" ON "vendor_pq_submissions" USING btree ("vendor_id","project_id","type");--> statement-breakpoint
CREATE VIEW "public"."project_approved_vendors" AS (select "vendors"."id", "vendors"."vendor_name", "vendors"."vendor_code", "vendors"."tax_id", "vendors"."email", "vendors"."phone", "vendors"."status", "vendor_types"."name_ko", "vendor_types"."name_en", "projects"."code", "projects"."name", "projects"."type", "vendor_pq_submissions"."submitted_at", "vendor_pq_submissions"."approved_at" from "vendors" inner join "vendor_pq_submissions" on "vendor_pq_submissions"."vendor_id" = "vendors"."id" inner join "projects" on "vendor_pq_submissions"."project_id" = "projects"."id" left join "vendor_types" on "vendors"."vendor_type_id" = "vendor_types"."id" where "vendor_pq_submissions"."status" = 'APPROVED');