DROP VIEW "public"."rfqs_view";--> statement-breakpoint
CREATE VIEW "public"."rfqs_view" AS (select "rfqs"."id" as "rfq_id", "rfqs"."status" as "status", "rfqs"."created_at" as "created_at", "rfqs"."updated_at" as "updated_at", "rfqs"."created_by" as "created_by", "rfqs"."rfq_type" as "rfq_type", "rfqs"."rfq_code" as "rfq_code", "rfqs"."description" as "description", "rfqs"."due_date" as "due_date", "rfqs"."parent_rfq_id" as "parent_rfq_id", "projects"."id" as "project_id", "projects"."code" as "project_code", "projects"."name" as "project_name", "users"."email" as "user_email", "users"."name" as "user_name", (
          SELECT COUNT(*) 
          FROM "rfq_items" 
          WHERE "rfq_items"."rfq_id" = "rfqs"."id"
        ) as "item_count", (
          SELECT COUNT(*) 
          FROM "rfq_attachments" 
          WHERE "rfq_attachments"."rfq_id" = "rfqs"."id"
        ) as "attachment_count", (
          SELECT MAX("created_at") 
          FROM "rfq_attachments" 
          WHERE "rfq_attachments"."rfq_id" = "rfqs"."id"
        ) as "latest_attachment_date", (
          SELECT MIN("unit_price") 
          FROM "rfq_items" 
          WHERE "rfq_items"."rfq_id" = "rfqs"."id"
        ) as "min_item_price", (
          SELECT MAX("unit_price") 
          FROM "rfq_items" 
          WHERE "rfq_items"."rfq_id" = "rfqs"."id"
        ) as "max_item_price", (
          SELECT SUM("unit_price" * "quantity") 
          FROM "rfq_items" 
          WHERE "rfq_items"."rfq_id" = "rfqs"."id"
        ) as "total_items_amount" from "rfqs" left join "projects" on "rfqs"."project_id" = "projects"."id" left join "users" on "rfqs"."created_by" = "users"."id");