DROP VIEW "public"."vendor_detail_view";--> statement-breakpoint
DROP VIEW "public"."vendors_with_types";--> statement-breakpoint
ALTER TABLE "item_offshore_hull" RENAME COLUMN "item_list1" TO "item_list";--> statement-breakpoint
ALTER TABLE "item_offshore_hull" RENAME COLUMN "item_list2" TO "sub_item_list";--> statement-breakpoint
ALTER TABLE "item_offshore_top" RENAME COLUMN "item_list1" TO "item_list";--> statement-breakpoint
ALTER TABLE "item_offshore_top" RENAME COLUMN "item_list2" TO "sub_item_list";--> statement-breakpoint
ALTER TABLE "item_shipbuilding" ALTER COLUMN "ship_types" SET DEFAULT 'OPTION';--> statement-breakpoint
ALTER TABLE "item_shipbuilding" ADD COLUMN "item_list" text;--> statement-breakpoint
ALTER TABLE "item_offshore_hull" DROP COLUMN "item_list3";--> statement-breakpoint
ALTER TABLE "item_offshore_hull" DROP COLUMN "item_list4";--> statement-breakpoint
ALTER TABLE "item_offshore_top" DROP COLUMN "item_list3";--> statement-breakpoint
ALTER TABLE "item_offshore_top" DROP COLUMN "item_list4";--> statement-breakpoint
CREATE VIEW "public"."form_lists_view" AS (select "tag_type_class_form_mappings"."id" as "id", "projects"."code" as "project_code", "projects"."name" as "project_name", "tag_type_class_form_mappings"."tag_type_label" as "tag_type_label", "tag_type_class_form_mappings"."class_label" as "class_label", "tag_type_class_form_mappings"."form_code" as "form_code", "tag_type_class_form_mappings"."form_name" as "form_name", "tag_type_class_form_mappings"."ep" as "ep", "tag_type_class_form_mappings"."remark" as "remark", "tag_type_class_form_mappings"."created_at" as "created_at", "tag_type_class_form_mappings"."updated_at" as "updated_at" from "tag_type_class_form_mappings" inner join "projects" on "tag_type_class_form_mappings"."project_id" = "projects"."id");--> statement-breakpoint
CREATE VIEW "public"."vendor_detail_view" AS (select "id", "vendor_name", "vendor_code", "tax_id", "address", "business_size", "country", "phone", "email", "website", "status", "representative_name", "representative_birth", "representative_email", "representative_phone", "corporate_registration_number", "credit_agency", "credit_rating", "cash_flow_rating", "created_at", "updated_at", 
        (SELECT COALESCE(
          json_agg(
            json_build_object(
              'id', c.id,
              'contactName', c.contact_name,
              'contactPosition', c.contact_position,
              'contactEmail', c.contact_email,
              'contactPhone', c.contact_phone,
              'isPrimary', c.is_primary
            )
          ),
          '[]'::json
        )
        FROM vendor_contacts c
        WHERE c.vendor_id = vendors.id)
       as "contacts", 
        (SELECT COALESCE(
          json_agg(
            json_build_object(
              'id', a.id,
              'fileName', a.file_name,
              'filePath', a.file_path,
              'attachmentType', a.attachment_type,
              'createdAt', a.created_at
            )
            ORDER BY a.attachment_type, a.created_at DESC
          ),
          '[]'::json
        )
        FROM vendor_attachments a
        WHERE a.vendor_id = vendors.id)
       as "attachments", 
        (SELECT COUNT(*)
        FROM vendor_attachments a
        WHERE a.vendor_id = vendors.id)
       as "attachment_count", 
        (SELECT COUNT(*) 
        FROM vendor_contacts c
        WHERE c.vendor_id = vendors.id)
       as "contact_count" from "vendors");--> statement-breakpoint
CREATE VIEW "public"."vendors_with_types" AS (select "vendors"."id", "vendors"."vendor_name", "vendors"."vendor_code", "vendors"."tax_id", "vendors"."address", "vendors"."country", "vendors"."phone", "vendors"."email", "vendors"."business_size", "vendors"."website", "vendors"."status", "vendors"."vendor_type_id", "vendors"."representative_name", "vendors"."representative_birth", "vendors"."representative_email", "vendors"."representative_phone", "vendors"."corporate_registration_number", "vendors"."items", "vendors"."credit_agency", "vendors"."credit_rating", "vendors"."cash_flow_rating", "vendors"."created_at", "vendors"."updated_at", "vendor_types"."name_ko", "vendor_types"."name_en", "vendor_types"."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");