CREATE TABLE "vendor_selections" (
	"id" serial PRIMARY KEY NOT NULL,
	"rfq_id" integer NOT NULL,
	"rfq_code" varchar(50),
	"vendor_id" integer NOT NULL,
	"vendor_name" varchar(255) NOT NULL,
	"vendor_code" varchar(50) NOT NULL,
	"selected_amount" numeric(15, 2) NOT NULL,
	"currency" varchar(10) NOT NULL,
	"selection_reason" text NOT NULL,
	"price_rank" integer,
	"has_condition_differences" boolean DEFAULT false,
	"critical_differences" json,
	"approval_status" varchar(30) DEFAULT '대기',
	"approved_by" integer,
	"approved_at" timestamp with time zone,
	"approval_comment" text,
	"selected_by" integer,
	"selected_at" timestamp with time zone NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "vendor_selections_rfq_id_unique" UNIQUE("rfq_id")
);
--> statement-breakpoint
DROP VIEW "public"."rfq_last_details_view";--> statement-breakpoint
ALTER TABLE "rfq_last_details" RENAME COLUMN "sparepart_escription" TO "sparepart_description";--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "is_selected" boolean DEFAULT false;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selection_date" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selection_reason" text;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selected_by" integer;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "total_amount" numeric(15, 2);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "price_rank" integer;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "technical_score" numeric(5, 2);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "commercial_score" numeric(5, 2);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "total_score" numeric(5, 2);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selection_approval_status" varchar(30);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selection_approved_by" integer;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selection_approved_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "selection_approval_comment" text;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "contract_status" varchar(30);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "contract_created_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "contract_no" varchar(50);--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "created_at" timestamp DEFAULT now() NOT NULL;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD COLUMN "created_by" integer;--> statement-breakpoint
ALTER TABLE "vendor_selections" ADD CONSTRAINT "vendor_selections_rfq_id_rfqs_last_id_fk" FOREIGN KEY ("rfq_id") REFERENCES "public"."rfqs_last"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_selections" ADD CONSTRAINT "vendor_selections_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_selections" ADD CONSTRAINT "vendor_selections_approved_by_users_id_fk" FOREIGN KEY ("approved_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_selections" ADD CONSTRAINT "vendor_selections_selected_by_users_id_fk" FOREIGN KEY ("selected_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "vendor_selections_rfq_id_idx" ON "vendor_selections" USING btree ("rfq_id");--> statement-breakpoint
CREATE INDEX "vendor_selections_vendor_id_idx" ON "vendor_selections" USING btree ("vendor_id");--> statement-breakpoint
CREATE INDEX "vendor_selections_approval_status_idx" ON "vendor_selections" USING btree ("approval_status");--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_selected_by_users_id_fk" FOREIGN KEY ("selected_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_selection_approved_by_users_id_fk" FOREIGN KEY ("selection_approved_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ADD CONSTRAINT "rfq_last_details_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "unique_selected_vendor" ON "rfq_last_details" USING btree ("rfqs_last_id") WHERE "rfq_last_details"."is_selected" = true AND "rfq_last_details"."is_latest" = true;--> statement-breakpoint
CREATE INDEX "idx_is_selected" ON "rfq_last_details" USING btree ("is_selected");--> statement-breakpoint
CREATE INDEX "idx_price_rank" ON "rfq_last_details" USING btree ("price_rank");--> statement-breakpoint
CREATE INDEX "idx_selection_date" ON "rfq_last_details" USING btree ("selection_date");--> statement-breakpoint
CREATE VIEW "public"."vendor_response_summary_view" AS (select "response"."id" as "response_id", "rfq"."id" as "rfq_id", "rfq"."rfq_code" as "rfq_code", "vendor"."id" as "vendor_id", "vendor"."vendor_name" as "vendor_name", "vendor"."vendor_code" as "vendor_code", "response"."response_version" as "response_version", "response"."status" as "status", "response"."is_latest" as "is_latest", "response"."submitted_at" as "submitted_at", "submitted_by_user"."name" as "submitted_by_name", "response"."total_amount" as "total_amount", "response"."currency" as "currency", 
                CASE 
                    WHEN "detail"."delivery_date" = "response"."vendor_delivery_date" THEN true
                    ELSE false
                END
             as "delivery_date_match", 
                CASE 
                    WHEN "detail"."currency" = "response"."vendor_currency" THEN true
                    ELSE false
                END
             as "currency_match", (
                SELECT COUNT(*)
                FROM rfq_last_vendor_quotation_items
                WHERE vendor_response_id = "response"."id"
            ) as "quoted_item_count", (
                SELECT COUNT(*)
                FROM rfq_last_vendor_attachments
                WHERE vendor_response_id = "response"."id"
            ) as "attachment_count", "response"."created_at" as "created_at", "response"."updated_at" as "updated_at" from "rfq_last_vendor_responses" "response" left join "rfqs_last" "rfq" on "response"."rfqs_last_id" = "rfq"."id" left join "rfq_last_details" "detail" on "response"."rfq_last_details_id" = "detail"."id" left join "vendors" "vendor" on "response"."vendor_id" = "vendor"."id" left join "users" "submitted_by_user" on "response"."submitted_by" = "submitted_by_user"."id");--> statement-breakpoint
CREATE VIEW "public"."rfq_last_details_view" AS (select "rfq_details"."id" as "detail_id", "rfqs"."id" as "rfq_id", "rfqs"."rfq_code" as "rfq_code", "rfqs"."rfq_type" as "rfq_type", "rfqs"."rfq_title" as "rfq_title", "rfqs"."project_company" as "project_company", "rfqs"."project_flag" as "project_flag", "rfqs"."project_site" as "project_site", "rfqs"."sm_code" as "sm_code", "rfqs"."pr_number" as "pr_number", "rfqs"."pr_issue_date" as "pr_issue_date", "rfqs"."series" as "series", "projects"."code" as "project_code", "projects"."name" as "project_name", "rfqs"."item_code" as "item_code", "rfqs"."item_name" as "item_name", "vendors"."id" as "vendor_id", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "vendors"."country" as "vendor_country", "rfq_details"."currency" as "currency", "payment_terms"."code" as "payment_terms_code", "payment_terms"."description" as "payment_terms_description", "incoterms"."code" as "incoterms_code", "incoterms"."description" as "incoterms_description", "rfq_details"."incoterms_detail" as "incoterms_detail", "rfq_details"."delivery_date" as "delivery_date", "rfq_details"."contract_duration" as "contract_duration", "rfq_details"."tax_code" as "tax_code", "rfq_details"."place_of_shipping" as "place_of_shipping", "rfq_details"."place_of_destination" as "place_of_destination", "rfq_details"."short_list" as "short_list", "rfq_details"."return_yn" as "return_yn", "rfq_details"."returned_at" as "returned_at", "rfq_details"."project_gtc_yn" as "project_gtc_yn", "rfq_details"."general_gtc_yn" as "general_gtc_yn", "rfq_details"."nda_yn" as "nda_yn", "rfq_details"."agreement_yn" as "agreement_yn", "rfq_details"."gtc_type" as "gtc_type", "rfq_details"."material_price_related_yn" as "material_price_related_yn", "rfq_details"."sparepart_yn" as "sparepart_yn", "rfq_details"."first_yn" as "first_yn", "rfq_details"."remark" as "remark", "rfq_details"."cancel_reason" as "cancel_reason", "rfq_details"."first_description" as "first_description", "rfq_details"."sparepart_description" as "sparepart_description", "rfq_details"."is_latest" as "is_latest", "rfq_details"."send_version" as "send_version", "rfq_details"."email_sent_at" as "email_sent_at", "rfq_details"."email_sent_to" as "email_sent_to", "rfq_details"."email_resent_count" as "email_resent_count", "rfq_details"."last_email_sent_at" as "last_email_sent_at", "rfq_details"."email_status" as "email_status", "rfq_details"."updated_by" as "updated_by", "updated_by_user"."name" as "updated_by_user_name", "rfq_details"."updated_at" as "updated_at", (
                SELECT COUNT(*) 
                FROM rfq_pr_items 
                WHERE rfqs_last_id = "rfqs"."id"
            ) as "pr_items_count", (
                SELECT COUNT(*) 
                FROM rfq_pr_items 
                WHERE rfqs_last_id = "rfqs"."id" 
                AND major_yn = true
            ) as "major_items_count", (
                SELECT COUNT(*) > 0
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "has_quotation", (
                SELECT status
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "quotation_status", (
                SELECT total_amount
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY created_at DESC LIMIT 1
            ) as "quotation_total_price", (
                SELECT response_version
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY response_version DESC LIMIT 1
            ) as "quotation_version", (
                SELECT COUNT(DISTINCT response_version)
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
            ) as "quotation_version_count", (
                SELECT created_at
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY response_version DESC LIMIT 1
            ) as "last_quotation_date", (
                SELECT submitted_at
                FROM rfq_last_vendor_responses
                WHERE rfqs_last_id = "rfqs"."id" AND vendor_id = "rfq_details"."vendors_id"
                ORDER BY response_version DESC LIMIT 1
            ) as "quotation_submitted_at" from "rfq_last_details" "rfq_details" left join "rfqs_last" "rfqs" on "rfq_details"."rfqs_last_id" = "rfqs"."id" left join "projects" on "rfqs"."project_id" = "projects"."id" left join "vendors" on "rfq_details"."vendors_id" = "vendors"."id" left join "payment_terms" on "rfq_details"."payment_terms_code" = "payment_terms"."code" left join "incoterms" on "rfq_details"."incoterms_code" = "incoterms"."code" left join "users" "updated_by_user" on "rfq_details"."updated_by" = "updated_by_user"."id");