DROP VIEW "public"."vendors_with_types";--> statement-breakpoint
DROP INDEX "unique_contract_doc_status";--> statement-breakpoint
DROP INDEX "unique_contract_vendor_doc";--> statement-breakpoint
DROP INDEX "unique_project_doc_status";--> statement-breakpoint
ALTER TABLE "rfq_last_details" ALTER COLUMN "delivery_date" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "rfq_last_details" ALTER COLUMN "tax_code" DROP DEFAULT;--> statement-breakpoint
ALTER TABLE "rfq_last_vendor_responses" ALTER COLUMN "status" SET DEFAULT '초대됨';--> statement-breakpoint
ALTER TABLE "vendors" ADD COLUMN "is_association_member" varchar(1);--> statement-breakpoint
ALTER TABLE "rfqs_last" ADD COLUMN "ANFNR" varchar(50);--> statement-breakpoint
CREATE UNIQUE INDEX "unique_contract_doc_status" ON "documents" USING btree ("contract_id","doc_number","discipline","status");--> statement-breakpoint
CREATE UNIQUE INDEX "unique_contract_vendor_doc" ON "documents" USING btree ("contract_id","discipline","vendor_doc_number") WHERE "documents"."vendor_doc_number" IS NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX "unique_project_doc_status" ON "documents" USING btree ("project_id","doc_number","discipline","status");--> statement-breakpoint
ALTER TABLE "rfqs_last" ADD CONSTRAINT "rfqs_last_ANFNR_unique" UNIQUE("ANFNR");--> statement-breakpoint
CREATE VIEW "public"."vendors_with_types_and_materials" AS (select "vendors"."id" as "id", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "vendors"."tax_id" as "tax_id", "vendors"."address" as "address", "vendors"."address_detail" as "address_detail", "vendors"."postal_code" as "postal_code", "vendors"."country" as "country", "vendors"."phone" as "phone", "vendors"."email" as "email", "vendors"."business_size" as "business_size", "vendors"."website" as "website", "vendors"."status" as "status", "vendors"."vendor_type_id" as "vendor_type_id", "vendors"."representative_name" as "representative_name", "vendors"."representative_birth" as "representative_birth", "vendors"."representative_email" as "representative_email", "vendors"."representative_phone" as "representative_phone", "vendors"."corporate_registration_number" as "corporate_registration_number", "vendors"."items" as "items", "vendors"."credit_agency" as "credit_agency", "vendors"."credit_rating" as "credit_rating", "vendors"."cash_flow_rating" as "cash_flow_rating", "vendors"."is_association_member" as "is_association_member", "vendors"."created_at" as "created_at", "vendors"."updated_at" as "updated_at", "vendor_types"."name_ko" as "vendor_type_name", "vendor_types"."name_en" as "vendor_type_name_en", "vendor_types"."code" as "vendor_type_code", 
        CASE
          WHEN "vendors"."status" = 'ACTIVE' THEN '정규업체'
          WHEN "vendors"."status" IN ('INACTIVE', 'BLACKLISTED', 'REJECTED') THEN ''
          ELSE '잠재업체'
        END
       as "vendor_category", 
        (SELECT CASE 
           WHEN vpm1.item_name IS NOT NULL AND vpm1.item_code IS NOT NULL 
           THEN vpm1.item_name || E'
(' || vpm1.item_code || ')'
           ELSE NULL
         END
         FROM vendor_possible_materials vpm1
         WHERE vpm1.vendor_id = "vendors"."id"
         ORDER BY vpm1.is_confirmed DESC NULLS LAST, vpm1.created_at DESC
         LIMIT 1 OFFSET 0)
       as "primary_material_1", 
        (SELECT CASE 
           WHEN vpm2.item_name IS NOT NULL AND vpm2.item_code IS NOT NULL 
           THEN vpm2.item_name || E'
(' || vpm2.item_code || ')'
           ELSE NULL
         END
         FROM vendor_possible_materials vpm2
         WHERE vpm2.vendor_id = "vendors"."id"
         ORDER BY vpm2.is_confirmed DESC NULLS LAST, vpm2.created_at DESC
         LIMIT 1 OFFSET 1)
       as "primary_material_2", 
        (SELECT CASE 
           WHEN vpm3.item_name IS NOT NULL AND vpm3.item_code IS NOT NULL 
           THEN vpm3.item_name || E'
(' || vpm3.item_code || ')'
           ELSE NULL
         END
         FROM vendor_possible_materials vpm3
         WHERE vpm3.vendor_id = "vendors"."id"
         ORDER BY vpm3.is_confirmed DESC NULLS LAST, vpm3.created_at DESC
         LIMIT 1 OFFSET 2)
       as "primary_material_3", 
        (SELECT c.contract_no
         FROM contracts c
         WHERE c.vendor_id = "vendors"."id"
         ORDER BY c.start_date DESC NULLS LAST
         LIMIT 1)
       as "recent_po_number", 
        (SELECT CASE 
           WHEN u.name IS NOT NULL AND u.user_code IS NOT NULL 
           THEN u.name || E'
(' || u.user_code || ')'
           ELSE c.purchase_group
         END
         FROM contracts c
         LEFT JOIN users u ON c.purchase_group = u.user_code
         WHERE c.vendor_id = "vendors"."id"
         ORDER BY c.start_date DESC NULLS LAST
         LIMIT 1)
       as "recent_po_order_by", 
        (SELECT c.start_date
         FROM contracts c
         WHERE c.vendor_id = "vendors"."id"
         ORDER BY c.start_date DESC NULLS LAST
         LIMIT 1)
       as "recent_po_date", NULL as "recent_delivery_number", NULL as "recent_delivery_by", NULL as "recent_delivery_date", NULL as "regular_evaluation_grade", NULL as "fa_contract", NULL as "avl_registration", NULL as "regular_vendor_registration" from "vendors" left join "vendor_types" on "vendors"."vendor_type_id" = "vendor_types"."id");--> statement-breakpoint
CREATE VIEW "public"."vendors_with_types" AS (select "vendors"."id" as "id", "vendors"."vendor_name" as "vendor_name", "vendors"."vendor_code" as "vendor_code", "vendors"."tax_id" as "tax_id", "vendors"."address" as "address", "vendors"."address_detail" as "address_detail", "vendors"."postal_code" as "postal_code", "vendors"."country" as "country", "vendors"."phone" as "phone", "vendors"."email" as "email", "vendors"."business_size" as "business_size", "vendors"."website" as "website", "vendors"."status" as "status", "vendors"."vendor_type_id" as "vendor_type_id", "vendors"."representative_name" as "representative_name", "vendors"."representative_birth" as "representative_birth", "vendors"."representative_email" as "representative_email", "vendors"."representative_phone" as "representative_phone", "vendors"."corporate_registration_number" as "corporate_registration_number", "vendors"."items" as "items", "vendors"."credit_agency" as "credit_agency", "vendors"."credit_rating" as "credit_rating", "vendors"."cash_flow_rating" as "cash_flow_rating", "vendors"."is_association_member" as "is_association_member", "vendors"."created_at" as "created_at", "vendors"."updated_at" as "updated_at", "vendor_types"."name_ko" as "vendor_type_name", "vendor_types"."name_en" as "vendor_type_name_en", "vendor_types"."code" as "vendor_type_code", 
        CASE
          WHEN "vendors"."status" = 'ACTIVE' THEN '정규업체'
          WHEN "vendors"."status" IN ('INACTIVE', 'BLACKLISTED', 'REJECTED') THEN ''
          ELSE '잠재업체'
        END
       as "vendor_category" from "vendors" left join "vendor_types" on "vendors"."vendor_type_id" = "vendor_types"."id");