ALTER TABLE "rfq_last_tbe_communications" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE "rfq_last_tbe_evaluation_items" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
DROP VIEW "public"."tbe_session_summary_view";--> statement-breakpoint
DROP TABLE "rfq_last_tbe_communications" CASCADE;--> statement-breakpoint
DROP TABLE "rfq_last_tbe_evaluation_items" CASCADE;--> statement-breakpoint
ALTER TABLE "rfq_last_tbe_vendor_documents" RENAME COLUMN "is_response_to" TO "is_response_to_review_id";--> statement-breakpoint
ALTER TABLE "rfq_last_tbe_vendor_documents" DROP CONSTRAINT "rfq_last_tbe_vendor_documents_is_response_to_rfq_last_tbe_communications_id_fk";
--> statement-breakpoint
ALTER TABLE "rfq_last_tbe_vendor_documents" ADD CONSTRAINT "rfq_last_tbe_vendor_documents_is_response_to_review_id_rfq_last_tbe_document_reviews_id_fk" FOREIGN KEY ("is_response_to_review_id") REFERENCES "public"."rfq_last_tbe_document_reviews"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "rfq_last_tbe_sessions" DROP COLUMN "evaluation_score";--> statement-breakpoint
ALTER TABLE "rfq_last_tbe_sessions" DROP COLUMN "evaluation_grade";--> statement-breakpoint
CREATE VIEW "public"."tbe_session_summary_view" AS (select "tbe"."id" as "tbe_id", "tbe"."session_code" as "session_code", "tbe"."session_title" as "session_title", "rfq"."rfq_code" as "rfq_code", "vendor"."vendor_name" as "vendor_name", "vendor"."vendor_code" as "vendor_code", "tbe"."status" as "status", "tbe"."evaluation_result" as "evaluation_result", "lead_evaluator"."name" as "lead_evaluator_name", (
                SELECT COUNT(*)
                FROM rfq_last_tbe_document_reviews
                WHERE tbe_session_id = "tbe"."id"
            ) as "total_documents", (
                SELECT COUNT(*)
                FROM rfq_last_tbe_document_reviews
                WHERE tbe_session_id = "tbe"."id"
                AND review_status IN ('검토완료', '승인')
            ) as "reviewed_documents", (
                SELECT COUNT(*)
                FROM rfq_last_tbe_pdftron_comments pc
                JOIN rfq_last_tbe_document_reviews dr ON pc.document_review_id = dr.id
                WHERE dr.tbe_session_id = "tbe"."id"
            ) as "total_comments", (
                SELECT COUNT(*)
                FROM rfq_last_tbe_pdftron_comments pc
                JOIN rfq_last_tbe_document_reviews dr ON pc.document_review_id = dr.id
                WHERE dr.tbe_session_id = "tbe"."id"
                AND pc.status = 'open'
            ) as "unresolved_comments", "tbe"."actual_start_date" as "actual_start_date", "tbe"."actual_end_date" as "actual_end_date", "tbe"."created_at" as "created_at", "tbe"."updated_at" as "updated_at" from "rfq_last_tbe_sessions" "tbe" left join "rfqs_last" "rfq" on "tbe"."rfqs_last_id" = "rfq"."id" left join "vendors" "vendor" on "tbe"."vendor_id" = "vendor"."id" left join "users" "lead_evaluator" on "tbe"."lead_evaluator_id" = "lead_evaluator"."id");