DROP VIEW "public"."vendor_investigations_view";--> statement-breakpoint
CREATE VIEW "public"."vendor_investigations_view" AS (select "vendor_investigations"."id", "vendor_investigations"."vendor_id", "vendor_investigations"."pq_submission_id", "vendor_investigations"."requester_id", "vendor_investigations"."qm_manager_id", "vendor_investigations"."investigation_status", "vendor_investigations"."evaluation_type", "vendor_investigations"."investigation_address", "vendor_investigations"."investigation_method", "vendor_investigations"."scheduled_start_at", "vendor_investigations"."scheduled_end_at", "vendor_investigations"."forecasted_at", "vendor_investigations"."requested_at", "vendor_investigations"."confirmed_at", "vendor_investigations"."completed_at", "vendor_investigations"."evaluation_score", "vendor_investigations"."evaluation_result", "vendor_investigations"."investigation_notes", "vendor_investigations"."created_at", "vendor_investigations"."updated_at", "vendor_investigations"."created_at", "vendor_investigations"."updated_at", "vendors"."vendor_name", "vendors"."vendor_code", "vendors"."tax_id", "vendors"."status", "vendors"."country", "vendors"."email", "vendors"."phone", "vendors"."website", (
        SELECT COALESCE(
          json_agg(json_build_object(
            'id', c.id,
            'contactName', c.contact_name,
            'contactEmail', c.contact_email,
            'contactPhone', c.contact_phone,
            'contactPosition', c.contact_position,
            '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', pi.id,
            'vendorId', pi.vendor_id,
            'itemCode', pi.item_code,
            'itemName', i.item_name,

          )),
          '[]'::json
        )
        FROM vendor_possible_items pi
        LEFT JOIN items i ON pi.item_code = i.item_code
        WHERE pi.vendor_id = "vendors"."id"
      ) as "possibleItems" from "vendor_investigations" left join "vendors" on "vendor_investigations"."vendor_id" = "vendors"."id");