-- IGNORE: requires postgis; see tests/corpus_sagri_mrv.rs for the dedicated runner
-- Sanitized snapshot of a real-world PostgreSQL schema.
-- Generated by scripts/sanitize_schema.py — see
-- tests/corpus/sagri_mrv.README.md for provenance and policy.
-- This file is intentionally unreadable; identifiers are opaque.
CREATE SCHEMA IF NOT EXISTS "extensions";






CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA extensions;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA extensions;


CREATE EXTENSION IF NOT EXISTS "postgis" WITH SCHEMA public;


CREATE EXTENSION IF NOT EXISTS "plpgsql" WITH SCHEMA pg_catalog;

CREATE SCHEMA IF NOT EXISTS "auth";




ALTER SCHEMA "auth" OWNER TO "supabase_auth_admin";

ALTER SEQUENCE "auth"."seq_0001"
    OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0046" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0054" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0055" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0056" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0070" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0071" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0072" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0075" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0077" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0080" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0081" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0088" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0089" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0090" OWNER TO "supabase_auth_admin";

ALTER TABLE "auth"."t_0095" OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0028" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0037" (jsonb) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0041" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0056" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0061" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0062" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0065" (text, uuid, uuid, uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0066" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0070" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0071" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."role" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0092" (jsonb) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0102" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0111" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0112" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0113" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0115" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0116" (text, text, uuid, uuid, uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0117" (text, text) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0118" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0119" (uuid, uuid, uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0120" (uuid, uuid, uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0121" () OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0122" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0123" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0124" (uuid, text) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0126" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0122" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0111" (uuid) OWNER TO "supabase_auth_admin";

ALTER FUNCTION "auth"."f_0143" (uuid) OWNER TO "supabase_auth_admin";


CREATE TYPE "auth"."ty_0030" AS ENUM ('v1', 'v2', 'v3');

CREATE TYPE "auth"."ty_0031" AS ENUM ('v1', 'v2');

CREATE TYPE "auth"."ty_0032" AS ENUM ('v1', 'v2');

CREATE TYPE "auth"."ty_0033" AS ENUM ('v1', 'v2', 'v3');

CREATE TYPE "auth"."ty_0035" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6');





CREATE SEQUENCE "auth"."seq_0001";

CREATE TABLE "auth"."t_0087" (
    "c_0539" VARCHAR(255) NOT NULL,
    PRIMARY KEY ("c_0539")
);

CREATE TABLE "auth"."t_0056" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "id" UUID NOT NULL,
    "c_0385" TEXT,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    "uuid" UUID,
    PRIMARY KEY ("id")
);

CREATE TABLE "auth"."t_0046" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "id" UUID NOT NULL,
    "c_0238" UUID,
    "c_0245" VARCHAR(64) NOT NULL DEFAULT '_'::character varying,
    "c_0341" JSON,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0064" ON "auth"."t_0046" ("c_0238");

CREATE TABLE "auth"."t_0054" (
    "c_0026" TEXT NOT NULL,
    "c_0027" TIMESTAMP WITH TIME ZONE,
    "c_0028" TEXT NOT NULL,
    "c_0070" TEXT NOT NULL,
    "ty_0031" "auth"."ty_0031" NOT NULL,
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "id" UUID NOT NULL,
    "c_0377" TEXT,
    "c_0379" TEXT,
    "c_0380" TEXT NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    "p_0124" UUID,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0069" ON "auth"."t_0054" ("c_0095");

CREATE INDEX "idx_0083" ON "auth"."t_0054" ("c_0026");

CREATE INDEX "idx_0115" ON "auth"."t_0054" ("p_0124", "c_0028");

CREATE TABLE "auth"."t_0090" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "id" UUID NOT NULL,
    "con_0139" TEXT,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY ("id")
);

ALTER TABLE "auth"."t_0090" ADD CONSTRAINT "con_0139 not empty" CHECK (1 = 1);

CREATE TABLE "auth"."t_0095" (
    "c_0025" VARCHAR(255),
    "c_0035" TIMESTAMP WITH TIME ZONE,
    "c_0081" TIMESTAMP WITH TIME ZONE,
    "c_0082" VARCHAR(255),
    "c_0083" TIMESTAMP WITH TIME ZONE,
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "c_0135" TIMESTAMP WITH TIME ZONE,
    "f_0041" VARCHAR(255),
    "c_0149" VARCHAR(255),
    "c_0150" SMALLINT DEFAULT 0,
    "c_0151" TIMESTAMP WITH TIME ZONE,
    "c_0152" VARCHAR(255) DEFAULT '_'::character varying,
    "c_0153" VARCHAR(255),
    "c_0154" TIMESTAMP WITH TIME ZONE,
    "c_0167" VARCHAR(255),
    "id" UUID NOT NULL,
    "c_0238" UUID,
    "c_0241" TIMESTAMP WITH TIME ZONE,
    "c_0260" BOOLEAN NOT NULL DEFAULT false,
    "c_0270" BOOLEAN NOT NULL DEFAULT false,
    "c_0271" BOOLEAN,
    "c_0281" TIMESTAMP WITH TIME ZONE,
    "c_0346" TEXT DEFAULT NULL::character varying,
    "c_0347" TEXT DEFAULT '_'::character varying,
    "c_0348" TIMESTAMP WITH TIME ZONE,
    "c_0349" VARCHAR(255) DEFAULT '_'::character varying,
    "c_0350" TIMESTAMP WITH TIME ZONE,
    "c_0384" JSONB,
    "c_0386" JSONB,
    "c_0388" TIMESTAMP WITH TIME ZONE,
    "c_0389" VARCHAR(255) DEFAULT '_'::character varying,
    "c_0392" TIMESTAMP WITH TIME ZONE,
    "c_0393" VARCHAR(255),
    "role" VARCHAR(255),
    "c_0529" TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "idx_0065" ON "auth"."t_0095" ("c_0082") WHERE ((c_0082)::text <> '_'::text);

CREATE UNIQUE INDEX "idx_0066" ON "auth"."t_0095" ("c_0152") WHERE ((c_0152)::text <> '_'::text);

CREATE UNIQUE INDEX "idx_0067" ON "auth"."t_0095" ("c_0153") WHERE ((c_0153)::text <> '_'::text);

CREATE UNIQUE INDEX "idx_0132" ON "auth"."t_0095" ("c_0389") WHERE ((c_0389)::text <> '_'::text);

CREATE UNIQUE INDEX "idx_0133" ON "auth"."t_0095" ("c_0393") WHERE ((c_0393)::text <> '_'::text);

CREATE UNIQUE INDEX "idx_0154" ON "auth"."t_0095" ("f_0041");

CREATE INDEX "idx_0155" ON "auth"."t_0095" ("c_0238");

CREATE INDEX "idx_0156" ON "auth"."t_0095" ("c_0238");

CREATE INDEX "idx_0157" ON "auth"."t_0095" ("c_0260");

CREATE UNIQUE INDEX "idx_0158" ON "auth"."t_0095" ("c_0346");

ALTER TABLE "auth"."t_0095" ADD CONSTRAINT "con_0172" CHECK (2 = 2);

CREATE TABLE "auth"."t_0089" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "con_0117" TEXT NOT NULL,
    "id" UUID NOT NULL,
    "c_0469" UUID NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0148" ON "auth"."t_0089" ("c_0469");

ALTER TABLE "auth"."t_0089" ADD CONSTRAINT "con_0160" FOREIGN KEY ("c_0469") REFERENCES "auth"."t_0090" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "auth"."t_0089" ADD CONSTRAINT "con_0117 not empty" CHECK (3 = 3);

CREATE TABLE "auth"."t_0081" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "c_0203" UUID,
    "c_0204" TEXT,
    "id" UUID NOT NULL,
    "c_0394" TEXT,
    "con_0138" TEXT NOT NULL,
    "c_0469" UUID NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0143" ON "auth"."t_0081" ("c_0095");

CREATE INDEX "idx_0144" ON "auth"."t_0081" ("c_0204");

CREATE INDEX "idx_0145" ON "auth"."t_0081" ("c_0469");

ALTER TABLE "auth"."t_0081" ADD CONSTRAINT "con_0144" FOREIGN KEY ("c_0203") REFERENCES "auth"."t_0054" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "auth"."t_0081" ADD CONSTRAINT "con_0145" FOREIGN KEY ("c_0469") REFERENCES "auth"."t_0090" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "auth"."t_0081" ADD CONSTRAINT "con_0138 not empty" CHECK (4 = 4);

CREATE TABLE "auth"."t_0080" (
    "c_0024" JSONB,
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "con_0122" TEXT NOT NULL,
    "id" UUID NOT NULL,
    "con_0127" TEXT,
    "con_0128" TEXT NOT NULL,
    "c_0320" TEXT,
    "c_0469" UUID NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "idx_0141" ON "auth"."t_0080" ("con_0122");

CREATE INDEX "idx_0142" ON "auth"."t_0080" ("c_0469");

ALTER TABLE "auth"."t_0080" ADD CONSTRAINT "con_0143" FOREIGN KEY ("c_0469") REFERENCES "auth"."t_0090" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "auth"."t_0080" ADD CONSTRAINT "con_0122 not empty" CHECK (5 = 5);

ALTER TABLE "auth"."t_0080" ADD CONSTRAINT "con_0127 not empty" CHECK (6 = 6);

ALTER TABLE "auth"."t_0080" ADD CONSTRAINT "con_0128 not empty" CHECK (7 = 7);

CREATE TABLE "auth"."t_0075" (
    "c_0095" TIMESTAMP NOT NULL DEFAULT now(),
    "id" UUID NOT NULL,
    "c_0400" TEXT NOT NULL,
    "c_0506" TEXT NOT NULL,
    "c_0507" "auth"."ty_0035" NOT NULL,
    "c_0529" TIMESTAMP NOT NULL DEFAULT now(),
    "p_0124" UUID NOT NULL,
    PRIMARY KEY ("id")
);

CREATE INDEX "one_time_tokens_relates_to_hash_idx" USING hash ON "auth"."t_0075" ("c_0400");

CREATE INDEX "one_time_tokens_token_hash_hash_idx" USING hash ON "auth"."t_0075" ("c_0506");

CREATE UNIQUE INDEX "idx_0130" ON "auth"."t_0075" ("p_0124", "c_0507");

ALTER TABLE "auth"."t_0075" ADD CONSTRAINT "con_0135" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "auth"."t_0075" ADD CONSTRAINT "con_0134" CHECK (8 = 8);

CREATE TABLE "auth"."t_0055" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "f_0041" TEXT,
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0232" JSONB NOT NULL,
    "c_0281" TIMESTAMP WITH TIME ZONE,
    "c_0376" TEXT NOT NULL,
    "c_0378" TEXT NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    "p_0124" UUID NOT NULL,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0070" ON "auth"."t_0055" ("f_0041");

CREATE UNIQUE INDEX "idx_0071" ON "auth"."t_0055" ("c_0378", "c_0376");

CREATE INDEX "idx_0072" ON "auth"."t_0055" ("p_0124");

ALTER TABLE "auth"."t_0055" ADD CONSTRAINT "con_0124" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

CREATE TABLE "auth"."t_0088" (
    "c_0001" "auth"."ty_0030",
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "c_0185" UUID,
    "id" UUID NOT NULL,
    "c_0244" TEXT,
    "c_0326" TIMESTAMP WITH TIME ZONE,
    "c_0395" TIMESTAMP,
    "c_0493" TEXT,
    "c_0529" TIMESTAMP WITH TIME ZONE,
    "c_0531" TEXT,
    "p_0124" UUID NOT NULL,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0146" ON "auth"."t_0088" ("c_0326");

CREATE INDEX "idx_0147" ON "auth"."t_0088" ("p_0124");

CREATE INDEX "idx_0152" ON "auth"."t_0088" ("p_0124", "c_0095");

ALTER TABLE "auth"."t_0088" ADD CONSTRAINT "con_0159" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

CREATE TABLE "auth"."t_0072" (
    "c_0095" TIMESTAMP WITH TIME ZONE NOT NULL,
    "ty_0033" "auth"."ty_0033" NOT NULL,
    "c_0205" TEXT,
    "id" UUID NOT NULL,
    "c_0280" TIMESTAMP WITH TIME ZONE,
    "c_0346" TEXT,
    "c_0438" TEXT,
    "c_0472" "auth"."ty_0032" NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE NOT NULL,
    "p_0124" UUID NOT NULL,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0068" ON "auth"."t_0072" ("p_0124", "c_0095");

CREATE UNIQUE INDEX "idx_0125" ON "auth"."t_0072" ("c_0280");

CREATE UNIQUE INDEX "idx_0126" ON "auth"."t_0072" ("c_0346");

CREATE UNIQUE INDEX "idx_0127" ON "auth"."t_0072" ("c_0205", "p_0124");

CREATE INDEX "idx_0128" ON "auth"."t_0072" ("p_0124");

CREATE UNIQUE INDEX "idx_0151" ON "auth"."t_0072" ("p_0124", "c_0346");

ALTER TABLE "auth"."t_0072" ADD CONSTRAINT "con_0132" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

CREATE TABLE "auth"."t_0070" (
    "c_0028" TEXT NOT NULL,
    "c_0095" TIMESTAMP WITH TIME ZONE NOT NULL,
    "id" UUID NOT NULL,
    "c_0440" UUID NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE NOT NULL,
    PRIMARY KEY ("id")
);

ALTER TABLE "auth"."t_0070" ADD CONSTRAINT "con_0129" UNIQUE ("c_0440", "c_0028");

ALTER TABLE "auth"."t_0070" ADD CONSTRAINT "con_0130" FOREIGN KEY ("c_0440") REFERENCES "auth"."t_0088" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

CREATE TABLE "auth"."t_0077" (
    "c_0095" TIMESTAMP WITH TIME ZONE,
    "id" BIGINT NOT NULL DEFAULT nextval('auth.seq_0001'::regclass),
    "c_0238" UUID,
    "c_0340" VARCHAR(255),
    "c_0414" BOOLEAN,
    "c_0440" UUID,
    "c_0505" VARCHAR(255),
    "c_0529" TIMESTAMP WITH TIME ZONE,
    "p_0124" VARCHAR(255),
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0134" ON "auth"."t_0077" ("c_0238");

CREATE INDEX "idx_0135" ON "auth"."t_0077" ("c_0238", "p_0124");

CREATE INDEX "idx_0136" ON "auth"."t_0077" ("c_0340");

CREATE INDEX "idx_0137" ON "auth"."t_0077" ("c_0440", "c_0414");

CREATE UNIQUE INDEX "idx_0138" ON "auth"."t_0077" ("c_0505");

CREATE INDEX "idx_0139" ON "auth"."t_0077" ("c_0529");

ALTER TABLE "auth"."t_0077" ADD CONSTRAINT "con_0137" FOREIGN KEY ("c_0440") REFERENCES "auth"."t_0088" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

CREATE TABLE "auth"."t_0071" (
    "c_0095" TIMESTAMP WITH TIME ZONE NOT NULL,
    "c_0185" UUID NOT NULL,
    "id" UUID NOT NULL,
    "c_0245" TEXT NOT NULL,
    "c_0337" TEXT,
    "c_0536" TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY ("id")
);

CREATE INDEX "idx_0124" ON "auth"."t_0071" ("c_0095");

ALTER TABLE "auth"."t_0071" ADD CONSTRAINT "con_0131" FOREIGN KEY ("c_0185") REFERENCES "auth"."t_0072" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "auth"."t_0046" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0054" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0055" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0056" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0070" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0071" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0072" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0075" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0077" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0080" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0081" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0087" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0088" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0089" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0090" ENABLE ROW LEVEL SECURITY;

ALTER TABLE "auth"."t_0095" ENABLE ROW LEVEL SECURITY;

CREATE TRIGGER "f_0070" AFTER INSERT ON "auth"."t_0095" FOR EACH ROW EXECUTE FUNCTION "auth"."f_0070"();

CREATE TRIGGER "f_0071" AFTER UPDATE ON "auth"."t_0095" FOR EACH ROW EXECUTE FUNCTION "auth"."f_0071"();







GRANT SELECT, UPDATE, USAGE ON SEQUENCE "auth"."seq_0001" TO "supabase_auth_admin";
GRANT SELECT, UPDATE, USAGE ON SEQUENCE "auth"."seq_0001" TO "dashboard_user";


GRANT ALL PRIVILEGES ON TABLE "auth"."t_0046" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0054" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0055" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0056" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0070" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0071" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0072" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0075" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0077" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0080" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0081" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0087" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0088" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0089" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0090" TO "supabase_auth_admin";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0095" TO "supabase_auth_admin";


GRANT ALL PRIVILEGES ON TABLE "auth"."t_0046" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0054" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0055" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0056" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0070" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0071" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0072" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0075" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0077" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0080" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0081" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0087" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0088" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0089" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0090" TO "dashboard_user";
GRANT ALL PRIVILEGES ON TABLE "auth"."t_0095" TO "dashboard_user";


GRANT SELECT ON TABLE "auth"."t_0046" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0054" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0055" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0056" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0070" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0071" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0072" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0075" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0077" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0080" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0081" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0087" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0088" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0089" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0090" TO "metabase_ro";
GRANT SELECT ON TABLE "auth"."t_0095" TO "metabase_ro";


GRANT SELECT ON TABLE "auth"."t_0095" TO "authenticated";









CREATE FUNCTION "auth"."f_0028"() RETURNS TABLE (
  p_0124 uuid,
  f_0041 text,
  c_0199 text[]
) LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$
DECLARE
  user_record RECORD;
  cleaned_meta JSONB;
  removed_fields TEXT[];
  template_fields TEXT[];
  field TEXT;
BEGIN
  
  
  IF NOT (
    session_user = '_' OR
    session_user = '_' OR
    COALESCE(current_setting('_', true)::jsonb->>'_', '_') = '_'
  ) THEN
    RAISE EXCEPTION '_';
  END IF;

  
  template_fields := auth.f_0056();

  
  
  PERFORM set_config('_', '_', true);

  BEGIN
    FOR user_record IN
      SELECT u.id, u.f_0041, u.c_0386
      FROM auth.t_0095 u
      WHERE u.c_0386 ?| template_fields
    LOOP
      
      removed_fields := ARRAY[]::TEXT[];
      cleaned_meta := user_record.c_0386;

      FOREACH field IN ARRAY template_fields LOOP
        IF user_record.c_0386 ? field THEN
          removed_fields := removed_fields || field;
          cleaned_meta := cleaned_meta - field;
        END IF;
      END LOOP;

      
      UPDATE auth.t_0095
      SET c_0386 = cleaned_meta
      WHERE id = user_record.id;

      
      p_0124 := user_record.id;
      f_0041 := user_record.f_0041;
      c_0199 := removed_fields;
      RETURN NEXT;
    END LOOP;
  EXCEPTION
    WHEN OTHERS THEN
      
      PERFORM set_config('_', '_', true);
      RAISE;
  END;

  
  PERFORM set_config('_', '_', true);

  RETURN;
END;$$;


CREATE FUNCTION "auth"."f_0037"("p_0003" jsonb) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
    p_0124 uuid;
    claims jsonb;
    rbac_context jsonb;
    debug_info jsonb := 'null'::jsonb;
BEGIN
    BEGIN
        claims := p_0003->'_';
        debug_info := debug_info || jsonb_build_object('_', true);

        BEGIN
            p_0124 := (claims ->> '_')::uuid;
        EXCEPTION WHEN OTHERS THEN
            p_0124 := NULL;
            debug_info := debug_info || jsonb_build_object('_', true);
        END;

        debug_info := debug_info || jsonb_build_object('_', p_0124);

        IF p_0124 IS NOT NULL THEN
            SELECT public.f_0059(p_0124) INTO rbac_context;

            debug_info := debug_info || jsonb_build_object(
                '_', true,
                '_', rbac_context IS NOT NULL
            );
        END IF;

        IF rbac_context IS NULL THEN
            rbac_context := jsonb_build_object(
                '_', jsonb_build_array(),
                '_', jsonb_build_array(),
                '_', jsonb_build_array(),
                '_', jsonb_build_array(),
                '_', jsonb_build_array(),
                '_', jsonb_build_array(),
                '_', NULL,
                '_', jsonb_build_array()
            );
            debug_info := debug_info || jsonb_build_object('_', true);
        END IF;

        
        claims := jsonb_set(
            claims,
            '_',
            rbac_context,
            true
        );
        
        claims := claims || jsonb_build_object(
            '_', rbac_context,
            '_', debug_info
        );

        p_0003 := jsonb_set(p_0003, '_', claims);

        RETURN p_0003;
    EXCEPTION WHEN OTHERS THEN
        claims := p_0003->'_';
        claims := claims || jsonb_build_object(
            '_', SQLERRM,
            '_', SQLSTATE,
            '_', debug_info
        );
        p_0003 := jsonb_set(p_0003, '_', claims);
        RETURN p_0003;
    END;
END;$$;



CREATE FUNCTION "auth"."f_0041"() RETURNS text LANGUAGE sql STABLE SECURITY INVOKER AS $$select
  coalesce(
    nullif(current_setting('_', true), '_'),
    (nullif(current_setting('_', true), '_')::jsonb ->> '_'),
    nullif(current_setting('_', true), '_')
  )::text$$;









CREATE FUNCTION "auth"."f_0056"() RETURNS text[] LANGUAGE sql IMMUTABLE AS $$
  SELECT ARRAY[
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_',
    '_'
  ];
$$;


CREATE OR REPLACE FUNCTION auth.f_0061() RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    SELECT auth.f_0062()
$$;



CREATE OR REPLACE FUNCTION auth.f_0062() RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    SELECT COALESCE(
        (
            
            SELECT rbac_context->'_' ? '_'
            FROM (
                SELECT auth.f_0066()->'_' AS rbac_context
            ) jwt_data
            WHERE rbac_context IS NOT NULL
        ),
        FALSE
    )
$$;


CREATE FUNCTION "auth"."f_0065"("p_0090" text, "p_0026" uuid DEFAULT null::uuid, "p_0105" uuid DEFAULT null::uuid, "p_0037" uuid DEFAULT null::uuid) RETURNS boolean LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
    v_user_id uuid := auth.f_0102 ();
    v_result boolean;
BEGIN
    
    SELECT
        EXISTS (
            SELECT
                1
            FROM
                public.t_0094 ur
                JOIN public.t_0079 r ON ur.c_0417 = r.id
                JOIN public.t_0078 rp ON ur.c_0417 = rp.c_0417
                JOIN public.t_0076 p ON rp.c_0344 = p.id
            WHERE
                ur.p_0124 = v_user_id
                AND r.name = p_0090
                AND p.c_0409 = '_'
                AND p.c_0334 = '_'
                AND ((p_0026 IS NULL
                        OR ur.p_0001 = p_0026)
                    AND (p_0105 IS NULL
                        OR ur.p_0122 = p_0105)
                    AND (p_0037 IS NULL
                        OR ur.p_0005 = p_0037))) INTO v_result;
    RETURN v_result;
END;$$;






CREATE FUNCTION "auth"."f_0066"() RETURNS jsonb LANGUAGE sql STABLE SECURITY INVOKER AS $$SELECT
    coalesce(
        nullif(current_setting('_', true), '_')::jsonb,
        nullif(current_setting('_', true), '_')::jsonb,
        CASE WHEN nullif(current_setting('_', true), '_') IS NOT NULL
        THEN jsonb_strip_nulls(jsonb_build_object(
            '_', current_setting('_', true),
            '_', current_setting('_', true),
            '_', current_setting('_', true),
            '_', CASE
                WHEN nullif(current_setting('_', true), '_') IS NOT NULL
                THEN current_setting('_', true)::jsonb
                WHEN nullif(current_setting('_', true), '_') IS NOT NULL
                THEN (current_setting('_', true)::jsonb) -> '_'
            END
        ))
        END
    )$$;


CREATE OR REPLACE FUNCTION "auth"."f_0070"() RETURNS trigger LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public AS $$DECLARE
    c_0240 text;
    p_0121 text;
    enterprise_id_param uuid;
    supplier_id_param uuid;
    farmer_id_param uuid;
    methodology_mode_param text;
    new_role_id uuid;
    new_enterprise_id uuid;
    new_supplier_id uuid;
    new_farmer_id uuid;
    inviter_id uuid;
    inviter_roles text;
    inviter_enterprise_id uuid;
BEGIN
    
    
    
    c_0240 := NEW.c_0386 ->> '_';
    p_0121 := NEW.c_0386 ->> '_';
    enterprise_id_param := (NEW.c_0386 ->> '_')::uuid;
    supplier_id_param := (NEW.c_0386 ->> '_')::uuid;
    farmer_id_param := (NEW.c_0386 ->> '_')::uuid;
    methodology_mode_param := NEW.c_0386 ->> '_';
    inviter_id := (NEW.c_0386 ->> '_')::uuid;
    
    IF c_0240 IS NOT NULL AND p_0121 IS NOT NULL THEN
        
        SELECT
            id INTO new_role_id
        FROM
            t_0079
        WHERE
            name = p_0121;
        IF new_role_id IS NULL THEN
            RAISE EXCEPTION '_%_', p_0121;
        END IF;
        
        IF c_0240 = '_' THEN
            
            SELECT
                string_agg(r.name || '_' || COALESCE(ur.p_0001::text, '_') || '_' || COALESCE(ur.p_0122::text, '_') || '_' || COALESCE(ur.p_0005::text, '_') || '_', '_') INTO inviter_roles
            FROM
                t_0094 ur
                JOIN t_0079 r ON ur.c_0417 = r.id
            WHERE
                ur.p_0124 = inviter_id;
            
            IF supplier_id_param IS NULL AND enterprise_id_param IS NULL THEN
                RAISE EXCEPTION '_%_%_%_', inviter_id, inviter_roles, NEW.c_0386;
            END IF;
            IF supplier_id_param IS NOT NULL THEN
                
                
                
                
                
                SELECT ur.p_0001 INTO inviter_enterprise_id
                FROM t_0094 ur
                JOIN t_0079 r ON ur.c_0417 = r.id
                WHERE ur.p_0124 = inviter_id
                  AND r.name IN ('v1', 'v1')
                  AND ur.p_0001 IS NOT NULL
                  AND EXISTS (
                    SELECT 1 FROM public.t_0050 es2
                    WHERE es2.p_0122 = supplier_id_param
                      AND es2.p_0001 = ur.p_0001
                  )
                LIMIT 1;

                IF inviter_enterprise_id IS NOT NULL THEN
                    enterprise_id_param := inviter_enterprise_id;
                ELSE
                    SELECT es.p_0001 INTO enterprise_id_param
                    FROM public.t_0050 es
                    WHERE es.p_0122 = supplier_id_param
                    ORDER BY es.p_0001
                    LIMIT 1;
                END IF;
                
                
                
                IF farmer_id_param IS NULL AND NEW.c_0386 ->> '_' IS NOT NULL THEN
                    INSERT INTO t_0052 (name, c_0095, c_0529)
                        VALUES (NEW.c_0386 ->> '_', NOW(), NOW())
                    RETURNING
                        id INTO new_farmer_id;
                    farmer_id_param := new_farmer_id;
                END IF;
                
                
                
                IF farmer_id_param IS NOT NULL THEN
                    INSERT INTO public.t_0091 (p_0122, p_0005)
                        VALUES (supplier_id_param, farmer_id_param)
                    ON CONFLICT (p_0122, p_0005) DO NOTHING;
                END IF;
                
                IF inviter_id IS NOT NULL THEN
                    IF NOT EXISTS (
                        SELECT
                            1
                        FROM
                            t_0094 ur
                            JOIN t_0079 r ON ur.c_0417 = r.id
                        WHERE
                            ur.p_0124 = inviter_id
                            AND ur.p_0122 = supplier_id_param
                            AND r.name IN ('v1', 'v1')) THEN
                    RAISE EXCEPTION '_';
                END IF;
            END IF;
            ELSE
                
                
                IF farmer_id_param IS NULL AND NEW.c_0386 ->> '_' IS NOT NULL THEN
                    INSERT INTO t_0052 (name, c_0095, c_0529)
                        VALUES (NEW.c_0386 ->> '_', NOW(), NOW())
                    RETURNING
                        id INTO new_farmer_id;
                    farmer_id_param := new_farmer_id;
                END IF;
                
                IF farmer_id_param IS NOT NULL AND enterprise_id_param IS NOT NULL THEN
                    INSERT INTO public.t_0049 (p_0001, p_0005)
                        VALUES (enterprise_id_param, farmer_id_param)
                    ON CONFLICT (p_0001, p_0005) DO NOTHING;
                END IF;
                
                IF inviter_id IS NOT NULL THEN
                    IF NOT EXISTS (
                        SELECT
                            1
                        FROM
                            t_0094 ur
                            JOIN t_0079 r ON ur.c_0417 = r.id
                        WHERE
                            ur.p_0124 = inviter_id
                            AND ur.p_0001 = enterprise_id_param
                            AND r.name = 'v1') THEN
                    RAISE EXCEPTION '_';
                END IF;
            END IF;
            END IF;
    ELSIF c_0240 = '_' THEN
        
        SELECT
            string_agg(r.name || '_' || COALESCE(ur.p_0001::text, '_') || '_' || COALESCE(ur.p_0122::text, '_') || '_', '_') INTO inviter_roles
        FROM
            t_0094 ur
            JOIN t_0079 r ON ur.c_0417 = r.id
        WHERE
            ur.p_0124 = inviter_id;
        
        
        SELECT
            ur.p_0001 INTO inviter_enterprise_id
        FROM
            t_0094 ur
            JOIN t_0079 r ON ur.c_0417 = r.id
        WHERE
            ur.p_0124 = inviter_id
            AND r.name = 'v1'
        LIMIT 1;
        
        
        IF inviter_id IS NOT NULL THEN
            IF NOT EXISTS (
                SELECT
                    1
                FROM
                    t_0094 ur
                    JOIN t_0079 r ON ur.c_0417 = r.id
                WHERE
                    ur.p_0124 = inviter_id
                    AND (r.name IN ('v1', 'v1') OR r.level = '_')) THEN
            RAISE EXCEPTION '_%_%_', inviter_id, inviter_roles;
        END IF;
    END IF;
    
    IF enterprise_id_param IS NULL AND methodology_mode_param IS NULL AND supplier_id_param IS NULL THEN
        RAISE EXCEPTION '_%_%_%_%_%_%_%_', inviter_id, enterprise_id_param, supplier_id_param, p_0121, c_0240, inviter_roles, NEW.c_0386;
    END IF;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    IF supplier_id_param IS NOT NULL AND inviter_id IS NOT NULL THEN
        IF NOT EXISTS (
            SELECT
                1
            FROM
                t_0094 ur
                JOIN t_0079 r ON ur.c_0417 = r.id
            WHERE
                ur.p_0124 = inviter_id
                AND (
                    (ur.p_0122 = supplier_id_param AND r.name = 'v1')
                    OR r.level = '_'
                )) THEN
        RAISE EXCEPTION '_%_%_%_', inviter_id, supplier_id_param, inviter_roles;
    END IF;
END IF;
    
    IF supplier_id_param IS NOT NULL AND enterprise_id_param IS NOT NULL THEN
        IF NOT EXISTS (
            SELECT
                1
            FROM
                public.t_0050 es
            WHERE
                es.p_0122 = supplier_id_param
                AND es.p_0001 = enterprise_id_param) THEN
        RAISE EXCEPTION '_%_%_', supplier_id_param, enterprise_id_param;
    END IF;
END IF;
    
    
    
    
    
    
    
    IF supplier_id_param IS NULL THEN
        IF enterprise_id_param IS NOT NULL THEN
            
            INSERT INTO t_0092 (name, c_0095, c_0529)
                VALUES (NEW.c_0386 ->> '_', NOW(), NOW())
            RETURNING
                id INTO new_supplier_id;
        ELSE
            
            
            IF methodology_mode_param = '_' THEN
                RAISE EXCEPTION '_';
            END IF;
            INSERT INTO t_0092 (name, ty_0034, c_0095, c_0529)
                VALUES (NEW.c_0386 ->> '_', methodology_mode_param::ty_0034, NOW(), NOW())
            RETURNING
                id INTO new_supplier_id;
        END IF;
        supplier_id_param := new_supplier_id;
    END IF;
    
    IF enterprise_id_param IS NOT NULL AND supplier_id_param IS NOT NULL THEN
        INSERT INTO public.t_0050 (p_0001, p_0122)
            VALUES (enterprise_id_param, supplier_id_param)
        ON CONFLICT (p_0001, p_0122) DO NOTHING;
    END IF;
ELSIF c_0240 = '_' THEN
    
    IF methodology_mode_param = '_' THEN
        RAISE EXCEPTION '_';
    END IF;
    
    IF enterprise_id_param IS NULL THEN
        INSERT INTO t_0051 (name, ty_0034, c_0095, c_0529)
            VALUES (COALESCE(NEW.c_0386 ->> '_', split_part(NEW.f_0041, '_', 2), '_' || NEW.f_0041), COALESCE(methodology_mode_param, '_')::ty_0034, NOW(), NOW())
        RETURNING
            id INTO new_enterprise_id;
        enterprise_id_param := new_enterprise_id;
    END IF;
    
    
ELSE
    RAISE EXCEPTION '_%_', c_0240;
END IF;
    
    PERFORM
        set_config('_', '_', TRUE);
    
    INSERT INTO t_0094 (p_0124, c_0417, p_0001, p_0122, p_0005)
        VALUES (NEW.id, new_role_id, CASE WHEN c_0240 IN ('_', '_', '_') THEN
                enterprise_id_param
            ELSE
                NULL
            END, supplier_id_param, farmer_id_param);
    
    PERFORM
        set_config('_', '_', TRUE);
END IF;
    RETURN NEW;
END;$$;

ALTER FUNCTION auth.f_0070() OWNER TO supabase_auth_admin;



CREATE OR REPLACE FUNCTION "auth"."f_0071"() RETURNS trigger LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public AS $$
DECLARE
  new_context JSONB;
  old_context JSONB;
  already_in_trigger TEXT;
  cleaned_meta JSONB;
  needs_meta_cleanup BOOLEAN;
  template_fields TEXT[];
  field TEXT;
BEGIN
  
  already_in_trigger := current_setting('_', true);

  IF already_in_trigger = '_' THEN
    RETURN NEW;
  END IF;

  
  PERFORM set_config('_', '_', true);

  BEGIN
    
    new_context := public.f_0059(NEW.id);
    old_context := COALESCE(OLD.c_0384->'_', 'null'::jsonb);

    
    template_fields := auth.f_0056();

    
    
    
    
    needs_meta_cleanup := (NEW.c_0386 ?| template_fields);

    
    IF new_context IS DISTINCT FROM old_context OR needs_meta_cleanup THEN
      
      IF needs_meta_cleanup THEN
        cleaned_meta := NEW.c_0386;
        FOREACH field IN ARRAY template_fields LOOP
          cleaned_meta := cleaned_meta - field;
        END LOOP;

        UPDATE auth.t_0095
        SET
          c_0386 = cleaned_meta,
          c_0384 = jsonb_set(
            COALESCE(c_0384, 'null'::jsonb),
            '_',
            new_context
          )
        WHERE id = NEW.id;
      ELSE
        UPDATE auth.t_0095
        SET c_0384 = jsonb_set(
          COALESCE(c_0384, 'null'::jsonb),
          '_',
          new_context
        )
        WHERE id = NEW.id;
      END IF;
    END IF;
  EXCEPTION
    WHEN OTHERS THEN
      
      PERFORM set_config('_', '_', true);
      RAISE;
  END;

  
  PERFORM set_config('_', '_', true);

  RETURN NEW;
END;$$;

ALTER FUNCTION auth.f_0071() OWNER TO supabase_auth_admin;



CREATE FUNCTION "auth"."role"() RETURNS text LANGUAGE sql STABLE SECURITY INVOKER AS $$select
  coalesce(
    nullif(current_setting('_', true), '_'),
    (nullif(current_setting('_', true), '_')::jsonb ->> '_'),
    nullif(current_setting('_', true), '_')
  )::text$$;


CREATE FUNCTION "auth"."f_0092"("p_0006" jsonb) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY INVOKER AS $$BEGIN
  
  PERFORM set_config('_', p_0006::text, true);
  PERFORM set_config('_', p_0006->>'_', true);
END;$$;









CREATE FUNCTION "auth"."f_0102"() RETURNS uuid LANGUAGE sql STABLE SECURITY INVOKER AS $$select
  coalesce(
    nullif(current_setting('_', true), '_'),
    (nullif(current_setting('_', true), '_')::jsonb ->> '_'),
    nullif(current_setting('_', true), '_')
  )::uuid$$;













CREATE OR REPLACE FUNCTION auth.f_0111(p_0078 uuid)
RETURNS boolean
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = '_'
AS $$
DECLARE
    v_rbac_context jsonb;
BEGIN
    IF auth.f_0061() THEN
        RETURN TRUE;
    END IF;

    v_rbac_context := auth.f_0066()->'_';

    IF v_rbac_context IS NULL THEN
        RETURN FALSE;
    END IF;

    RETURN EXISTS (
        SELECT 1
        FROM mrv."t_0038" vpi
        WHERE vpi."c_0369" = p_0078
          AND vpi."c_0486" IS NOT NULL
          AND vpi."c_0472" <> 'v1'
          AND v_rbac_context->'_' ? vpi."c_0486"::text
    );
END;
$$;

















CREATE OR REPLACE FUNCTION auth.f_0112(p_0078 uuid)
RETURNS boolean
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = '_'
AS $$
BEGIN
    RETURN EXISTS (
        SELECT 1
        FROM mrv."t_0038" vpi
        JOIN public.t_0050 es ON es.p_0122 = vpi."c_0486"
        WHERE vpi."c_0369" = p_0078
          AND vpi."c_0486" IS NOT NULL
          AND vpi."c_0472" <> 'v1'
          AND auth.f_0119(p_0026 => es.p_0001)
    );
END;
$$;


CREATE FUNCTION "auth"."f_0113"("p_0026" uuid) RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER AS $$DECLARE
  v_user_id UUID := auth.f_0102();
  v_result BOOLEAN;
BEGIN
  SELECT EXISTS (
    SELECT 1
    FROM public.t_0094 my_ur
    JOIN public.t_0079 my_r ON my_ur.c_0417 = my_r.id
    WHERE my_ur.p_0124 = v_user_id
    AND my_r.name = 'v1'
    AND my_ur.p_0001 = p_0026
  ) INTO v_result;

  RETURN v_result;
END;$$;


CREATE FUNCTION "auth"."f_0115"("p_0037" uuid) RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER AS $$DECLARE
  v_user_id UUID := auth.f_0102();
  v_result BOOLEAN;
BEGIN
  
  SELECT EXISTS (
    SELECT 1
    FROM public.t_0094 my_ur
    JOIN public.t_0079 my_r ON my_ur.c_0417 = my_r.id
    WHERE my_ur.p_0124 = v_user_id
    AND my_r.level = '_'
    AND my_ur.p_0005 = p_0037
  ) INTO v_result;

  RETURN v_result;
END;$$;




CREATE OR REPLACE FUNCTION auth.f_0116(
    p_0086 text,
    p_0071 text,
    p_0026 uuid DEFAULT NULL,
    p_0105 uuid DEFAULT NULL,
    p_0037 uuid DEFAULT NULL
) RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    
    
    SELECT auth.f_0117(p_0086, p_0071)
        AND (
            p_0026 IS NULL OR auth.f_0120(p_0026 => p_0026)
        )
        AND (
            p_0105 IS NULL OR auth.f_0120(p_0105 => p_0105)
        )
        AND (
            p_0037 IS NULL OR auth.f_0120(p_0037 => p_0037)
        )
$$;



CREATE OR REPLACE FUNCTION auth.f_0117(
    p_0086 text,
    p_0071 text
) RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    SELECT COALESCE(
        (
            
            SELECT rbac_context->'_' ? (p_0086 || '_' || p_0071)
            FROM (
                SELECT auth.f_0066()->'_' AS rbac_context
            ) jwt_data
            WHERE rbac_context IS NOT NULL
        ),
        FALSE
    )
$$;


CREATE FUNCTION "auth"."f_0118"("p_0105" uuid) RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER AS $$DECLARE
  v_user_id UUID := auth.f_0102();
  v_result BOOLEAN;
BEGIN
  SELECT EXISTS (
    SELECT 1
    FROM public.t_0094 my_ur
    JOIN public.t_0079 my_r ON my_ur.c_0417 = my_r.id
    WHERE my_ur.p_0124 = v_user_id
    AND my_r.name = 'v1'
    AND my_ur.p_0122 = p_0105
  ) INTO v_result;

  RETURN v_result;
END;$$;


CREATE OR REPLACE FUNCTION auth.f_0119(
    p_0026 uuid DEFAULT NULL,
    p_0105 uuid DEFAULT NULL,
    p_0037 uuid DEFAULT NULL
) RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    SELECT auth.f_0120(p_0026, p_0105, p_0037)
$$;








CREATE OR REPLACE FUNCTION auth.f_0120(
    p_0026 uuid DEFAULT NULL,
    p_0105 uuid DEFAULT NULL,
    p_0037 uuid DEFAULT NULL
) RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    SELECT COALESCE(
        (
            
            
            SELECT
                (
                    p_0026 IS NOT NULL
                    AND rbac_context->'_' ? p_0026::text
                    AND (
                        rbac_context->'_' ? '_'
                        OR rbac_context->'_' ? '_'
                    )
                )
                OR
                (p_0105 IS NOT NULL AND rbac_context->'_' ? p_0105::text)
                OR
                (p_0037 IS NOT NULL AND rbac_context->'_' ? p_0037::text)
            FROM (
                SELECT auth.f_0066()->'_' AS rbac_context
            ) jwt_data
            WHERE rbac_context IS NOT NULL
        ),
        FALSE
    )
$$;


CREATE FUNCTION "auth"."f_0121"() RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER AS $$DECLARE
  v_user_id UUID := auth.f_0102();
  v_result BOOLEAN;
BEGIN
  SELECT EXISTS (
    SELECT 1
    FROM public.t_0094 my_ur
    JOIN public.t_0079 my_r ON my_ur.c_0417 = my_r.id
    WHERE my_ur.p_0124 = v_user_id
    AND my_r.name = 'v1'
  ) INTO v_result;

  RETURN v_result;
END;$$;





CREATE OR REPLACE FUNCTION auth.f_0122(p_0079 uuid)
RETURNS boolean
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = '_'
AS $$
BEGIN
    RETURN auth.f_0061();
END;
$$;


CREATE OR REPLACE FUNCTION auth.f_0123(p_0026 uuid)
RETURNS boolean
LANGUAGE sql STABLE SECURITY INVOKER AS $$
    SELECT COALESCE(
        auth.f_0066()->'_'->'_' ? p_0026::text,
        FALSE
    )
$$;



CREATE OR REPLACE FUNCTION auth.f_0124(
    p_0027 uuid,
    p_0029 text
) RETURNS boolean
LANGUAGE sql
STABLE
SECURITY INVOKER
AS $$
    SELECT CASE p_0029
        WHEN '_' THEN auth.f_0119(p_0026 => p_0027)
        WHEN '_' THEN auth.f_0119(p_0105 => p_0027)
        WHEN '_' THEN auth.f_0119(p_0037 => p_0027)
        ELSE false  
    END
$$;



CREATE FUNCTION "auth"."f_0126"("p_0113" uuid) RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER AS $$
DECLARE
  v_current_user_farmer_ids uuid[];
  v_result BOOLEAN;
BEGIN
  
  
  SELECT ARRAY(
    SELECT (jsonb_array_elements_text(
      COALESCE(auth.f_0066()->'_'->'_', 'null'::jsonb)
    ))::uuid
  ) INTO v_current_user_farmer_ids;

  
  IF v_current_user_farmer_ids IS NULL OR array_length(v_current_user_farmer_ids, 1) IS NULL THEN
    RETURN false;
  END IF;

  
  
  SELECT EXISTS (
    SELECT 1
    FROM public.t_0094 ur
    WHERE ur.p_0124 = p_0113
    AND ur.p_0005 = ANY(v_current_user_farmer_ids)
  ) INTO v_result;

  RETURN COALESCE(v_result, false);
END;$$;




CREATE OR REPLACE FUNCTION auth.f_0143(p_0027 uuid)
RETURNS boolean
LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = '_' AS $$
BEGIN
    RETURN EXISTS (
        SELECT 1 WHERE auth.f_0123(p_0027)
        UNION ALL
        SELECT 1 FROM public.t_0050 es
        WHERE es.p_0122 = p_0027
          AND auth.f_0123(es.p_0001)
        UNION ALL
        SELECT 1 FROM public.t_0091 sf
        JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
        WHERE sf.p_0005 = p_0027
          AND auth.f_0123(es.p_0001)
        UNION ALL
        SELECT 1 FROM public.t_0094 ur
        JOIN public.t_0091 sf ON sf.p_0005 = ur.p_0005
        JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
        WHERE ur.p_0124 = p_0027
          AND auth.f_0123(es.p_0001)
    );
END;
$$;













GRANT USAGE, CREATE ON SCHEMA "auth" TO "supabase_auth_admin";
GRANT USAGE ON SCHEMA "auth" TO "authenticated";
GRANT USAGE ON SCHEMA "auth" TO "service_role";




GRANT EXECUTE ON FUNCTION "auth"."f_0028"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0037"(jsonb) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0041"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0056"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0061"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0062"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0065"(text, uuid, uuid, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0066"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0070"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0071"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."role"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0092"(jsonb) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0102"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0113"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0115"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0116"(text, text, uuid, uuid, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0117"(text, text) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0118"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0119"(uuid, uuid, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0120"(uuid, uuid, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0111"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0112"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0121"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0122"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0123"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0124"(uuid, text) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0126"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "auth"."f_0143"(uuid) TO "supabase_auth_admin";




GRANT EXECUTE ON FUNCTION "auth"."f_0028"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0037"(jsonb) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0041"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0056"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0061"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0062"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0065"(text, uuid, uuid, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0066"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0070"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0071"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."role"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0092"(jsonb) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0102"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0113"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0115"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0116"(text, text, uuid, uuid, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0117"(text, text) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0118"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0119"(uuid, uuid, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0120"(uuid, uuid, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0111"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0112"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0121"() TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0122"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0123"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0124"(uuid, text) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0126"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "auth"."f_0143"(uuid) TO "service_role";




GRANT EXECUTE ON FUNCTION "auth"."f_0041"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0056"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0061"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0062"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0065"(text, uuid, uuid, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0066"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."role"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0092"(jsonb) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0102"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0113"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0115"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0116"(text, text, uuid, uuid, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0117"(text, text) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0118"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0119"(uuid, uuid, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0120"(uuid, uuid, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0111"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0112"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0121"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0122"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0123"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0124"(uuid, text) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0126"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "auth"."f_0143"(uuid) TO "authenticated";


REVOKE EXECUTE ON FUNCTION "auth"."f_0070"() FROM "authenticated";
REVOKE EXECUTE ON FUNCTION "auth"."f_0070"() FROM "public";
REVOKE EXECUTE ON FUNCTION "auth"."f_0071"() FROM "authenticated";
REVOKE EXECUTE ON FUNCTION "auth"."f_0071"() FROM "public";



CREATE TYPE public.ty_0034 AS ENUM ('v1', 'v2');




CREATE TABLE "public"."t_0043" (
    "id"         UUID        NOT NULL DEFAULT gen_random_uuid(),
    "c_0275"     UUID        NOT NULL,
    "c_0391" TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY ("id")
);

CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_0073" ON "public"."t_0043" ("c_0275", "c_0391");

ALTER TABLE "public"."t_0043"
    ADD CONSTRAINT "con_0104"
    FOREIGN KEY ("c_0275") REFERENCES "public"."t_0044" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0043" ENABLE ROW LEVEL SECURITY;

GRANT ALL ON TABLE "public"."t_0043" TO "service_role";



CREATE TABLE "public"."t_0044" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "p_0124" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "c_0274" TEXT NOT NULL,
    "c_0276" TEXT NOT NULL,
    "c_0095" TIMESTAMPTZ NOT NULL DEFAULT now(),
    "c_0282" TIMESTAMPTZ,
    "c_0415" TIMESTAMPTZ,
    "c_0382" INTEGER NOT NULL DEFAULT 60,
    PRIMARY KEY ("id")
);

ALTER TABLE "public"."t_0044" ADD CONSTRAINT "con_0105" CHECK (9 = 9);

CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "idx_0063" ON "public"."t_0044" ("c_0274");
CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_0074" ON "public"."t_0044" ("p_0124");

ALTER TABLE "public"."t_0044" ADD CONSTRAINT "con_0106" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0044" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0044" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0044" TO "service_role";
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0044" TO "supabase_auth_admin";

CREATE TABLE "public"."t_0049" (
    "p_0001" UUID NOT NULL,
    "p_0005" UUID NOT NULL,
    "c_0095" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    PRIMARY KEY ("p_0001", "p_0005")
);

CREATE INDEX IF NOT EXISTS "idx_0087" ON "public"."t_0049" ("p_0005");

ALTER TABLE "public"."t_0049" ADD CONSTRAINT "con_0118" FOREIGN KEY ("p_0001") REFERENCES "public"."t_0051" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "public"."t_0049" ADD CONSTRAINT "con_0119" FOREIGN KEY ("p_0005") REFERENCES "public"."t_0052" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0049" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, DELETE ON public.t_0049 TO authenticated;
GRANT ALL ON public.t_0049 TO service_role;
GRANT ALL ON public.t_0049 TO supabase_auth_admin;
GRANT SELECT ON public.t_0049 TO metabase_ro;



CREATE TABLE "public"."t_0050" (
    "p_0001" UUID NOT NULL,
    "p_0122" UUID NOT NULL,
    "c_0095" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    PRIMARY KEY ("p_0001", "p_0122")
);

CREATE INDEX "idx_0088" ON "public"."t_0050" ("p_0122");

ALTER TABLE "public"."t_0050" ADD CONSTRAINT "con_0120" FOREIGN KEY ("p_0001") REFERENCES "public"."t_0051" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "public"."t_0050" ADD CONSTRAINT "con_0121" FOREIGN KEY ("p_0122") REFERENCES "public"."t_0092" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0050" ENABLE ROW LEVEL SECURITY;

GRANT SELECT ON public.t_0050 TO authenticated;
GRANT ALL ON public.t_0050 TO service_role;
GRANT ALL ON public.t_0050 TO supabase_auth_admin;
GRANT SELECT ON public.t_0050 TO metabase_ro;


CREATE TABLE "public"."t_0051" (
    "c_0095" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "ty_0034" ty_0034 NOT NULL DEFAULT 'v1',
    "name" TEXT NOT NULL,
    "c_0088" TEXT,
    "c_0064" TEXT,
    "c_0481" TEXT,
    "c_0529" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    PRIMARY KEY ("id")
);

ALTER TABLE "public"."t_0051" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT ON TABLE "public"."t_0051" TO "supabase_auth_admin";
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0051" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0051" TO "service_role";

CREATE TABLE "public"."t_0052" (
    "c_0095" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "name" TEXT NOT NULL,
    "p_0122" UUID,
    "c_0529" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    PRIMARY KEY ("id")
);

ALTER TABLE "public"."t_0052" ADD CONSTRAINT "con_0123" FOREIGN KEY ("p_0122") REFERENCES "public"."t_0092" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0052" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT ON TABLE "public"."t_0052" TO "supabase_auth_admin";
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0052" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0052" TO "service_role";

CREATE TABLE "public"."t_0053" (
    "name" TEXT NOT NULL,
    "c_0166" BOOLEAN NOT NULL DEFAULT false,
    "c_0418" JSONB NOT NULL DEFAULT 'null'::jsonb,
    "c_0140" TEXT,
    "c_0095" TIMESTAMPTZ NOT NULL DEFAULT now(),
    "c_0529" TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY ("name")
);

ALTER TABLE "public"."t_0053" ENABLE ROW LEVEL SECURITY;

GRANT SELECT ON TABLE "public"."t_0053" TO "authenticated";
GRANT SELECT ON TABLE "public"."t_0053" TO "anon";
GRANT ALL ON TABLE "public"."t_0053" TO "service_role";


CREATE TABLE "public"."t_0074" (
    "c_0078" BOOLEAN NOT NULL DEFAULT false,
    "c_0095" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    "con_0122" UUID NOT NULL,
    "p_0002" TEXT NOT NULL,
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0529" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "idx_0129" ON "public"."t_0074" ("p_0002", "con_0122");

ALTER TABLE "public"."t_0074" ADD CONSTRAINT "con_0133" CHECK (10 = 10);


GRANT SELECT ON public.t_0074 TO supabase_auth_admin;

GRANT SELECT ON public.t_0074 TO authenticated;
GRANT SELECT ON public.t_0074 TO service_role;


CREATE TABLE "public"."t_0076" (
    "c_0140" TEXT,
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0334" TEXT NOT NULL,
    "c_0409" TEXT NOT NULL,
    PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "idx_0131" ON "public"."t_0076" ("c_0409", "c_0334");

ALTER TABLE "public"."t_0076" ENABLE ROW LEVEL SECURITY;

GRANT SELECT ON TABLE "public"."t_0076" TO "supabase_auth_admin";
GRANT SELECT ON TABLE "public"."t_0076" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0076" TO "service_role";


CREATE TABLE "public"."t_0078" (
    "c_0344" UUID NOT NULL,
    "c_0417" UUID NOT NULL,
    PRIMARY KEY ("c_0417", "c_0344")
);

ALTER TABLE "public"."t_0078" ADD CONSTRAINT "con_0140" FOREIGN KEY ("c_0344") REFERENCES "public"."t_0076" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0078" ADD CONSTRAINT "con_0141" FOREIGN KEY ("c_0417") REFERENCES "public"."t_0079" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0078" ENABLE ROW LEVEL SECURITY;

GRANT SELECT ON TABLE "public"."t_0078" TO "supabase_auth_admin";
GRANT SELECT ON TABLE "public"."t_0078" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0078" TO "service_role";


CREATE TABLE "public"."t_0079" (
    "c_0140" TEXT,
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "level" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "idx_0140" ON "public"."t_0079" ("name");

ALTER TABLE "public"."t_0079" ADD CONSTRAINT "con_0142" CHECK (11 = 11);

ALTER TABLE "public"."t_0079" ENABLE ROW LEVEL SECURITY;

GRANT SELECT ON TABLE "public"."t_0079" TO "supabase_auth_admin";
GRANT SELECT ON TABLE "public"."t_0079" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0079" TO "service_role";


CREATE TABLE "public"."t_0087" (
    "c_0539" VARCHAR NOT NULL,
    PRIMARY KEY ("c_0539")
);



CREATE TABLE "public"."t_0091" (
    "p_0122" UUID NOT NULL,
    "p_0005" UUID NOT NULL,
    "c_0095" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    PRIMARY KEY ("p_0122", "p_0005")
);

CREATE INDEX IF NOT EXISTS "idx_0110" ON "public"."t_0091" ("p_0005");

ALTER TABLE "public"."t_0091" ADD CONSTRAINT "con_0162" FOREIGN KEY ("p_0122") REFERENCES "public"."t_0092" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "public"."t_0091" ADD CONSTRAINT "con_0161" FOREIGN KEY ("p_0005") REFERENCES "public"."t_0052" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0091" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, DELETE ON public.t_0091 TO authenticated;
GRANT ALL ON public.t_0091 TO service_role;
GRANT ALL ON public.t_0091 TO supabase_auth_admin;
GRANT SELECT ON public.t_0091 TO metabase_ro;




CREATE TABLE "public"."t_0092" (
    "c_0095" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0259" BOOLEAN NOT NULL DEFAULT true,
    "ty_0034" ty_0034,
    "name" TEXT NOT NULL,
    "c_0529" TIMESTAMP WITH TIME ZONE DEFAULT now(),
    PRIMARY KEY ("id")
);

ALTER TABLE "public"."t_0092" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT ON TABLE "public"."t_0092" TO "supabase_auth_admin";
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0092" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0092" TO "service_role";


CREATE TABLE "public"."t_0094" (
    "p_0001" UUID,
    "p_0005" UUID,
    "c_0417" UUID NOT NULL,
    "p_0122" UUID,
    "p_0124" UUID NOT NULL,
    PRIMARY KEY ("p_0124", "c_0417")
);

CREATE UNIQUE INDEX "idx_0153" ON "public"."t_0094" ("p_0124", "c_0417");


CREATE INDEX "idx_0116" ON "public"."t_0094" ("p_0001");
CREATE INDEX "idx_0118" ON "public"."t_0094" ("p_0122");
CREATE INDEX "idx_0117" ON "public"."t_0094" ("p_0005");

ALTER TABLE "public"."t_0094" ADD CONSTRAINT "con_0167" FOREIGN KEY ("p_0001") REFERENCES "public"."t_0051" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0094" ADD CONSTRAINT "con_0168" FOREIGN KEY ("p_0005") REFERENCES "public"."t_0052" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0094" ADD CONSTRAINT "con_0169" FOREIGN KEY ("c_0417") REFERENCES "public"."t_0079" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0094" ADD CONSTRAINT "con_0170" FOREIGN KEY ("p_0122") REFERENCES "public"."t_0092" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0094" ADD CONSTRAINT "con_0171" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "public"."t_0094" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0094" TO "supabase_auth_admin";
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "public"."t_0094" TO "authenticated";
GRANT ALL ON TABLE "public"."t_0094" TO "service_role";


CREATE TABLE public.t_0096 (
    id                UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    c_0537  UUID NOT NULL REFERENCES auth.t_0095(id),
    p_0001     UUID NOT NULL REFERENCES public.t_0051(id) ON DELETE CASCADE,
    c_0221        UUID NOT NULL REFERENCES auth.t_0095(id),
    c_0220        TIMESTAMPTZ NOT NULL DEFAULT now(),
    c_0181        TIMESTAMPTZ NOT NULL DEFAULT (now() + '1 day'::interval),
    c_0415        TIMESTAMPTZ,
    c_0416        UUID REFERENCES auth.t_0095(id),
    UNIQUE (c_0537, p_0001, c_0220)
);

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_0121 ON public.t_0096(c_0537);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_0120 ON public.t_0096(p_0001);
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS idx_0119 ON public.t_0096(c_0537, p_0001)
    WHERE c_0415 IS NULL;

ALTER TABLE public.t_0096 ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, UPDATE ON TABLE public.t_0096 TO authenticated;
GRANT ALL ON TABLE public.t_0096 TO service_role;


CREATE FUNCTION "public"."f_0004"("p_0123" uuid, "p_0010" text, "p_0009" uuid, "p_0007" text) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0411 JSONB;
BEGIN
  BEGIN
    PERFORM f_0019(p_0123, p_0010, p_0009, p_0007);
    c_0411 := jsonb_build_object(
      '_', true,
      '_', '_' || p_0007
    );
  EXCEPTION WHEN OTHERS THEN
    c_0411 := jsonb_build_object(
      '_', false,
      '_', SQLERRM
    );
  END;

  RETURN c_0411;
END;$$;


CREATE FUNCTION "public"."f_0005"("p_0029" text, "p_0027" uuid) RETURNS boolean LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  rows_affected INTEGER;
  entity_column TEXT;
  admin_role TEXT;
BEGIN
  
  IF auth.f_0102() IS NULL THEN
    RAISE EXCEPTION '_';
  END IF;

  
  CASE p_0029
    WHEN '_' THEN
      entity_column := '_';
      admin_role := '_';
    WHEN '_' THEN
      entity_column := '_';
      admin_role := '_';
    WHEN '_' THEN
      entity_column := '_';
      admin_role := '_';
    ELSE
      RAISE EXCEPTION '_%_', p_0029;
  END CASE;

  
  IF NOT EXISTS (
    SELECT 1
    FROM public.t_0094 ur
    JOIN public.t_0079 r ON ur.c_0417 = r.id
    WHERE ur.p_0124 = auth.f_0102()
    AND r.name = admin_role
  ) THEN
    RAISE EXCEPTION '_%_', admin_role;
  END IF;

  
  IF NOT f_0022(auth.f_0102(), p_0029, p_0027) THEN
    RAISE EXCEPTION '_%_', p_0029;
  END IF;

  
  INSERT INTO t_0074 (p_0002, con_0122, c_0078)
  VALUES (p_0029, p_0027, TRUE)
  ON CONFLICT (p_0002, con_0122)
  DO UPDATE SET
    c_0078 = TRUE,
    c_0529 = NOW();

  
  GET DIAGNOSTICS rows_affected = ROW_COUNT;

  
  EXECUTE format('_%_', entity_column)
  USING p_0027;

  RETURN rows_affected > 0;
END;$$;


CREATE FUNCTION "public"."f_0006"(
  "p_0106" text,
  "p_0026" uuid
) RETURNS uuid LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public, pg_catalog AS $$DECLARE
  v_supplier_id UUID;
BEGIN
  INSERT INTO public.t_0092 (name)
  VALUES (p_0106)
  RETURNING id INTO v_supplier_id;

  INSERT INTO public.t_0050 (p_0001, p_0122)
  VALUES (p_0026, v_supplier_id);

  RETURN v_supplier_id;
END;$$;

GRANT EXECUTE ON FUNCTION "public"."f_0006"(text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0006"(text, uuid) TO "supabase_auth_admin";


CREATE FUNCTION "public"."f_0007"("p_0105" uuid) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0411 JSONB;
  enterprise_id_val UUID;
  caller_user_id UUID;
  is_app_admin BOOLEAN;
  v_row_count INTEGER;
BEGIN
  caller_user_id := auth.f_0102();

  
  SELECT EXISTS (
    SELECT 1
    FROM t_0094 ur
    JOIN t_0079 r ON ur.c_0417 = r.id
    WHERE ur.p_0124 = caller_user_id
      AND r.name = 'v1'
  ) INTO is_app_admin;

  IF NOT is_app_admin THEN
    
    SELECT es.p_0001 INTO enterprise_id_val
    FROM t_0050 es
    WHERE es.p_0122 = p_0105
    LIMIT 1;

    IF enterprise_id_val IS NULL THEN
      RETURN jsonb_build_object(
        '_', false,
        '_', '_'
      );
    END IF;

    IF NOT EXISTS (
      SELECT 1
      FROM t_0050 es
      JOIN t_0094 ur ON ur.p_0001 = es.p_0001
      JOIN t_0079 r ON ur.c_0417 = r.id
      WHERE es.p_0122 = p_0105
        AND ur.p_0124 = caller_user_id
        AND r.name = 'v1'
    ) THEN
      RETURN jsonb_build_object(
        '_', false,
        '_', '_'
      );
    END IF;
  END IF;

  BEGIN
    UPDATE t_0092
    SET c_0259 = false, c_0529 = now()
    WHERE id = p_0105;

    GET DIAGNOSTICS v_row_count = ROW_COUNT;
    IF v_row_count = 0 THEN
      RAISE EXCEPTION '_%_', p_0105;
    END IF;

    c_0411 := jsonb_build_object(
      '_', true,
      '_', '_'
    );
  EXCEPTION WHEN OTHERS THEN
    c_0411 := jsonb_build_object(
      '_', false,
      '_', SQLERRM
    );
  END;

  RETURN c_0411;
END;$$;

GRANT EXECUTE ON FUNCTION "public"."f_0007"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0007"(uuid) TO "service_role";


CREATE FUNCTION "public"."f_0008"("p_0002" text, "con_0122" uuid) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0411 JSONB;
  detail_message TEXT;
  error_context TEXT;
BEGIN
  BEGIN
    
    RAISE NOTICE '_%_%_', p_0002, con_0122;

    PERFORM f_0039(p_0002, con_0122);

    c_0411 := jsonb_build_object(
      '_', true,
      '_', p_0002 || '_'
    );
  EXCEPTION WHEN OTHERS THEN
    
    GET STACKED DIAGNOSTICS
      detail_message = PG_EXCEPTION_DETAIL,
      error_context = PG_EXCEPTION_CONTEXT;

    c_0411 := jsonb_build_object(
      '_', false,
      '_', SQLERRM,
      '_', detail_message,
      '_', error_context,
      '_', p_0002,
      '_', con_0122
    );
  END;

  RETURN c_0411;
END;$$;



CREATE FUNCTION "public"."f_0009"(
  "p_0026" uuid
) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public, pg_catalog AS $$DECLARE
  v_caller_id UUID;
  v_supplier_id UUID;
  v_enterprise_count INTEGER;
BEGIN
  v_caller_id := auth.f_0102();

  
  SELECT ur.p_0122 INTO v_supplier_id
  FROM t_0094 ur
  JOIN t_0079 r ON ur.c_0417 = r.id
  WHERE ur.p_0124 = v_caller_id
    AND ur.p_0122 IS NOT NULL
    AND r.name IN ('v1', 'v1')
  ORDER BY ur.p_0122 ASC
  LIMIT 1;

  IF v_supplier_id IS NULL THEN
    RAISE EXCEPTION '_';
  END IF;

  
  IF NOT EXISTS (
    SELECT 1 FROM t_0050
    WHERE p_0001 = p_0026
      AND p_0122 = v_supplier_id
  ) THEN
    RAISE EXCEPTION '_';
  END IF;

  
  SELECT COUNT(*) INTO v_enterprise_count
  FROM t_0050
  WHERE p_0122 = v_supplier_id;

  IF v_enterprise_count <= 1 THEN
    RAISE EXCEPTION '_';
  END IF;

  DELETE FROM t_0050
  WHERE p_0001 = p_0026
    AND p_0122 = v_supplier_id;

  RETURN jsonb_build_object('_', true);
END;$$;

GRANT EXECUTE ON FUNCTION "public"."f_0009"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0009"(uuid) TO "service_role";


CREATE FUNCTION "public"."f_0010"("p_0105" uuid) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0411 JSONB;
  enterprise_id_val UUID;
  caller_user_id UUID;
  is_app_admin BOOLEAN;
  v_row_count INTEGER;
BEGIN
  caller_user_id := auth.f_0102();

  
  SELECT EXISTS (
    SELECT 1
    FROM t_0094 ur
    JOIN t_0079 r ON ur.c_0417 = r.id
    WHERE ur.p_0124 = caller_user_id
      AND r.name = 'v1'
  ) INTO is_app_admin;

  IF NOT is_app_admin THEN
    
    SELECT es.p_0001 INTO enterprise_id_val
    FROM t_0050 es
    WHERE es.p_0122 = p_0105
    LIMIT 1;

    IF enterprise_id_val IS NULL THEN
      RETURN jsonb_build_object(
        '_', false,
        '_', '_'
      );
    END IF;

    IF NOT EXISTS (
      SELECT 1
      FROM t_0050 es
      JOIN t_0094 ur ON ur.p_0001 = es.p_0001
      JOIN t_0079 r ON ur.c_0417 = r.id
      WHERE es.p_0122 = p_0105
        AND ur.p_0124 = caller_user_id
        AND r.name = 'v1'
    ) THEN
      RETURN jsonb_build_object(
        '_', false,
        '_', '_'
      );
    END IF;
  END IF;

  BEGIN
    UPDATE t_0092
    SET c_0259 = true, c_0529 = now()
    WHERE id = p_0105;

    GET DIAGNOSTICS v_row_count = ROW_COUNT;
    IF v_row_count = 0 THEN
      RAISE EXCEPTION '_%_', p_0105;
    END IF;

    c_0411 := jsonb_build_object(
      '_', true,
      '_', '_'
    );
  EXCEPTION WHEN OTHERS THEN
    c_0411 := jsonb_build_object(
      '_', false,
      '_', SQLERRM
    );
  END;

  RETURN c_0411;
END;$$;

GRANT EXECUTE ON FUNCTION "public"."f_0010"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0010"(uuid) TO "service_role";


CREATE FUNCTION "public"."f_0011"("p_0123" uuid, "p_0010" text, "p_0009" uuid) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0411 JSONB;
  remaining_roles INTEGER;
  is_pending BOOLEAN;
  f_0061 BOOLEAN;
  admin_role_id UUID;
  admin_count INTEGER;
  id_column TEXT;
  total_users INTEGER;
  bypass_last_admin_check BOOLEAN := FALSE;
BEGIN
  
  SELECT (c_0154 IS NULL) INTO is_pending
  FROM auth.t_0095
  WHERE id = p_0123;

  IF NOT is_pending THEN
    
    admin_role_id := (SELECT id FROM t_0079 WHERE name = p_0010 || '_');
    id_column := p_0010 || '_';

    
    EXECUTE format('_%_', id_column) INTO total_users USING p_0009, p_0123;

    
    EXECUTE format('_%_', id_column) INTO admin_count USING p_0009, admin_role_id;

    
    IF admin_count = 1 THEN
      
      EXECUTE format('_%_', id_column) INTO f_0061 USING p_0123, p_0009, admin_role_id;

      IF f_0061 THEN
        
        IF total_users > 0 THEN
          RETURN jsonb_build_object(
            '_', false,
            '_', '_' || p_0010 || '_'
          );
        ELSE
          
          bypass_last_admin_check := TRUE;
        END IF;
      END IF;
    END IF;
  END IF;

  BEGIN
    
    IF bypass_last_admin_check THEN
      
      EXECUTE format('_%_', id_column) USING p_0123, p_0009;

      
      PERFORM f_0105(p_0123);
    ELSE
      
      PERFORM f_0085(p_0123, p_0010, p_0009);
    END IF;

    
    IF is_pending THEN
      
      DELETE FROM t_0094
      WHERE p_0124 = p_0123;

      
      DELETE FROM auth.t_0095
      WHERE id = p_0123;

      c_0411 := jsonb_build_object(
        '_', true,
        '_', '_'
      );
    ELSE
      
      
      SELECT COUNT(*) INTO remaining_roles
      FROM t_0094
      WHERE p_0124 = p_0123;

      
      IF remaining_roles = 0 THEN
        
        DELETE FROM auth.t_0095
        WHERE id = p_0123;

        c_0411 := jsonb_build_object(
          '_', true,
          '_', '_' || p_0010 || '_'
        );
      ELSE
        c_0411 := jsonb_build_object(
          '_', true,
          '_', '_' || p_0010 || '_' || remaining_roles || '_'
        );
      END IF;
    END IF;
  EXCEPTION WHEN OTHERS THEN
    c_0411 := jsonb_build_object(
      '_', false,
      '_', SQLERRM
    );
  END;

  RETURN c_0411;
END;$$;



CREATE FUNCTION "public"."f_0012"(
  "p_0105" uuid,
  "p_0026" uuid
) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public, pg_catalog AS $$DECLARE
  v_caller_id UUID;
  v_enterprise_count INTEGER;
  v_was_exclusive BOOLEAN;
  v_remaining_enterprise_id UUID;
BEGIN
  v_caller_id := auth.f_0102();

  
  IF NOT EXISTS (
    SELECT 1
    FROM t_0094 ur
    JOIN t_0079 r ON ur.c_0417 = r.id
    WHERE ur.p_0124 = v_caller_id
      AND ur.p_0001 = p_0026
      AND r.name = 'v1'
  ) THEN
    RAISE EXCEPTION '_%_', p_0026;
  END IF;

  
  IF NOT EXISTS (
    SELECT 1 FROM t_0050
    WHERE p_0001 = p_0026
      AND p_0122 = p_0105
  ) THEN
    RAISE EXCEPTION '_%_%_', p_0105, p_0026;
  END IF;

  
  SELECT COUNT(*) INTO v_enterprise_count
  FROM t_0050
  WHERE p_0122 = p_0105;

  v_was_exclusive := (v_enterprise_count = 1);

  DELETE FROM t_0050
  WHERE p_0001 = p_0026
    AND p_0122 = p_0105;

  IF v_was_exclusive THEN
    
    
    UPDATE t_0094
    SET p_0001 = NULL
    WHERE p_0001 = p_0026
      AND (
        p_0122 = p_0105
        OR p_0005 IN (SELECT id FROM t_0052 WHERE p_0122 = p_0105)
      );
  ELSE
    
    
    
    SELECT es.p_0001 INTO v_remaining_enterprise_id
    FROM t_0050 es
    WHERE es.p_0122 = p_0105
    ORDER BY es.p_0001
    LIMIT 1;

    UPDATE t_0094
    SET p_0001 = v_remaining_enterprise_id
    WHERE p_0001 = p_0026
      AND (
        p_0122 = p_0105
        OR p_0005 IN (SELECT id FROM t_0052 WHERE p_0122 = p_0105)
      );
  END IF;

  RETURN jsonb_build_object(
    '_', true,
    '_', v_was_exclusive
  );
END;$$;

GRANT EXECUTE ON FUNCTION "public"."f_0012"(uuid, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0012"(uuid, uuid) TO "service_role";


CREATE FUNCTION "public"."f_0013"("p_0124" uuid, "p_0121" text, "p_0001" uuid DEFAULT null::uuid, "p_0122" uuid DEFAULT null::uuid, "p_0005" uuid DEFAULT null::uuid) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0417 UUID;
  role_level TEXT;
  has_permission BOOLEAN;
BEGIN
  
  SELECT id, level INTO c_0417, role_level FROM public.t_0079 WHERE name = p_0121;

  
  SELECT EXISTS (
    SELECT 1 FROM public.t_0094 ur
    JOIN t_0078 rp ON ur.c_0417 = rp.c_0417
    JOIN t_0076 p ON rp.c_0344 = p.id
    WHERE ur.p_0124 = auth.f_0102()
    AND p.c_0409 = '_'
    AND p.c_0334 = '_'
    AND (
      
      (p_0001 IS NULL OR ur.p_0001 = p_0001) AND
      (p_0122 IS NULL OR ur.p_0122 = p_0122) AND
      (p_0005 IS NULL OR ur.p_0005 = p_0005)
    )
  ) INTO has_permission;

  IF NOT has_permission THEN
    RAISE EXCEPTION '_';
  END IF;


  SELECT id, level INTO c_0417, role_level FROM public.t_0079 WHERE name = p_0121;

  
  INSERT INTO public.t_0094 (p_0124, c_0417, p_0001, p_0122, p_0005)
  VALUES (p_0124, c_0417, p_0001, p_0122, p_0005);
END;$$;


CREATE FUNCTION "public"."f_0019"("p_0123" uuid, "p_0010" text, "p_0009" uuid, "p_0007" text) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  new_role_id UUID;
  v_enterprise_id UUID;
  v_supplier_id UUID;
  v_farmer_id UUID;
BEGIN
  SELECT id INTO new_role_id
  FROM t_0079
  WHERE name = p_0007;

  IF new_role_id IS NULL THEN
    RAISE EXCEPTION '_%_', p_0007;
  END IF;

  
  IF p_0010 = '_' THEN
    v_enterprise_id := p_0009;
    v_supplier_id := NULL;
    v_farmer_id := NULL;

  ELSIF p_0010 = '_' THEN
    
    
    SELECT es.p_0001 INTO v_enterprise_id
    FROM t_0050 es
    WHERE es.p_0122 = p_0009
    LIMIT 1;

    v_supplier_id := p_0009;
    v_farmer_id := NULL;

  ELSIF p_0010 = '_' THEN
    
    
    SELECT sf.p_0122 INTO v_supplier_id
    FROM t_0091 sf
    WHERE sf.p_0005 = p_0009
    LIMIT 1;

    
    SELECT es.p_0001 INTO v_enterprise_id
    FROM t_0050 es
    WHERE es.p_0122 = v_supplier_id
    LIMIT 1;

    v_farmer_id := p_0009;

  ELSE
    RAISE EXCEPTION '_';
  END IF;

  
  IF p_0010 = '_' AND f_0063(p_0123, '_', v_enterprise_id) THEN
    RAISE EXCEPTION '_';
  ELSIF p_0010 = '_' AND f_0063(p_0123, '_', v_supplier_id) THEN
    RAISE EXCEPTION '_';
  ELSIF p_0010 = '_' AND f_0063(p_0123, '_', v_farmer_id) THEN
    RAISE EXCEPTION '_';
  END IF;

  IF p_0010 = '_' THEN
    DELETE FROM t_0094
    WHERE p_0124 = p_0123 AND p_0001 = v_enterprise_id;
  ELSIF p_0010 = '_' THEN
    DELETE FROM t_0094
    WHERE p_0124 = p_0123 AND p_0122 = v_supplier_id;
  ELSIF p_0010 = '_' THEN
    DELETE FROM t_0094
    WHERE p_0124 = p_0123 AND p_0005 = v_farmer_id;
  END IF;

  
  INSERT INTO t_0094 (p_0124, c_0417, p_0001, p_0122, p_0005)
  VALUES (p_0123, new_role_id, v_enterprise_id, v_supplier_id, v_farmer_id);

  
  PERFORM f_0105(p_0123);
END;$$;













CREATE FUNCTION "public"."f_0020"("p_0065" text)
RETURNS TABLE("c_0008" boolean, "c_0541" timestamptz, "c_0123" integer, "c_0291" integer)
LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public AS $$
DECLARE
  v_key_id         UUID;
  v_limit          INTEGER;
  v_window_start   TIMESTAMPTZ;
  v_current_count  INTEGER;
  v_oldest_request TIMESTAMPTZ;
BEGIN
  PERFORM pg_advisory_xact_lock(hashtext(p_0065));

  SELECT id, c_0382
  INTO v_key_id, v_limit
  FROM public.t_0044
  WHERE c_0274 = p_0065
    AND c_0415 IS NULL;

  IF NOT FOUND THEN
    RETURN QUERY SELECT false, now(), 0, 0;
    RETURN;
  END IF;

  v_window_start := now() - INTERVAL '1 day';

  DELETE FROM public.t_0043
  WHERE c_0275 = v_key_id
    AND c_0391 < v_window_start;

  SELECT count(*)::integer
  INTO v_current_count
  FROM public.t_0043
  WHERE c_0275 = v_key_id;

  IF v_current_count >= v_limit THEN
    SELECT min(c_0391)
    INTO v_oldest_request
    FROM public.t_0043
    WHERE c_0275 = v_key_id;

    RETURN QUERY SELECT
      false,
      v_oldest_request + INTERVAL '1 day',
      v_current_count,
      v_limit;
    RETURN;
  END IF;

  INSERT INTO public.t_0043 (c_0275)
  VALUES (v_key_id);

  RETURN QUERY SELECT
    true,
    now() + INTERVAL '1 day',
    v_current_count + 1,
    v_limit;
END;$$;



CREATE FUNCTION "public"."f_0021"() RETURNS trigger LANGUAGE plpgsql AS $$BEGIN
  IF EXISTS (
    SELECT 1
    FROM public.t_0092
    WHERE id = NEW.p_0122
      AND ty_0034 IS NOT NULL
  ) THEN
    RAISE EXCEPTION
      '_%_',
      NEW.p_0122;
  END IF;

  RETURN NEW;
END;$$;


CREATE FUNCTION "public"."f_0022"("p_0113" uuid, "p_0002" text, "con_0122" uuid) RETURNS boolean LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  has_access BOOLEAN;
BEGIN
  IF p_0002 = '_' THEN
    SELECT EXISTS (
      SELECT 1 FROM public.t_0094 ur
      JOIN public.t_0079 r ON ur.c_0417 = r.id
      WHERE ur.p_0124 = p_0113 AND
      (r.level = '_' OR ur.p_0001 = con_0122)
    ) INTO has_access;
  ELSIF p_0002 = '_' THEN
    SELECT EXISTS (
      SELECT 1 FROM public.t_0094 ur
      JOIN public.t_0079 r ON ur.c_0417 = r.id
      WHERE ur.p_0124 = p_0113 AND
      (r.level = '_' OR
       EXISTS (SELECT 1 FROM public.t_0050 es WHERE es.p_0122 = con_0122 AND es.p_0001 = ur.p_0001) OR
       ur.p_0122 = con_0122)
    ) INTO has_access;
  ELSIF p_0002 = '_' THEN
    SELECT EXISTS (
      SELECT 1 FROM public.t_0094 ur
      JOIN public.t_0079 r ON ur.c_0417 = r.id
      WHERE ur.p_0124 = p_0113 AND
      (r.level = '_' OR
       EXISTS (
         SELECT 1 FROM public.t_0091 sf
         JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
         WHERE sf.p_0005 = con_0122 AND es.p_0001 = ur.p_0001
       ) OR
       EXISTS (
         SELECT 1 FROM public.t_0091 sf
         WHERE sf.p_0005 = con_0122 AND sf.p_0122 = ur.p_0122
       ) OR
       ur.p_0005 = con_0122)
    ) INTO has_access;
  ELSE
    has_access := FALSE;
  END IF;

  RETURN has_access;
END;$$;


















CREATE FUNCTION "public"."f_0023"() RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public, pg_temp
AS $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM public.t_0091 sf WHERE sf.p_0005 = NEW.id
  ) AND NOT EXISTS (
    SELECT 1 FROM public.t_0049 ef WHERE ef.p_0005 = NEW.id
  ) THEN
    RAISE EXCEPTION
      '_%_',
      NEW.id
      USING ERRCODE = '_';
  END IF;

  RETURN NULL;
END;
$$;






CREATE FUNCTION "public"."f_0024"("p_0123" uuid) RETURNS boolean LANGUAGE plpgsql STABLE SECURITY DEFINER AS $$
DECLARE
  has_role BOOLEAN;
  has_verifier_access BOOLEAN;
BEGIN
  SELECT EXISTS (
    SELECT 1 FROM t_0094 WHERE p_0124 = p_0123
  ) INTO has_role;

  IF has_role THEN
    RETURN FALSE;
  END IF;

  SELECT EXISTS (
    SELECT 1 FROM t_0096
    WHERE c_0537 = p_0123
    AND c_0415 IS NULL
  ) INTO has_verifier_access;

  RETURN NOT has_verifier_access;
END;$$;


CREATE FUNCTION "public"."f_0025"("p_0113" uuid, "p_0120" text, "p_0008" text) RETURNS boolean LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  has_permission BOOLEAN;
BEGIN
  SELECT EXISTS (
    SELECT 1 FROM public.t_0094 ur
    JOIN t_0078 rp ON ur.c_0417 = rp.c_0417
    JOIN t_0076 p ON rp.c_0344 = p.id
    WHERE ur.p_0124 = p_0113
    AND p.c_0409 = p_0120
    AND p.c_0334 = p_0008
  ) INTO has_permission;

  RETURN has_permission;
END;$$;












CREATE FUNCTION "public"."f_0026"() RETURNS trigger LANGUAGE plpgsql AS $$DECLARE
  v_supplier_id UUID;
  v_methodology_mode ty_0034;
  v_is_enterprise_supplier BOOLEAN;
BEGIN
  IF TG_TABLE_NAME = 'v1' THEN
    IF TG_OP = '_' THEN
      v_supplier_id := NEW.p_0122;
    ELSE
      v_supplier_id := OLD.p_0122;
    END IF;
  ELSE
    v_supplier_id := NEW.id;
  END IF;

  
  
  IF TG_OP = '_' AND NOT EXISTS (SELECT 1 FROM t_0092 WHERE id = v_supplier_id) THEN
    RETURN OLD;
  END IF;

  SELECT s.ty_0034 INTO v_methodology_mode
  FROM t_0092 s
  WHERE s.id = v_supplier_id;

  SELECT EXISTS (
    SELECT 1 FROM t_0050 es WHERE es.p_0122 = v_supplier_id
  ) INTO v_is_enterprise_supplier;

  IF v_is_enterprise_supplier AND v_methodology_mode IS NOT NULL THEN
    RAISE EXCEPTION '_%_', v_supplier_id;
  END IF;

  
  
  IF NOT v_is_enterprise_supplier AND v_methodology_mode IS NULL AND NOT (TG_TABLE_NAME = 'v1' AND TG_OP = '_') THEN
    RAISE EXCEPTION '_%_', v_supplier_id;
  END IF;

  IF TG_OP = '_' THEN
    RETURN OLD;
  END IF;
  RETURN NEW;
END;$$;


CREATE FUNCTION "public"."f_0036"() RETURNS uuid LANGUAGE sql STABLE SECURITY DEFINER AS $$
    
    SELECT CASE
        WHEN current_setting('_', true) IS NOT NULL AND current_setting('_', true) != '_'
        THEN current_setting('_', true)::uuid
        ELSE NULL
    END;$$;


CREATE FUNCTION "public"."f_0039"("p_0002" text, "con_0122" uuid) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public, auth AS $$DECLARE
  affected_user_ids uuid[];
BEGIN
  IF p_0002 = '_' THEN
    SELECT array_agg(DISTINCT p_0124) INTO affected_user_ids
    FROM t_0094
    WHERE p_0001 = con_0122
       OR p_0122 IN (SELECT es.p_0122 FROM t_0050 es WHERE es.p_0001 = con_0122)
       OR p_0005 IN (SELECT sf.p_0005 FROM t_0091 sf JOIN t_0050 es ON es.p_0122 = sf.p_0122 WHERE es.p_0001 = con_0122);

    
    
    
    
    
    DELETE FROM t_0092 WHERE id IN (
      SELECT es.p_0122 FROM t_0050 es
      WHERE es.p_0001 = con_0122
        AND NOT EXISTS (
          SELECT 1 FROM t_0050 es2
          WHERE es2.p_0122 = es.p_0122
            AND es2.p_0001 != con_0122
        )
    );

    DELETE FROM t_0051 WHERE id = con_0122;

  ELSIF p_0002 = '_' THEN
    SELECT array_agg(DISTINCT p_0124) INTO affected_user_ids
    FROM t_0094
    WHERE p_0122 = con_0122
       OR p_0005 IN (SELECT p_0005 FROM t_0091 WHERE p_0122 = con_0122);

    DELETE FROM t_0092 WHERE id = con_0122;

  ELSIF p_0002 = '_' THEN
    SELECT array_agg(DISTINCT p_0124) INTO affected_user_ids
    FROM t_0094 WHERE p_0005 = con_0122;

    DELETE FROM t_0052 WHERE id = con_0122;

  ELSE
    RAISE EXCEPTION '_';
  END IF;

  IF affected_user_ids IS NOT NULL THEN
    DELETE FROM auth.t_0095 AS u
    WHERE u.id = ANY(affected_user_ids)
      AND NOT EXISTS (
        SELECT 1 FROM t_0094 WHERE p_0124 = u.id
      );
  END IF;
END;$$;


CREATE FUNCTION "public"."f_0040"("p_0123" uuid, "p_0010" text, "p_0009" uuid) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  regular_role TEXT;
BEGIN
  IF p_0010 = '_' THEN
    regular_role := '_';
  ELSIF p_0010 = '_' THEN
    regular_role := '_';
  ELSIF p_0010 = '_' THEN
    regular_role := '_';
  ELSE
    RAISE EXCEPTION '_';
  END IF;

  PERFORM f_0019(p_0123, p_0010, p_0009, regular_role);
END;$$;



CREATE FUNCTION "public"."f_0051"(
  "p_0026" uuid,
  "p_0106" text
) RETURNS TABLE("p_0122" uuid) LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = public, pg_catalog AS $$BEGIN
  RETURN QUERY
    SELECT es.p_0122
    FROM public.t_0050 es
    JOIN public.t_0092 s ON s.id = es.p_0122
    WHERE es.p_0001 = p_0026
      AND s.name = p_0106;
END;$$;

GRANT EXECUTE ON FUNCTION "public"."f_0051"(uuid, text) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0051"(uuid, text) TO "supabase_auth_admin";




CREATE OR REPLACE FUNCTION public.f_0057(
    p_0036 uuid[],
    p_0012 int
)
RETURNS TABLE (
    id uuid,
    c_0391 timestamptz,
    p_0124 uuid,
    c_0491 text,
    c_0390 uuid,
    c_0334 text,
    c_0333 jsonb,
    c_0323 jsonb,
    c_0387 text,
    con_0122 uuid
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = '_'
AS $$
DECLARE
    v_inaccessible_count int;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT COUNT(*)
    INTO v_inaccessible_count
    FROM unnest(p_0036) AS requested_id
    WHERE NOT EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = requested_id
          AND (
              f."c_0172" = auth.f_0102()
              OR auth.f_0126(f."c_0172")
              OR auth.f_0119(p_0105 => f."c_0172")
              OR auth.f_0143(f."c_0172")
              OR auth.f_0061()
          )
    );

    IF v_inaccessible_count > 0 THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT
        l.id,
        l.c_0391,
        l.p_0124,
        l.c_0491,
        l.c_0390,
        l.c_0334,
        l.c_0333,
        l.c_0323,
        l.c_0387,
        l.con_0122
    FROM audit.log l
    WHERE l.c_0491 = '_'
      AND l.c_0390 = ANY(p_0036)
      AND l.c_0391 >= (p_0012 || '_')::timestamptz
      AND l.c_0391 < ((p_0012 + 1) || '_')::timestamptz
    ORDER BY l.c_0391;
END;
$$;


CREATE FUNCTION "public"."f_0059"("p_0113" uuid) RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  c_0411 JSONB;
  roles_array JSONB;
  levels_array JSONB;
  enterprises_array JSONB;
  suppliers_array JSONB;
  farmers_array JSONB;
  permissions_array JSONB;
  t_0074 JSONB;
  methodology_mode_value TEXT;
  verifier_enterprises_array JSONB;
BEGIN
  BEGIN
    WITH user_role_context AS (
      SELECT
        ur.c_0417,
        ur.p_0122,
        ur.p_0005,
        ur.p_0001
      FROM public.t_0094 ur
      WHERE ur.p_0124 = p_0113
    ),
    
    farmer_suppliers AS (
      SELECT DISTINCT sf.p_0122
      FROM public.t_0094 ur
      JOIN public.t_0091 sf ON ur.p_0005 = sf.p_0005
      WHERE ur.p_0124 = p_0113 AND ur.p_0005 IS NOT NULL
    ),
    
    
    
    junction_enterprises AS (
      SELECT DISTINCT es.p_0001
      FROM public.t_0050 es
      WHERE es.p_0122 IN (
        SELECT ur.p_0122
        FROM public.t_0094 ur
        WHERE ur.p_0124 = p_0113 AND ur.p_0122 IS NOT NULL
        UNION
        SELECT p_0122 FROM farmer_suppliers
      )
    ),
    
    
    direct_farmer_enterprises AS (
      SELECT DISTINCT ef.p_0001
      FROM public.t_0049 ef
      JOIN public.t_0094 ur ON ur.p_0005 = ef.p_0005
      WHERE ur.p_0124 = p_0113 AND ur.p_0005 IS NOT NULL
    ),
    
    all_enterprises AS (
      SELECT p_0001 FROM user_role_context WHERE p_0001 IS NOT NULL
      UNION
      SELECT p_0001 FROM junction_enterprises
      UNION
      SELECT p_0001 FROM direct_farmer_enterprises
    )
    SELECT
      jsonb_agg(DISTINCT r.name),
      jsonb_agg(DISTINCT r.level),
      (SELECT jsonb_agg(ae.p_0001 ORDER BY ae.p_0001) FROM (SELECT DISTINCT p_0001 FROM all_enterprises) ae),
      (SELECT jsonb_agg(s.p_0122 ORDER BY s.p_0122) FROM (
        SELECT DISTINCT p_0122 FROM user_role_context WHERE p_0122 IS NOT NULL
        UNION
        SELECT p_0122 FROM farmer_suppliers
      ) s),
      jsonb_agg(DISTINCT urc.p_0005) FILTER (WHERE urc.p_0005 IS NOT NULL)
    INTO
      roles_array,
      levels_array,
      enterprises_array,
      suppliers_array,
      farmers_array
    FROM user_role_context urc
    JOIN public.t_0079 r ON urc.c_0417 = r.id;

    
    
    
    
    
    SELECT COALESCE(
      (SELECT s.ty_0034::text FROM public.t_0092 s WHERE s.id = (suppliers_array->>0)::uuid AND s.ty_0034 IS NOT NULL),
      (SELECT e.ty_0034::text FROM public.t_0051 e WHERE e.id = (enterprises_array->>0)::uuid)
    ) INTO methodology_mode_value;

    
    SELECT
      jsonb_agg(DISTINCT p.c_0409 || '_' || p.c_0334)
    INTO
      permissions_array
    FROM public.t_0094 ur
    JOIN public.t_0078 rp ON ur.c_0417 = rp.c_0417
    JOIN public.t_0076 p ON rp.c_0344 = p.id
    WHERE ur.p_0124 = p_0113;

    
    
    IF roles_array ? '_' THEN
      SELECT jsonb_build_object(
        '_', '_',
        '_', enterprises_array->>0,
        '_', COALESCE(os.c_0078, FALSE)
      ) INTO t_0074
      FROM (SELECT (enterprises_array->>0)::uuid AS id) e
      LEFT JOIN public.t_0074 os ON os.con_0122 = e.id AND os.p_0002 = '_';
    ELSIF roles_array ? '_' THEN
      SELECT jsonb_build_object(
        '_', '_',
        '_', suppliers_array->>0,
        '_', COALESCE(os.c_0078, FALSE)
      ) INTO t_0074
      FROM (SELECT (suppliers_array->>0)::uuid AS id) supplier_subq
      LEFT JOIN public.t_0074 os ON os.con_0122 = supplier_subq.id AND os.p_0002 = '_';
    ELSIF roles_array ? '_' THEN
      SELECT jsonb_build_object(
        '_', '_',
        '_', farmers_array->>0,
        '_', COALESCE(os.c_0078, FALSE)
      ) INTO t_0074
      FROM (SELECT (farmers_array->>0)::uuid AS id) farmer_subq
      LEFT JOIN public.t_0074 os ON os.con_0122 = farmer_subq.id AND os.p_0002 = '_';
    END IF;

    SELECT (SELECT jsonb_agg(ve.p_0001 ORDER BY ve.p_0001) FROM (SELECT DISTINCT va.p_0001 FROM public.t_0096 va WHERE va.c_0537 = p_0113 AND va.c_0181 > now() AND va.c_0415 IS NULL) ve)
    INTO verifier_enterprises_array;

  EXCEPTION
    WHEN OTHERS THEN
      RAISE EXCEPTION '_%_%_', p_0113, SQLERRM;
  END;

  
  roles_array := COALESCE(roles_array, 'null'::jsonb);
  levels_array := COALESCE(levels_array, 'null'::jsonb);
  enterprises_array := COALESCE(enterprises_array, 'null'::jsonb);
  suppliers_array := COALESCE(suppliers_array, 'null'::jsonb);
  farmers_array := COALESCE(farmers_array, 'null'::jsonb);
  permissions_array := COALESCE(permissions_array, 'null'::jsonb);
  verifier_enterprises_array := COALESCE(verifier_enterprises_array, 'null'::jsonb);

  
  
  
  c_0411 := jsonb_strip_nulls(jsonb_build_object(
    '_', roles_array,
    '_', levels_array,
    '_', enterprises_array,
    '_', suppliers_array,
    '_', farmers_array,
    '_', permissions_array,
    '_', t_0074,
    '_', methodology_mode_value,
    '_', verifier_enterprises_array
  ));

  RETURN c_0411;
END;$$;


CREATE FUNCTION "public"."f_0063"("p_0124" uuid, "p_0002" text, "con_0122" uuid) RETURNS boolean LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  admin_role_id UUID;
  admin_count INTEGER;
  f_0061 BOOLEAN;
BEGIN
  IF p_0002 = '_' THEN
    
    SELECT id INTO admin_role_id FROM public.t_0079 WHERE name = 'v1';

    
    SELECT EXISTS (
      SELECT 1 FROM public.t_0094
      WHERE t_0094.p_0124 = f_0063.p_0124
      AND t_0094.p_0001 = con_0122
      AND t_0094.c_0417 = admin_role_id
    ) INTO f_0061;

    
    IF NOT f_0061 THEN
      RETURN FALSE;
    END IF;

    
    SELECT COUNT(*) INTO admin_count
    FROM public.t_0094
    WHERE t_0094.p_0001 = con_0122
    AND t_0094.c_0417 = admin_role_id;

  ELSIF p_0002 = '_' THEN
    
    SELECT id INTO admin_role_id FROM public.t_0079 WHERE name = 'v1';

    
    SELECT EXISTS (
      SELECT 1 FROM public.t_0094
      WHERE t_0094.p_0124 = f_0063.p_0124
      AND t_0094.p_0122 = con_0122
      AND t_0094.c_0417 = admin_role_id
    ) INTO f_0061;

    
    IF NOT f_0061 THEN
      RETURN FALSE;
    END IF;

    
    SELECT COUNT(*) INTO admin_count
    FROM public.t_0094
    WHERE t_0094.p_0122 = con_0122
    AND t_0094.c_0417 = admin_role_id;

  ELSIF p_0002 = '_' THEN
    
    SELECT id INTO admin_role_id FROM public.t_0079 WHERE name = 'v1';

    
    SELECT EXISTS (
      SELECT 1 FROM public.t_0094
      WHERE t_0094.p_0124 = f_0063.p_0124
      AND t_0094.p_0005 = con_0122
      AND t_0094.c_0417 = admin_role_id
    ) INTO f_0061;

    
    IF NOT f_0061 THEN
      RETURN FALSE;
    END IF;

    
    SELECT COUNT(*) INTO admin_count
    FROM public.t_0094
    WHERE t_0094.p_0005 = con_0122
    AND t_0094.c_0417 = admin_role_id;

  ELSE
    RAISE EXCEPTION '_';
  END IF;

  
  RETURN admin_count = 1;
END;$$;


CREATE FUNCTION "public"."f_0064"("p_0124" uuid, "p_0002" text, "con_0122" uuid) RETURNS boolean LANGUAGE plpgsql VOLATILE SECURITY INVOKER AS $$DECLARE
  user_count INTEGER;
  is_user BOOLEAN;
  id_column TEXT := p_0002 || '_';
BEGIN
  EXECUTE format('_%_', id_column) INTO user_count USING con_0122;

  EXECUTE format('_%_', id_column) INTO is_user USING p_0124, con_0122;

  RETURN user_count = 1 AND is_user;
END;$$;


CREATE FUNCTION "public"."f_0082"("p_0123" uuid, "p_0010" text, "p_0009" uuid) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  admin_role TEXT;
BEGIN
  IF p_0010 = '_' THEN
    admin_role := '_';
  ELSIF p_0010 = '_' THEN
    admin_role := '_';
  ELSIF p_0010 = '_' THEN
    admin_role := '_';
  ELSE
    RAISE EXCEPTION '_';
  END IF;

  PERFORM f_0019(p_0123, p_0010, p_0009, admin_role);
END;$$;


CREATE FUNCTION "public"."f_0085"("p_0123" uuid, "p_0002" text, "con_0122" uuid) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  f_0063 BOOLEAN := FALSE;
  admin_role_id UUID;
  admin_count INTEGER;
BEGIN
  
  IF p_0002 = '_' THEN
    
    SELECT id INTO admin_role_id FROM t_0079 WHERE name = 'v1';

    
    SELECT COUNT(*) INTO admin_count
    FROM t_0094
    WHERE p_0001 = con_0122
    AND c_0417 = admin_role_id;

    
    IF admin_count = 1 AND EXISTS (
      SELECT 1 FROM t_0094
      WHERE p_0124 = p_0123
      AND p_0001 = con_0122
      AND c_0417 = admin_role_id
    ) THEN
      f_0063 := TRUE;
    END IF;

  ELSIF p_0002 = '_' THEN
    
    SELECT id INTO admin_role_id FROM t_0079 WHERE name = 'v1';

    
    SELECT COUNT(*) INTO admin_count
    FROM t_0094
    WHERE p_0122 = con_0122
    AND c_0417 = admin_role_id;

    
    IF admin_count = 1 AND EXISTS (
      SELECT 1 FROM t_0094
      WHERE p_0124 = p_0123
      AND p_0122 = con_0122
      AND c_0417 = admin_role_id
    ) THEN
      f_0063 := TRUE;
    END IF;

  ELSIF p_0002 = '_' THEN
    
    SELECT id INTO admin_role_id FROM t_0079 WHERE name = 'v1';

    
    SELECT COUNT(*) INTO admin_count
    FROM t_0094
    WHERE p_0005 = con_0122
    AND c_0417 = admin_role_id;

    
    IF admin_count = 1 AND EXISTS (
      SELECT 1 FROM t_0094
      WHERE p_0124 = p_0123
      AND p_0005 = con_0122
      AND c_0417 = admin_role_id
    ) THEN
      f_0063 := TRUE;
    END IF;

  ELSE
    RAISE EXCEPTION '_';
  END IF;

  
  IF f_0063 THEN
    RAISE EXCEPTION '_%_', p_0002;
  END IF;

  
  IF p_0002 = '_' THEN
    DELETE FROM t_0094
    WHERE p_0124 = p_0123
    AND p_0001 = con_0122;

  ELSIF p_0002 = '_' THEN
    DELETE FROM t_0094
    WHERE p_0124 = p_0123
    AND p_0122 = con_0122;

  ELSIF p_0002 = '_' THEN
    DELETE FROM t_0094
    WHERE p_0124 = p_0123
    AND p_0005 = con_0122;
  END IF;

  
  PERFORM f_0105(p_0123);
END;$$;



CREATE FUNCTION "public"."f_0087"("p_0065" text) RETURNS uuid LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public AS $$DECLARE
  v_user_id UUID;
  v_key_id UUID;
BEGIN
  SELECT id, p_0124
  INTO v_key_id, v_user_id
  FROM public.t_0044
  WHERE c_0274 = p_0065
    AND c_0415 IS NULL;

  IF v_key_id IS NULL THEN
    RETURN NULL;
  END IF;

  UPDATE public.t_0044
  SET c_0282 = now()
  WHERE id = v_key_id;

  RETURN v_user_id;
END;$$;


CREATE FUNCTION "public"."f_0097"() RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
    jwt_payload jsonb;
    p_0124 text;
BEGIN
    
    

    
    BEGIN
        
        p_0124 := current_setting('_', true);
        IF p_0124 IS NOT NULL AND p_0124 != '_' THEN
            PERFORM set_config('_', p_0124, true);
            RETURN;
        END IF;
    EXCEPTION WHEN OTHERS THEN
        
    END;

    BEGIN
        
        jwt_payload := current_setting('_', true)::jsonb;
        p_0124 := jwt_payload->>'_';
        IF p_0124 IS NOT NULL AND p_0124 != '_' THEN
            PERFORM set_config('_', p_0124, true);
            RETURN;
        END IF;
    EXCEPTION WHEN OTHERS THEN
        
    END;

    BEGIN
        
        jwt_payload := current_setting('_', true)::jsonb;
        p_0124 := jwt_payload->>'_';
        IF p_0124 IS NOT NULL AND p_0124 != '_' THEN
            PERFORM set_config('_', p_0124, true);
            RETURN;
        END IF;
    EXCEPTION WHEN OTHERS THEN
        
    END;
END;$$;


CREATE FUNCTION "public"."f_0105"("p_0124" uuid) RETURNS void LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$BEGIN
  UPDATE auth.t_0095
  SET c_0384 = jsonb_set(
    COALESCE(c_0384, 'null'::jsonb),
    '_',
    f_0059(p_0124)
  )
  WHERE id = p_0124;
END;$$;


CREATE FUNCTION "public"."f_0125"() RETURNS trigger LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  in_user_creation TEXT;
  p_0123 UUID;
  user_exists BOOLEAN;
BEGIN
  
  
  in_user_creation := current_setting('_', true);

  IF in_user_creation = '_' THEN
    RETURN CASE WHEN TG_OP = '_' THEN OLD ELSE NEW END;
  END IF;

  
  p_0123 := CASE WHEN TG_OP = '_' THEN OLD.p_0124 ELSE NEW.p_0124 END;

  
  SELECT EXISTS(SELECT 1 FROM auth.t_0095 WHERE id = p_0123) INTO user_exists;

  IF NOT user_exists THEN
    
    RETURN CASE WHEN TG_OP = '_' THEN OLD ELSE NEW END;
  END IF;

  
  
  PERFORM public.f_0105(p_0123);

  RETURN CASE WHEN TG_OP = '_' THEN OLD ELSE NEW END;
END;$$;


CREATE FUNCTION "public"."f_0142"() RETURNS trigger LANGUAGE plpgsql VOLATILE SECURITY DEFINER AS $$DECLARE
  p_0123 UUID;
  user_exists BOOLEAN;
BEGIN
  p_0123 := CASE WHEN TG_OP = '_' THEN OLD.c_0537 ELSE NEW.c_0537 END;

  SELECT EXISTS(SELECT 1 FROM auth.t_0095 WHERE id = p_0123) INTO user_exists;

  IF NOT user_exists THEN
    RETURN CASE WHEN TG_OP = '_' THEN OLD ELSE NEW END;
  END IF;

  PERFORM public.f_0105(p_0123);

  RETURN CASE WHEN TG_OP = '_' THEN OLD ELSE NEW END;
END;$$;










CREATE OR REPLACE VIEW "public"."v_0008" AS
SELECT u.id,
    u.id AS p_0124,
    f.id AS con_0122,
    u.f_0041,
    u.f_0041 AS user_email,
    f.name AS c_0194,
    f.name,
    f.id AS p_0005,
    s.id AS p_0122,
    s.name AS c_0488,
    enterprise_link.p_0001,
    enterprise_link.c_0171,
    r.name AS p_0121,
    r.name AS role,
    u.c_0095,
    f.c_0529,
    u.c_0241,
    '_'::text AS c_0524,
    CASE WHEN (u.c_0154 IS NOT NULL) THEN '_'::text ELSE '_'::text END AS c_0472,
    CASE
        WHEN (auth.f_0121() OR auth.f_0113(enterprise_link.p_0001) OR auth.f_0118(s.id) OR auth.f_0115(ur.p_0005)) THEN u.c_0154
        ELSE NULL::timestamp with time zone
    END AS c_0083,
    NULL::text AS password,
    f.c_0529 AS entity_updated_at
FROM (((((auth.t_0095 u
    JOIN t_0094 ur ON ((ur.p_0124 = u.id)))
    JOIN t_0052 f ON ((f.id = ur.p_0005)))
    LEFT JOIN LATERAL (
        SELECT sf.p_0122
        FROM t_0091 sf
        WHERE sf.p_0005 = f.id
          AND (
            auth.f_0121()
            OR ur.p_0124 = (SELECT auth.f_0102())
            OR auth.f_0118(sf.p_0122)
            OR EXISTS (
              SELECT 1 FROM t_0050 es2
              WHERE es2.p_0122 = sf.p_0122
                AND auth.f_0113(es2.p_0001)
            )
          )
        ORDER BY sf.c_0095 DESC
        LIMIT 1
    ) canonical_sf ON true
    LEFT JOIN t_0092 s ON (s.id = canonical_sf.p_0122))
    JOIN t_0079 r ON ((r.id = ur.c_0417)))
    LEFT JOIN LATERAL (
        SELECT es.p_0001, e.name AS c_0171
        FROM t_0050 es
        JOIN t_0051 e ON ((e.id = es.p_0001))
        WHERE es.p_0122 = s.id
        UNION
        SELECT ef.p_0001, e.name AS c_0171
        FROM t_0049 ef
        JOIN t_0051 e ON ((e.id = ef.p_0001))
        WHERE ef.p_0005 = f.id
    ) enterprise_link ON true)
WHERE ((r.level = '_'::text) AND ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) OR auth.f_0121() OR auth.f_0113(enterprise_link.p_0001) OR auth.f_0118(s.id) OR auth.f_0115(ur.p_0005)))

UNION ALL

SELECT pending_farmers.id,
    pending_farmers.p_0124,
    pending_farmers.con_0122,
    pending_farmers.f_0041,
    pending_farmers.user_email,
    pending_farmers.c_0194,
    pending_farmers.name,
    pending_farmers.p_0005,
    pending_farmers.p_0122,
    pending_farmers.c_0488,
    pending_farmers.p_0001,
    pending_farmers.c_0171,
    pending_farmers.p_0121,
    pending_farmers.role,
    pending_farmers.c_0095,
    pending_farmers.c_0529,
    pending_farmers.c_0241,
    pending_farmers.c_0524,
    pending_farmers.c_0472,
    pending_farmers.c_0083,
    pending_farmers.password,
    pending_farmers.entity_updated_at
FROM (
    SELECT DISTINCT ON (u.id) u.id,
        u.id AS p_0124,
        NULL::uuid AS con_0122,
        u.f_0041,
        u.f_0041 AS user_email,
        (u.c_0386 ->> '_'::text) AS c_0194,
        (u.c_0386 ->> '_'::text) AS name,
        NULL::uuid AS p_0005,
        s.id AS p_0122,
        s.name AS c_0488,
        COALESCE(e_via_supplier.id, e_direct.id) AS p_0001,
        COALESCE(e_via_supplier.name, e_direct.name) AS c_0171,
        (u.c_0386 ->> '_'::text) AS p_0121,
        (u.c_0386 ->> '_'::text) AS role,
        u.c_0095,
        u.c_0529,
        u.c_0241,
        '_'::text AS c_0524,
        '_'::text AS c_0472,
        NULL::timestamp with time zone AS c_0083,
        NULL::text AS password,
        u.c_0529 AS entity_updated_at
    FROM ((((auth.t_0095 u
        LEFT JOIN t_0092 s ON ((s.id = ((u.c_0386 ->> '_'::text))::uuid)))
        LEFT JOIN t_0050 es ON ((es.p_0122 = s.id)))
        LEFT JOIN t_0051 e_via_supplier ON ((e_via_supplier.id = es.p_0001)))
        LEFT JOIN t_0051 e_direct ON ((u.c_0386 ->> '_'::text) IS NULL AND e_direct.id = ((u.c_0386 ->> '_'::text))::uuid))
    WHERE ((u.c_0241 IS NOT NULL)
        AND (u.c_0154 IS NULL)
        AND (u.c_0135 IS NULL)
        AND ((u.c_0386 ->> '_'::text) = '_'::text)
        AND (NOT (EXISTS ( SELECT 1 FROM t_0094 ur WHERE (ur.p_0124 = u.id))))
        AND (
            auth.f_0121()
            OR ((s.id IS NOT NULL) AND auth.f_0118(s.id))
            OR ((e_via_supplier.id IS NOT NULL) AND auth.f_0113(e_via_supplier.id))
            OR ((e_direct.id IS NOT NULL) AND auth.f_0113(e_direct.id))
        ))
    ORDER BY u.id, es.p_0001
) pending_farmers;

GRANT SELECT ON public.v_0008 TO authenticated;
GRANT SELECT ON public.v_0008 TO service_role;
GRANT SELECT ON public.v_0008 TO metabase_ro;





CREATE OR REPLACE VIEW public.v_0007 AS
SELECT
  ff."c_0183" as facility_id,
  ff."c_0192" as p_0124,
  fu.p_0005,
  fu.f_0041,
  fu.name as c_0194,
  fu.p_0122,
  fu.p_0001,
  fu.c_0083,
  fu.c_0472,
  ff."c_0020" as c_0022
FROM mrv."t_0011" ff
JOIN public.v_0008 fu ON fu.p_0124 = ff."c_0192";


CREATE OR REPLACE VIEW public.v_0009 AS
SELECT
  pf."c_0361" as c_0364,
  pf."c_0192" as p_0124,
  fu.p_0005,
  fu.f_0041,
  fu.name as c_0194,
  fu.p_0122,
  fu.p_0001,
  fu.c_0083,
  fu.c_0472,
  pf."c_0020" as c_0022
FROM mrv."t_0023" pf
JOIN public.v_0008 fu ON fu.p_0124 = pf."c_0192";


GRANT SELECT ON public.v_0007 TO authenticated;
GRANT SELECT ON public.v_0007 TO service_role;
GRANT SELECT ON public.v_0007 TO metabase_ro;
GRANT SELECT ON public.v_0009 TO authenticated;
GRANT SELECT ON public.v_0009 TO service_role;
GRANT SELECT ON public.v_0009 TO metabase_ro;


CREATE OR REPLACE VIEW "public"."v_0001" AS
SELECT
    e.id AS p_0001,
    e.name AS c_0171,
    u.id AS p_0124,
    u.f_0041,
    u.c_0154 AS c_0083
FROM public.t_0051 e
LEFT JOIN public.t_0094 ur ON ur.p_0001 = e.id
LEFT JOIN public.t_0079 r ON r.id = ur.c_0417 AND r.name = 'v1'
LEFT JOIN auth.t_0095 u ON u.id = ur.p_0124
WHERE auth.f_0121();

GRANT SELECT ON "public"."v_0001" TO authenticated;
GRANT SELECT ON "public"."v_0001" TO service_role;







CREATE OR REPLACE VIEW "public"."v_0002" AS
SELECT
    e.id AS p_0001,
    e.name AS c_0171,
    COALESCE(
        json_agg(
            json_build_object(
                '_', u.id,
                '_', u.f_0041,
                '_', r.name,
                '_', u.c_0154
            )
        ) FILTER (WHERE u.id IS NOT NULL),
        'null'::json
    ) AS members,
    e.c_0088,
    e.c_0064,
    e.c_0481
FROM t_0051 e
LEFT JOIN t_0094 ur ON ur.p_0001 = e.id
LEFT JOIN t_0079 r ON ur.c_0417 = r.id AND r.name = ANY (ARRAY['_'::text, '_'::text])
LEFT JOIN auth.t_0095 u ON ur.p_0124 = u.id
WHERE auth.f_0121()
GROUP BY e.id, e.name, e.c_0088, e.c_0064, e.c_0481;

GRANT SELECT ON "public"."v_0002" TO authenticated;
GRANT SELECT ON "public"."v_0002" TO service_role;
GRANT SELECT ON "public"."v_0002" TO metabase_ro;




CREATE OR REPLACE VIEW "public"."v_0003" AS
SELECT
    e.id AS con_0122,
    e.name AS entity_name,
    '_'::text AS p_0002,
    e.ty_0034::text AS ty_0034,
    e.c_0088,
    e.c_0064,
    e.c_0481,
    COALESCE(
        json_agg(
            json_build_object(
                '_', u.id,
                '_', u.f_0041,
                '_', r.name,
                '_', u.c_0154
            )
        ) FILTER (WHERE u.id IS NOT NULL),
        'null'::json
    ) AS members
FROM t_0051 e
LEFT JOIN t_0094 ur ON ur.p_0001 = e.id
LEFT JOIN t_0079 r ON ur.c_0417 = r.id AND r.name = ANY (ARRAY['_'::text, '_'::text])
LEFT JOIN auth.t_0095 u ON ur.p_0124 = u.id
WHERE auth.f_0121()
GROUP BY e.id, e.name, e.ty_0034, e.c_0088, e.c_0064, e.c_0481

UNION ALL

SELECT
    s.id AS con_0122,
    s.name AS entity_name,
    '_'::text AS p_0002,
    s.ty_0034::text AS ty_0034,
    NULL::text AS c_0088,
    NULL::text AS c_0064,
    NULL::text AS c_0481,
    COALESCE(
        json_agg(
            json_build_object(
                '_', u.id,
                '_', u.f_0041,
                '_', r.name,
                '_', u.c_0154
            )
        ) FILTER (WHERE u.id IS NOT NULL),
        'null'::json
    ) AS members
FROM t_0092 s
LEFT JOIN t_0094 ur ON ur.p_0122 = s.id
LEFT JOIN t_0079 r ON ur.c_0417 = r.id AND r.name = ANY (ARRAY['_'::text, '_'::text])
LEFT JOIN auth.t_0095 u ON ur.p_0124 = u.id
WHERE NOT EXISTS (SELECT 1 FROM t_0050 es WHERE es.p_0122 = s.id)
  AND auth.f_0121()
GROUP BY s.id, s.name, s.ty_0034;

GRANT SELECT ON "public"."v_0003" TO authenticated;
GRANT SELECT ON "public"."v_0003" TO service_role;
GRANT SELECT ON "public"."v_0003" TO metabase_ro;



CREATE OR REPLACE VIEW "public"."v_0004" AS
SELECT
    u.id AS p_0124,
    u.f_0041,
    u.c_0386->>'_' AS c_0240,
    u.c_0386->>'_' AS c_0171,
    u.c_0386->>'_' AS c_0488,
    u.c_0386->>'_' AS c_0194,
    u.c_0241,
    u.c_0386->>'_' AS ty_0034
FROM auth.t_0095 u
WHERE u.c_0241 IS NOT NULL
  AND u.c_0154 IS NULL
  AND u.c_0386->>'_' IS NOT NULL
  AND auth.f_0121();

GRANT SELECT ON "public"."v_0004" TO authenticated;
GRANT SELECT ON "public"."v_0004" TO service_role;
GRANT SELECT ON "public"."v_0004" TO metabase_ro;


CREATE OR REPLACE VIEW "public"."v_0005" AS
SELECT s.id,
    s.id AS p_0122,
    s.name,
    es.p_0001,
    e.name AS c_0171,
    owner_user.id AS p_0124,
    owner_user.f_0041 AS user_email,
    owner_user.c_0241,
    owner_user.c_0083,
    s.c_0095,
    s.c_0529,
    '_'::text AS c_0524,
    owner_user.p_0121,
    CASE
        WHEN (s.c_0259 = false) THEN '_'::text
        WHEN (owner_user.c_0083 IS NULL) THEN '_'::text
        ELSE '_'::text
    END AS c_0472,
    (SELECT COUNT(*) FROM t_0050 es2 WHERE es2.p_0122 = s.id) AS enterprise_count
FROM ((t_0050 es
    JOIN t_0092 s ON ((s.id = es.p_0122)))
    JOIN t_0051 e ON ((e.id = es.p_0001))
    LEFT JOIN LATERAL (SELECT u.id,
            u.f_0041,
            u.c_0241,
            u.c_0154 AS c_0083,
            r.name AS p_0121
        FROM ((auth.t_0095 u
            JOIN t_0094 ur ON (((u.id = ur.p_0124) AND (ur.p_0122 = s.id))))
            JOIN t_0079 r ON (((ur.c_0417 = r.id) AND (r.name ~~ '_'::text))))
        WHERE (u.c_0241 IS NOT NULL)
        ORDER BY u.c_0241
        LIMIT 1) owner_user ON (true))
WHERE (
    auth.f_0121()
    OR (EXISTS (SELECT 1
        FROM (t_0094 viewer_ur
            JOIN t_0079 viewer_r ON ((viewer_ur.c_0417 = viewer_r.id)))
        WHERE ((viewer_ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (viewer_ur.p_0001 = es.p_0001) AND (viewer_r.name ~~ '_'::text))))
    OR (EXISTS (SELECT 1
        FROM t_0094 viewer_ur
        WHERE ((viewer_ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (viewer_ur.p_0122 = s.id))))
)
ORDER BY s.c_0095 DESC, owner_user.c_0241 DESC;

GRANT SELECT ON public.v_0005 TO authenticated;
GRANT SELECT ON public.v_0005 TO service_role;
GRANT SELECT ON public.v_0005 TO metabase_ro;



CREATE OR REPLACE VIEW "public"."v_0006" AS
SELECT u.id,
    u.id AS p_0124,
    e.id AS con_0122,
    u.f_0041,
    u.f_0041 AS user_email,
    e.name AS c_0171,
    e.name,
    e.id AS p_0001,
    r.name AS p_0121,
    r.name AS role,
    u.c_0095,
    e.c_0529,
    u.c_0241,
    '_'::text AS c_0524,
    CASE WHEN u.c_0154 IS NOT NULL THEN '_' ELSE '_' END AS c_0472,
    CASE
        WHEN (auth.f_0121() OR auth.f_0113(ur.p_0001)) THEN u.c_0154
        ELSE NULL::timestamp with time zone
    END AS c_0083,
    NULL::text AS password,
    e.c_0529 AS entity_updated_at
FROM (((auth.t_0095 u
    JOIN t_0094 ur ON ((ur.p_0124 = u.id)))
    JOIN t_0051 e ON ((e.id = ur.p_0001)))
    JOIN t_0079 r ON ((r.id = ur.c_0417)))
WHERE (r.level = '_'::text) AND (
    auth.f_0121()
    OR (auth.f_0113(ur.p_0001) AND (r.name ~~ '_'::text))
    OR ((EXISTS (SELECT 1
        FROM (t_0094 member_ur
            JOIN t_0079 member_r ON ((member_ur.c_0417 = member_r.id)))
        WHERE ((member_ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (member_r.name ~~ '_'::text) AND (member_ur.p_0001 = ur.p_0001)))) AND (r.name ~~ '_'::text))
)

UNION ALL


SELECT u.id,
    u.id AS p_0124,
    e.id AS con_0122,
    u.f_0041,
    u.f_0041 AS user_email,
    COALESCE(e.name, (u.c_0386->>'_')::text) AS c_0171,
    COALESCE(e.name, (u.c_0386->>'_')::text) AS name,
    e.id AS p_0001,
    (u.c_0386->>'_')::text AS p_0121,
    (u.c_0386->>'_')::text AS role,
    u.c_0095,
    u.c_0529,
    u.c_0241,
    '_'::text AS c_0524,
    '_'::text AS c_0472,
    NULL::timestamp with time zone AS c_0083,
    NULL::text AS password,
    u.c_0529 AS entity_updated_at
FROM auth.t_0095 u
    LEFT JOIN t_0051 e ON (e.id = (u.c_0386->>'_')::uuid)
WHERE u.c_0241 IS NOT NULL
    AND u.c_0154 IS NULL
    AND u.c_0135 IS NULL
    AND u.c_0386->>'_' = '_'
    AND NOT EXISTS (SELECT 1 FROM t_0094 ur WHERE ur.p_0124 = u.id)
    AND (
        auth.f_0121()
        OR (e.id IS NOT NULL AND auth.f_0113(e.id))
    );

GRANT SELECT ON public.v_0006 TO authenticated;
GRANT SELECT ON public.v_0006 TO service_role;
GRANT SELECT ON public.v_0006 TO metabase_ro;








CREATE OR REPLACE VIEW "public"."v_0011" AS
SELECT u.id,
    u.id AS p_0124,
    s.id AS con_0122,
    u.f_0041,
    u.f_0041 AS user_email,
    s.name AS c_0488,
    s.name,
    s.id AS p_0122,
    es.p_0001,
    e.name AS c_0171,
    r.name AS p_0121,
    r.name AS role,
    u.c_0095,
    s.c_0529,
    u.c_0241,
    '_'::text AS c_0524,
    CASE
        WHEN s.c_0259 = false THEN '_'
        WHEN u.c_0154 IS NOT NULL THEN '_'
        ELSE '_'
    END AS c_0472,
    CASE
        WHEN (auth.f_0121() OR auth.f_0113(es.p_0001) OR auth.f_0118(s.id)) THEN u.c_0154
        ELSE NULL::timestamp with time zone
    END AS c_0083,
    NULL::text AS password,
    s.c_0529 AS entity_updated_at,
    (SELECT COUNT(*) FROM t_0050 es2 WHERE es2.p_0122 = s.id) AS enterprise_count
FROM (((((auth.t_0095 u
    JOIN t_0094 ur ON ((ur.p_0124 = u.id)))
    JOIN t_0092 s ON ((s.id = ur.p_0122)))
    LEFT JOIN t_0050 es ON ((es.p_0122 = s.id)))
    LEFT JOIN t_0051 e ON ((e.id = es.p_0001)))
    JOIN t_0079 r ON ((r.id = ur.c_0417)))
WHERE ((r.level = '_'::text) AND ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) OR auth.f_0121() OR auth.f_0113(es.p_0001) OR auth.f_0118(s.id)))

UNION ALL






SELECT pending_suppliers.id,
    pending_suppliers.p_0124,
    pending_suppliers.con_0122,
    pending_suppliers.f_0041,
    pending_suppliers.user_email,
    pending_suppliers.c_0488,
    pending_suppliers.name,
    pending_suppliers.p_0122,
    pending_suppliers.p_0001,
    pending_suppliers.c_0171,
    pending_suppliers.p_0121,
    pending_suppliers.role,
    pending_suppliers.c_0095,
    pending_suppliers.c_0529,
    pending_suppliers.c_0241,
    pending_suppliers.c_0524,
    pending_suppliers.c_0472,
    pending_suppliers.c_0083,
    pending_suppliers.password,
    pending_suppliers.entity_updated_at,
    pending_suppliers.enterprise_count
FROM (
    SELECT DISTINCT ON (u.id) u.id,
        u.id AS p_0124,
        s.id AS con_0122,
        u.f_0041,
        u.f_0041 AS user_email,
        COALESCE(s.name, (u.c_0386->>'_')::text) AS c_0488,
        COALESCE(s.name, (u.c_0386->>'_')::text) AS name,
        s.id AS p_0122,
        e.id AS p_0001,
        e.name AS c_0171,
        (u.c_0386->>'_')::text AS p_0121,
        (u.c_0386->>'_')::text AS role,
        u.c_0095,
        u.c_0529,
        u.c_0241,
        '_'::text AS c_0524,
        '_'::text AS c_0472,
        NULL::timestamp with time zone AS c_0083,
        NULL::text AS password,
        u.c_0529 AS entity_updated_at,
        (SELECT COUNT(*) FROM t_0050 es2 WHERE es2.p_0122 = s.id) AS enterprise_count
    FROM auth.t_0095 u
        LEFT JOIN t_0092 s ON (s.id = (u.c_0386->>'_')::uuid)
        LEFT JOIN t_0050 es ON (es.p_0122 = s.id)
        LEFT JOIN t_0051 e ON (e.id = COALESCE(es.p_0001, (u.c_0386->>'_')::uuid))
    WHERE u.c_0241 IS NOT NULL
        AND u.c_0154 IS NULL
        AND u.c_0135 IS NULL
        AND u.c_0386->>'_' = '_'
        AND NOT EXISTS (SELECT 1 FROM t_0094 ur WHERE ur.p_0124 = u.id)
        AND (
            auth.f_0121()
            OR (s.id IS NOT NULL AND auth.f_0118(s.id))
            OR (e.id IS NOT NULL AND auth.f_0113(e.id))
        )
    ORDER BY u.id, e.id
) pending_suppliers;

GRANT SELECT ON public.v_0011 TO authenticated;
GRANT SELECT ON public.v_0011 TO service_role;
GRANT SELECT ON public.v_0011 TO metabase_ro;


CREATE TRIGGER "trg_0020" BEFORE INSERT ON "public"."t_0050" FOR EACH ROW EXECUTE FUNCTION "public"."f_0021"();



CREATE TRIGGER "trg_0021" AFTER INSERT OR DELETE ON "public"."t_0050" FOR EACH ROW EXECUTE FUNCTION "public"."f_0026"();











CREATE TRIGGER "trg_0022" AFTER UPDATE OF "ty_0034" ON "public"."t_0092" FOR EACH ROW EXECUTE FUNCTION "public"."f_0026"();


CREATE TRIGGER "trg_0023" AFTER INSERT OR UPDATE OR DELETE ON "public"."t_0094" FOR EACH ROW EXECUTE FUNCTION "public"."f_0125"();


CREATE TRIGGER "trg_0024" AFTER INSERT OR UPDATE OR DELETE ON "public"."t_0096" FOR EACH ROW EXECUTE FUNCTION "public"."f_0142"();


CREATE POLICY "pol_0071" ON "public"."t_0044" FOR SELECT TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102)));

CREATE POLICY "pol_0059" ON "public"."t_0044" FOR INSERT TO "authenticated" WITH CHECK (12 = 12);


CREATE POLICY "pol_0065" ON "public"."t_0044" FOR UPDATE TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102))) WITH CHECK (13 = 13);

CREATE POLICY "pol_0009" ON "public"."t_0044" FOR ALL TO "authenticated" USING (auth.f_0061());

CREATE POLICY "pol_0032" ON "public"."t_0044" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0308" ON "public"."t_0044" FOR ALL TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0035" ON "public"."t_0049" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0309" ON "public"."t_0049" FOR ALL TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0002" ON "public"."t_0049" FOR ALL TO "authenticated" USING ((EXISTS (SELECT 1
   FROM (t_0094 ur
     JOIN t_0079 r ON ((ur.c_0417 = r.id)))
  WHERE ((ur.p_0124 = auth.f_0102()) AND (r.level = '_'::text)))));

CREATE POLICY "pol_0019" ON "public"."t_0049" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = auth.f_0102()) AND (ur.p_0001 = t_0049.p_0001)))));

CREATE POLICY "pol_0024" ON "public"."t_0049" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = auth.f_0102()) AND (ur.p_0005 = t_0049.p_0005)))));

CREATE POLICY "pol_0016" ON "public"."t_0049" FOR INSERT TO "authenticated"
    WITH CHECK (14 = 14);

CREATE POLICY "pol_0015" ON "public"."t_0049" FOR DELETE TO "authenticated"
    USING (auth.f_0113(t_0049.p_0001));



CREATE POLICY "pol_0036" ON "public"."t_0050" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0310" ON "public"."t_0050" FOR ALL TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0003" ON "public"."t_0050" FOR ALL TO "authenticated" USING ((EXISTS (SELECT 1
   FROM (t_0094 ur
     JOIN t_0079 r ON ((ur.c_0417 = r.id)))
  WHERE ((ur.p_0124 = auth.f_0102()) AND (r.level = '_'::text)))));

CREATE POLICY "pol_0020" ON "public"."t_0050" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = auth.f_0102()) AND (ur.p_0001 = t_0050.p_0001)))));

CREATE POLICY "pol_0053" ON "public"."t_0050" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = auth.f_0102()) AND (ur.p_0122 = t_0050.p_0122)))));


CREATE POLICY "pol_0001" ON "public"."t_0051" FOR INSERT TO "authenticated" WITH CHECK (15 = 15);

CREATE POLICY "pol_0004" ON "public"."t_0051" FOR ALL TO "authenticated" USING ((EXISTS (SELECT 1
   FROM (t_0094 ur
     JOIN t_0079 r ON ((ur.c_0417 = r.id)))
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (r.level = '_'::text)))));

CREATE POLICY "pol_0037" ON "public"."t_0051" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0311" ON "public"."t_0051" FOR ALL TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0068" ON "public"."t_0051" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (ur.p_0001 = t_0051.id)))));

CREATE POLICY "pol_0005" ON "public"."t_0052" FOR ALL TO "authenticated"
    USING (auth.f_0061());

CREATE POLICY "pol_0026" ON "public"."t_0052" FOR SELECT TO "authenticated"
    USING (auth.f_0119(p_0037 => t_0052.id));

CREATE POLICY "pol_0038" ON "public"."t_0052" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0312" ON "public"."t_0052" FOR ALL TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0049" ON "public"."t_0052" FOR INSERT TO "authenticated"
    WITH CHECK (16 = 16);

CREATE POLICY "pol_0056" ON "public"."t_0052" FOR UPDATE TO "authenticated"
    USING (
        EXISTS (
            SELECT 1 FROM public.t_0091 sf
            WHERE sf.p_0005 = t_0052.id
            AND auth.f_0119(p_0105 => sf.p_0122)
        )
    )
    WITH CHECK (17 = 17);

CREATE POLICY "pol_0057" ON "public"."t_0052" FOR SELECT TO "authenticated"
    USING (
        EXISTS (
            SELECT 1 FROM public.t_0091 sf
            WHERE sf.p_0005 = t_0052.id
            AND auth.f_0119(p_0105 => sf.p_0122)
        )
    );

CREATE POLICY "pol_0022" ON "public"."t_0052" FOR SELECT TO "authenticated"
    USING (
        EXISTS (
            SELECT 1 FROM public.t_0049 ef
            WHERE ef.p_0005 = t_0052.id
            AND auth.f_0119(p_0026 => ef.p_0001)
        )
    );

CREATE POLICY "pol_0011" ON "public"."t_0053" FOR SELECT TO "authenticated", "anon" USING (true);

CREATE POLICY "pol_0044" ON "public"."t_0053" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0010" ON "public"."t_0053" FOR ALL TO "authenticated" USING (auth.f_0061()) WITH CHECK (18 = 18);




CREATE POLICY "pol_0304" ON "public"."t_0076" FOR SELECT TO "supabase_admin" USING (true);

CREATE POLICY "pol_0045" ON "public"."t_0076" FOR SELECT TO "service_role" USING (true);

CREATE POLICY "pol_0316" ON "public"."t_0076" FOR SELECT TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0012" ON "public"."t_0076" FOR SELECT TO "authenticated" USING (true);




CREATE POLICY "pol_0305" ON "public"."t_0078" FOR SELECT TO "supabase_admin" USING (true);

CREATE POLICY "pol_0046" ON "public"."t_0078" FOR SELECT TO "service_role" USING (true);

CREATE POLICY "pol_0317" ON "public"."t_0078" FOR SELECT TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0013" ON "public"."t_0078" FOR SELECT TO "authenticated" USING (true);




CREATE POLICY "pol_0306" ON "public"."t_0079" FOR SELECT TO "supabase_admin" USING (true);

CREATE POLICY "pol_0047" ON "public"."t_0079" FOR SELECT TO "service_role" USING (true);

CREATE POLICY "pol_0318" ON "public"."t_0079" FOR SELECT TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0014" ON "public"."t_0079" FOR SELECT TO "authenticated" USING (true);

CREATE POLICY "pol_0040" ON "public"."t_0091" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0313" ON "public"."t_0091" FOR ALL TO "supabase_auth_admin" USING (true);

CREATE POLICY "pol_0006" ON "public"."t_0091" FOR ALL TO "authenticated" USING ((EXISTS (SELECT 1
   FROM (t_0094 ur
     JOIN t_0079 r ON ((ur.c_0417 = r.id)))
  WHERE ((ur.p_0124 = auth.f_0102()) AND (r.level = '_'::text)))));

CREATE POLICY "pol_0055" ON "public"."t_0091" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = auth.f_0102()) AND (ur.p_0122 = t_0091.p_0122)))));

CREATE POLICY "pol_0025" ON "public"."t_0091" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = auth.f_0102()) AND (ur.p_0005 = t_0091.p_0005)))));

CREATE POLICY "pol_0051" ON "public"."t_0091" FOR INSERT TO "authenticated"
    WITH CHECK (19 = 19);

CREATE POLICY "pol_0050" ON "public"."t_0091" FOR DELETE TO "authenticated"
    USING (auth.f_0116('_', '_', p_0105 => t_0091.p_0122));


CREATE POLICY "pol_0007" ON "public"."t_0092" FOR ALL TO "authenticated" USING ((EXISTS (SELECT 1
   FROM (t_0094 ur
     JOIN t_0079 r ON ((ur.c_0417 = r.id)))
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (r.level = '_'::text)))));



CREATE POLICY "pol_0018" ON "public"."t_0092" FOR UPDATE TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (
    EXISTS (SELECT 1 FROM t_0050 es WHERE es.p_0122 = t_0092.id AND es.p_0001 = ur.p_0001) OR
    (NOT EXISTS (SELECT 1 FROM t_0050 es WHERE es.p_0122 = t_0092.id) AND ur.p_0122 = t_0092.id)
  )))));



CREATE POLICY "pol_0021" ON "public"."t_0092" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (
    EXISTS (SELECT 1 FROM t_0050 es WHERE es.p_0122 = t_0092.id AND es.p_0001 = ur.p_0001) OR
    (NOT EXISTS (SELECT 1 FROM t_0050 es WHERE es.p_0122 = t_0092.id) AND ur.p_0122 = t_0092.id)
  )))));



CREATE POLICY "pol_0029" ON "public"."t_0092" FOR INSERT TO "authenticated" WITH CHECK (20 = 20);


CREATE POLICY "pol_0041" ON "public"."t_0092" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0314" ON "public"."t_0092" FOR ALL TO "supabase_auth_admin" USING (true);


CREATE POLICY "pol_0054" ON "public"."t_0092" FOR SELECT TO "authenticated" USING ((EXISTS (SELECT 1
   FROM t_0094 ur
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (ur.p_0122 = t_0092.id)))));


CREATE POLICY "pol_0048" ON "public"."t_0092" FOR DELETE TO "authenticated" USING ((EXISTS (SELECT 1
   FROM (t_0094 ur
     JOIN t_0079 r ON ((ur.c_0417 = r.id)))
  WHERE ((ur.p_0124 = ( SELECT auth.f_0102() AS f_0102)) AND (ur.p_0122 = t_0092.id) AND (NOT EXISTS (SELECT 1 FROM t_0050 es WHERE es.p_0122 = t_0092.id)) AND (r.name = 'v1')))));


CREATE POLICY "pol_0008" ON "public"."t_0094" FOR ALL TO "authenticated" USING (auth.f_0061());

CREATE POLICY "pol_0017" ON "public"."t_0094" FOR ALL TO "authenticated" USING (auth.f_0065('_'::text, p_0001));

CREATE POLICY "pol_0023" ON "public"."t_0094" FOR ALL TO "authenticated" USING (auth.f_0065('_'::text, NULL::uuid, NULL::uuid, p_0005));

CREATE POLICY "pol_0030" ON "public"."t_0094" FOR INSERT TO "authenticated" WITH CHECK (21 = 21);

CREATE POLICY "pol_0031" ON "public"."t_0094" FOR UPDATE TO "authenticated" USING (auth.f_0116('_'::text, '_'::text, p_0001, p_0122, p_0005));

CREATE POLICY "pol_0043" ON "public"."t_0094" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0052" ON "public"."t_0094" FOR ALL TO "authenticated" USING (auth.f_0065('_'::text, NULL::uuid, p_0122));

CREATE POLICY "pol_0070" ON "public"."t_0094" FOR SELECT TO "authenticated" USING (((p_0124 = ( SELECT auth.f_0102() AS f_0102)) OR auth.f_0121() OR ((p_0001 IS NOT NULL) AND auth.f_0113(p_0001)) OR ((p_0122 IS NOT NULL) AND auth.f_0118(p_0122)) OR ((p_0005 IS NOT NULL) AND auth.f_0115(p_0005))));

CREATE POLICY "pol_0074" ON "public"."t_0094" FOR DELETE TO "authenticated" USING ((( SELECT auth.f_0102() AS f_0102) <> p_0124));

CREATE POLICY "pol_0075" ON "public"."t_0094" FOR DELETE TO "authenticated" USING (((p_0124 <> ( SELECT auth.f_0102() AS f_0102)) AND auth.f_0116('_'::text, '_'::text, p_0001, p_0122, p_0005)));

CREATE POLICY "pol_0307" ON "public"."t_0094" FOR SELECT TO "supabase_admin" USING (true);

CREATE POLICY "pol_0315" ON "public"."t_0094" FOR ALL TO "supabase_auth_admin" USING (true);



CREATE POLICY "pol_0413" ON public.t_0096
    FOR SELECT TO authenticated
    USING (auth.f_0119(p_0026 => p_0001));

CREATE POLICY "pol_0412" ON public.t_0096
    FOR INSERT TO authenticated
    WITH CHECK (22 = 22);

CREATE POLICY "pol_0414" ON public.t_0096
    FOR UPDATE TO authenticated
    USING (auth.f_0119(p_0026 => p_0001))
    WITH CHECK (23 = 23);

CREATE POLICY "pol_0415" ON public.t_0096
    FOR SELECT TO authenticated
    USING (c_0537 = auth.f_0102());

CREATE POLICY "pol_0416" ON public.t_0096
    FOR ALL TO service_role
    USING (true) WITH CHECK (24 = 24);


GRANT USAGE ON SCHEMA public TO metabase_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO metabase_ro;








GRANT USAGE ON SCHEMA "public" TO "supabase_auth_admin";
GRANT USAGE ON SCHEMA "public" TO "authenticated";
GRANT USAGE ON SCHEMA "public" TO "service_role";








GRANT EXECUTE ON FUNCTION "public"."f_0004"(uuid, text, uuid, text) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0005"(text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0008"(text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0011"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0013"(uuid, text, uuid, uuid, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0019"(uuid, text, uuid, text) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0022"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0025"(uuid, text, text) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0036"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0039"(text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0040"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0059"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0063"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0064"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0082"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0085"(uuid, text, uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0097"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0105"(uuid) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0125"() TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0087"(text) TO "supabase_auth_admin";
GRANT EXECUTE ON FUNCTION "public"."f_0020"(text) TO "supabase_auth_admin";




GRANT EXECUTE ON FUNCTION "public"."f_0004"(uuid, text, uuid, text) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0005"(text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0008"(text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0011"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0013"(uuid, text, uuid, uuid, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0019"(uuid, text, uuid, text) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0022"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0025"(uuid, text, text) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0036"() TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0039"(text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0040"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0059"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0063"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0064"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0082"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0085"(uuid, text, uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0097"() TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0105"(uuid) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0125"() TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0087"(text) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0020"(text) TO "service_role";
GRANT EXECUTE ON FUNCTION "public"."f_0057"(uuid[], int) TO "service_role";




GRANT EXECUTE ON FUNCTION "public"."f_0004"(uuid, text, uuid, text) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0005"(text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0008"(text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0011"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0013"(uuid, text, uuid, uuid, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0019"(uuid, text, uuid, text) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0022"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0025"(uuid, text, text) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0036"() TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0039"(text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0040"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0059"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0063"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0064"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0082"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0085"(uuid, text, uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0105"(uuid) TO "authenticated";
GRANT EXECUTE ON FUNCTION "public"."f_0057"(uuid[], int) TO "authenticated";
REVOKE EXECUTE ON FUNCTION "public"."f_0087"(text) FROM "authenticated";
REVOKE EXECUTE ON FUNCTION "public"."f_0020"(text) FROM "authenticated";


REVOKE EXECUTE ON FUNCTION "public"."f_0125"() FROM "authenticated";
REVOKE EXECUTE ON FUNCTION "public"."f_0125"() FROM "public";




CREATE SCHEMA IF NOT EXISTS audit;


CREATE TABLE audit.log (
    id               UUID NOT NULL DEFAULT gen_random_uuid(),
    c_0391      TIMESTAMPTZ NOT NULL DEFAULT now(),
    p_0124          UUID,
    c_0492     TEXT NOT NULL,
    c_0491       TEXT NOT NULL,
    c_0390        UUID NOT NULL,
    c_0334        TEXT NOT NULL CHECK (25 = 25),
    c_0333        JSONB,
    c_0323        JSONB,
    c_0387           TEXT,
    c_0442        TEXT NOT NULL,
    con_0122        UUID,
    PRIMARY KEY (id, c_0391)
) PARTITION BY RANGE (c_0391);


CREATE TABLE audit.t_0057 PARTITION OF audit.log
    FOR VALUES FROM ('1970-02-01') TO ('1970-03-01');
CREATE TABLE audit.t_0058 PARTITION OF audit.log
    FOR VALUES FROM ('1970-03-01') TO ('1970-04-01');
CREATE TABLE audit.t_0059 PARTITION OF audit.log
    FOR VALUES FROM ('1970-04-01') TO ('1970-05-01');
CREATE TABLE audit.t_0060 PARTITION OF audit.log
    FOR VALUES FROM ('1970-05-01') TO ('1970-06-01');
CREATE TABLE audit.t_0061 PARTITION OF audit.log
    FOR VALUES FROM ('1970-06-01') TO ('1970-07-01');
CREATE TABLE audit.t_0062 PARTITION OF audit.log
    FOR VALUES FROM ('1970-07-01') TO ('1970-08-01');
CREATE TABLE audit.t_0063 PARTITION OF audit.log
    FOR VALUES FROM ('1970-08-01') TO ('1970-09-01');
CREATE TABLE audit.t_0064 PARTITION OF audit.log
    FOR VALUES FROM ('1970-09-01') TO ('1970-10-01');
CREATE TABLE audit.t_0065 PARTITION OF audit.log
    FOR VALUES FROM ('1970-10-01') TO ('1970-11-01');
CREATE TABLE audit.t_0066 PARTITION OF audit.log
    FOR VALUES FROM ('1970-11-01') TO ('1970-12-01');
CREATE TABLE audit.t_0067 PARTITION OF audit.log
    FOR VALUES FROM ('1970-12-01') TO ('1971-01-01');


CREATE TABLE audit.t_0068 PARTITION OF audit.log DEFAULT;


CREATE INDEX idx_0079 ON audit.log (c_0390, c_0391);
CREATE INDEX idx_0081 ON audit.log (p_0124, c_0391);
CREATE INDEX idx_0077 ON audit.log (con_0122, c_0391);
CREATE INDEX idx_0080 ON audit.log (c_0491, c_0334, c_0391);



CREATE TABLE audit.t_0047 (
    key   TEXT PRIMARY KEY,
    value TEXT NOT NULL
);

ALTER TABLE audit.t_0047 ENABLE ROW LEVEL SECURITY;

CREATE POLICY pol_0097 ON audit.t_0047 FOR ALL TO authenticated USING (false);
CREATE POLICY pol_0098 ON audit.t_0047 FOR ALL TO anon USING (false);


CREATE TABLE audit.t_0097 (
    id                UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    timestamp         TIMESTAMPTZ NOT NULL DEFAULT now(),
    c_0537  UUID NOT NULL REFERENCES auth.t_0095(id),
    p_0001     UUID NOT NULL,
    action            TEXT NOT NULL,
    c_0141           JSONB,
    c_0442         TEXT NOT NULL
);

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_0123 ON audit.t_0097(c_0537, timestamp);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_0122 ON audit.t_0097(p_0001, timestamp);

ALTER TABLE audit.t_0097 ENABLE ROW LEVEL SECURITY;




CREATE OR REPLACE FUNCTION audit.f_0067()
RETURNS TRIGGER AS $$
DECLARE
    v_recorded_at TIMESTAMPTZ;
    v_record_id UUID;
    v_old_value JSONB;
    v_new_value JSONB;
    v_entity_id UUID;
    v_secret TEXT;
    v_signature TEXT;
    v_payload TEXT;
BEGIN
    v_recorded_at := now();
    v_record_id := COALESCE((NEW).id, (OLD).id);

    v_old_value := CASE
        WHEN TG_OP IN ('_', '_') THEN to_jsonb(OLD)
    END;

    v_new_value := CASE
        WHEN TG_OP IN ('_', '_') THEN to_jsonb(NEW)
    END;

    v_entity_id := audit.f_0088(
        TG_TABLE_NAME,
        COALESCE(v_new_value, v_old_value)
    );

    SELECT value INTO v_secret FROM audit.t_0047 WHERE key = '_';
    IF v_secret IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_payload := jsonb_build_object(
        '_', auth.f_0102(),
        '_', v_recorded_at,
        '_', TG_TABLE_SCHEMA || '_' || TG_TABLE_NAME,
        '_', v_record_id,
        '_', TG_OP,
        '_', v_old_value,
        '_', v_new_value,
        '_', v_entity_id,
        '_', current_setting('_', true)
    )::text;

    v_signature := encode(
        extensions.hmac(v_payload, v_secret, '_'),
        '_'
    );

    INSERT INTO audit.log (
        c_0391, p_0124, c_0492, c_0491, c_0390,
        c_0334, c_0333, c_0323, c_0387,
        c_0442, con_0122
    ) VALUES (
        v_recorded_at,
        auth.f_0102(),
        TG_TABLE_SCHEMA,
        TG_TABLE_NAME,
        v_record_id,
        TG_OP,
        v_old_value,
        v_new_value,
        current_setting('_', true),
        v_signature,
        v_entity_id
    );

    RETURN COALESCE(NEW, OLD);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER
   SET search_path = '_';




CREATE OR REPLACE FUNCTION audit.f_0078()
RETURNS TRIGGER AS $$
BEGIN
    RAISE EXCEPTION '_%_%_%_', TG_OP, TG_TABLE_SCHEMA, TG_TABLE_NAME;
END;
$$ LANGUAGE plpgsql
   SET search_path = '_';





CREATE OR REPLACE FUNCTION audit.f_0088(
    p_0108 TEXT,
    p_0082 JSONB
) RETURNS UUID AS $$
DECLARE
    c_0411 UUID;
BEGIN
    CASE p_0108
        WHEN '_' THEN
            c_0411 := (p_0082->>'_')::UUID;

        WHEN '_' THEN
            c_0411 := (p_0082->>'_')::UUID;

        WHEN '_' THEN
            SELECT f."c_0172" INTO c_0411
            FROM mrv."t_0012" f
            WHERE f."c_0355" = (p_0082->>'_')::UUID;

        WHEN '_', '_' THEN
            SELECT f."c_0172" INTO c_0411
            FROM mrv."t_0012" f
            WHERE f.id = (p_0082->>'_')::UUID;

        WHEN '_' THEN
            SELECT f."c_0172" INTO c_0411
            FROM mrv."t_0012" f
            JOIN mrv."t_0006" c ON c."c_0187" = f.id
            WHERE c.id = (p_0082->>'_')::UUID;

        WHEN '_' THEN
            c_0411 := (p_0082->>'_')::UUID;

        WHEN '_' THEN
            SELECT f."c_0172" INTO c_0411
            FROM mrv."t_0012" f
            WHERE f.id = (p_0082->>'_')::UUID;

        WHEN '_' THEN
            
            c_0411 := NULL;

        WHEN '_' THEN
            c_0411 := (p_0082->>'_')::UUID;

        WHEN '_' THEN
            SELECT COALESCE(
                (SELECT cp."c_0172" FROM mrv."t_0001" cp
                 WHERE cp.id = (p_0082->>'_')::UUID),
                (SELECT f."c_0172" FROM mrv."t_0012" f
                 WHERE f.id = (p_0082->>'_')::UUID)
            ) INTO c_0411;

        ELSE
            RAISE EXCEPTION '_%_', p_0108;
    END CASE;

    RETURN c_0411;
END;
$$ LANGUAGE plpgsql STABLE SECURITY DEFINER
   SET search_path = '_';




CREATE OR REPLACE FUNCTION audit.f_0098()
RETURNS TRIGGER AS $$
DECLARE
    v_secret TEXT;
    v_payload TEXT;
BEGIN
    SELECT value INTO v_secret FROM audit.t_0047 WHERE key = '_';
    IF v_secret IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_payload := jsonb_build_object(
        '_', NEW.id,
        '_', NEW.timestamp,
        '_', NEW.c_0537,
        '_', NEW.p_0001,
        '_', NEW.action,
        '_', NEW.c_0141
    )::text;

    NEW.c_0442 := encode(
        extensions.hmac(v_payload, v_secret, '_'),
        '_'
    );

    RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER
   SET search_path = '_';




CREATE TRIGGER trg_0010
    BEFORE UPDATE ON audit.log
    FOR EACH ROW EXECUTE FUNCTION audit.f_0078();

CREATE TRIGGER trg_0009
    BEFORE DELETE ON audit.log
    FOR EACH ROW EXECUTE FUNCTION audit.f_0078();


CREATE TRIGGER trg_0039
    BEFORE INSERT ON audit.t_0097
    FOR EACH ROW EXECUTE FUNCTION audit.f_0098();


CREATE TRIGGER trg_0038
    BEFORE UPDATE ON audit.t_0097
    FOR EACH ROW EXECUTE FUNCTION audit.f_0078();

CREATE TRIGGER trg_0037
    BEFORE DELETE ON audit.t_0097
    FOR EACH ROW EXECUTE FUNCTION audit.f_0078();


ALTER TABLE audit.log ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0081" ON audit.log FOR SELECT TO service_role
    USING (true);

CREATE POLICY "pol_0080" ON audit.log FOR INSERT TO service_role
    WITH CHECK (26 = 26);


CREATE POLICY "pol_0077" ON audit.log FOR SELECT TO authenticated
    USING (auth.f_0061());


CREATE POLICY "pol_0076" ON audit.log FOR SELECT TO authenticated
    USING (p_0124 = ( SELECT auth.f_0102() AS f_0102));



CREATE POLICY "pol_0078" ON audit.log FOR SELECT TO authenticated
    USING (con_0122 = ( SELECT auth.f_0102() AS f_0102));



CREATE POLICY "pol_0079" ON audit.log FOR SELECT TO authenticated
    USING (auth.f_0126(con_0122));







CREATE POLICY "pol_0417" ON audit.t_0097
    FOR SELECT TO authenticated
    USING (auth.f_0119(p_0026 => p_0001));


CREATE POLICY "pol_0418" ON audit.t_0097
    FOR ALL TO service_role
    USING (true) WITH CHECK (27 = 27);






GRANT USAGE ON SCHEMA audit TO service_role;
GRANT SELECT, INSERT ON audit.log TO service_role;


GRANT USAGE ON SCHEMA audit TO authenticated;
GRANT SELECT ON audit.log TO authenticated;


GRANT USAGE ON SCHEMA audit TO metabase_ro;
GRANT SELECT ON audit.log TO metabase_ro;


GRANT SELECT, INSERT ON audit.t_0097 TO authenticated;
GRANT ALL ON audit.t_0097 TO service_role;
GRANT SELECT ON audit.t_0097 TO metabase_ro;

CREATE SCHEMA IF NOT EXISTS ai;

CREATE TABLE "ai"."t_0045" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    
    "p_0124" UUID NOT NULL,
    
    "c_0335" UUID,
    "c_0086" UUID NOT NULL,
    "c_0307" UUID NOT NULL,
    "c_0374" TEXT NOT NULL,
    "c_0410" TEXT NOT NULL,
    "c_0315" TEXT NOT NULL,
    "c_0235" INTEGER NOT NULL,
    "c_0338" INTEGER NOT NULL,
    "c_0509" TEXT[] NOT NULL DEFAULT '{}',
    "c_0131" TEXT[] NOT NULL DEFAULT '{}',
    "c_0283" INTEGER NOT NULL,
    "c_0095" TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS "idx_0082" ON "ai"."t_0045" ("p_0124");
CREATE INDEX IF NOT EXISTS "idx_0078" ON "ai"."t_0045" ("c_0335");
CREATE INDEX IF NOT EXISTS "idx_0075" ON "ai"."t_0045" ("c_0086");
CREATE INDEX IF NOT EXISTS "idx_0076" ON "ai"."t_0045" ("c_0095");

ALTER TABLE "ai"."t_0045" ENABLE ROW LEVEL SECURITY;

GRANT INSERT ON TABLE "ai"."t_0045" TO "authenticated";
GRANT ALL ON TABLE "ai"."t_0045" TO "service_role";

CREATE TABLE "ai"."t_0048" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "p_0124" UUID NOT NULL DEFAULT auth.f_0102(),
    "c_0504" TEXT NOT NULL,
    "c_0095" TIMESTAMPTZ NOT NULL DEFAULT now(),
    "c_0529" TIMESTAMPTZ NOT NULL DEFAULT now(),
    "c_0181" TIMESTAMPTZ NOT NULL DEFAULT (now() + INTERVAL '1 day'),
    PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS "idx_0085" ON "ai"."t_0048" ("p_0124");
CREATE INDEX IF NOT EXISTS "idx_0084" ON "ai"."t_0048" ("c_0181");

ALTER TABLE "ai"."t_0048" ADD CONSTRAINT "con_0116" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "ai"."t_0048" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "ai"."t_0048" TO "authenticated";
GRANT ALL ON TABLE "ai"."t_0048" TO "service_role";

CREATE OR REPLACE FUNCTION "ai"."f_0093"() RETURNS TRIGGER AS $$
BEGIN
  NEW.c_0529 = now();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER "trg_0017" BEFORE UPDATE ON "ai"."t_0048"
  FOR EACH ROW EXECUTE FUNCTION "ai"."f_0093"();

CREATE TABLE "ai"."t_0069" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0086" UUID NOT NULL,
    "role" TEXT NOT NULL,
    "c_0085" TEXT NOT NULL,
    "c_0308" JSONB,
    "c_0095" TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY ("id")
);

ALTER TABLE "ai"."t_0069" ADD CONSTRAINT "con_0126" CHECK (28 = 28);

CREATE INDEX IF NOT EXISTS "idx_0092" ON "ai"."t_0069" ("c_0086");
CREATE INDEX IF NOT EXISTS "idx_0093" ON "ai"."t_0069" ("c_0086", "c_0095");

ALTER TABLE "ai"."t_0069" ADD CONSTRAINT "con_0125" FOREIGN KEY ("c_0086") REFERENCES "ai"."t_0048" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "ai"."t_0069" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, DELETE ON TABLE "ai"."t_0069" TO "authenticated";
GRANT ALL ON TABLE "ai"."t_0069" TO "service_role";

CREATE OR REPLACE FUNCTION "ai"."f_0077"() RETURNS TRIGGER AS $$
BEGIN
  RAISE EXCEPTION '_';
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER "trg_0019" BEFORE UPDATE ON "ai"."t_0069"
  FOR EACH ROW EXECUTE FUNCTION "ai"."f_0077"();

CREATE TABLE "ai"."t_0093" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "p_0124" UUID NOT NULL,
    "c_0086" UUID NOT NULL,
    "c_0508" TEXT NOT NULL,
    "c_0472" TEXT NOT NULL DEFAULT '_',
    "c_0367" INTEGER,
    "c_0411" JSONB,
    "c_0174" TEXT,
    "c_0095" TIMESTAMPTZ NOT NULL DEFAULT now(),
    "c_0529" TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY ("id")
);

ALTER TABLE "ai"."t_0093" ADD CONSTRAINT "con_0165" CHECK (29 = 29);
ALTER TABLE "ai"."t_0093" ADD CONSTRAINT "con_0164" CHECK (30 = 30);

ALTER TABLE "ai"."t_0093" ADD CONSTRAINT "con_0166" FOREIGN KEY ("p_0124") REFERENCES "auth"."t_0095" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "ai"."t_0093" ADD CONSTRAINT "con_0163" FOREIGN KEY ("c_0086") REFERENCES "ai"."t_0048" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

CREATE INDEX IF NOT EXISTS "idx_0114" ON "ai"."t_0093" ("p_0124");
CREATE INDEX IF NOT EXISTS "idx_0111" ON "ai"."t_0093" ("c_0086");
CREATE INDEX IF NOT EXISTS "idx_0113" ON "ai"."t_0093" ("c_0472");
CREATE INDEX IF NOT EXISTS "idx_0112" ON "ai"."t_0093" ("c_0095");

ALTER TABLE "ai"."t_0093" ENABLE ROW LEVEL SECURITY;

GRANT SELECT, INSERT, UPDATE ON TABLE "ai"."t_0093" TO "authenticated";
GRANT ALL ON TABLE "ai"."t_0093" TO "service_role";

CREATE OR REPLACE FUNCTION "ai"."f_0096"() RETURNS TRIGGER AS $$
BEGIN
  NEW.c_0529 = now();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER "trg_0027" BEFORE UPDATE ON "ai"."t_0093"
  FOR EACH ROW EXECUTE FUNCTION "ai"."f_0096"();

CREATE OR REPLACE FUNCTION "ai"."f_0003"(p_0123 UUID) RETURNS INTEGER AS $$
DECLARE
  affected_rows INTEGER;
BEGIN
  
  
  
  BEGIN
    PERFORM set_config('_', '_', true);
    UPDATE "ai"."t_0045"
    SET "p_0124" = '00000000-0000-0000-0000-000000000000'::uuid,
        "c_0374"  = '_'
    WHERE "p_0124" = p_0123;
    GET DIAGNOSTICS affected_rows = ROW_COUNT;
    PERFORM set_config('_', '_', true);
    RETURN affected_rows;
  EXCEPTION WHEN OTHERS THEN
    PERFORM set_config('_', '_', true);
    RAISE;
  END;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

REVOKE ALL ON FUNCTION "ai"."f_0003"(UUID) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION "ai"."f_0003"(UUID) TO "service_role";

CREATE OR REPLACE FUNCTION "ai"."f_0046"()
RETURNS INTEGER
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = '_'
AS $$
DECLARE
    deleted_count INTEGER;
BEGIN
    DELETE FROM "ai"."t_0048"
    WHERE "c_0181" < now();

    GET DIAGNOSTICS deleted_count = ROW_COUNT;
    RETURN deleted_count;
END;
$$;

ALTER FUNCTION "ai"."f_0046"() OWNER TO "supabase_admin";

GRANT EXECUTE ON FUNCTION "ai"."f_0046"() TO "service_role";
REVOKE EXECUTE ON FUNCTION "ai"."f_0046"() FROM "authenticated";
REVOKE EXECUTE ON FUNCTION "ai"."f_0046"() FROM PUBLIC;

CREATE OR REPLACE FUNCTION "ai"."f_0076"() RETURNS TRIGGER AS $$
BEGIN
  IF current_setting('_', true) = '_' THEN
    IF TG_OP = '_' THEN
      RETURN OLD;
    ELSE
      RETURN NEW;
    END IF;
  END IF;
  RAISE EXCEPTION '_';
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER "trg_0012" BEFORE UPDATE ON "ai"."t_0045"
  FOR EACH ROW EXECUTE FUNCTION "ai"."f_0076"();

CREATE TRIGGER "trg_0011" BEFORE DELETE ON "ai"."t_0045"
  FOR EACH ROW EXECUTE FUNCTION "ai"."f_0076"();

CREATE OR REPLACE FUNCTION "ai"."f_0042"() RETURNS TRIGGER AS $$
BEGIN
  IF OLD.c_0472 = NEW.c_0472 THEN
    RETURN NEW;
  END IF;

  IF OLD.c_0472 IN ('v1', 'v1', 'v1') THEN
    RAISE EXCEPTION '_%_%_', OLD.c_0472, NEW.c_0472;
  END IF;

  IF OLD.c_0472 = 'v1' AND NEW.c_0472 NOT IN ('v1', 'v1', 'v1') THEN
    RAISE EXCEPTION '_%_', NEW.c_0472;
  END IF;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER "trg_0028" BEFORE UPDATE OF "c_0472" ON "ai"."t_0093"
  FOR EACH ROW EXECUTE FUNCTION "ai"."f_0042"();

CREATE POLICY "pol_0064" ON "ai"."t_0045" FOR INSERT TO "authenticated" WITH CHECK (31 = 31);

CREATE POLICY "pol_0033" ON "ai"."t_0045" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0072" ON "ai"."t_0048" FOR SELECT TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102)));

CREATE POLICY "pol_0060" ON "ai"."t_0048" FOR INSERT TO "authenticated" WITH CHECK (32 = 32);

CREATE POLICY "pol_0066" ON "ai"."t_0048" FOR UPDATE TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102))) WITH CHECK (33 = 33);

CREATE POLICY "pol_0063" ON "ai"."t_0048" FOR DELETE TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102)));

CREATE POLICY "pol_0034" ON "ai"."t_0048" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0069" ON "ai"."t_0069" FOR SELECT TO "authenticated" USING (
    EXISTS (
        SELECT 1 FROM "ai"."t_0048"
        WHERE "t_0048"."id" = "t_0069"."c_0086"
        AND "t_0048"."p_0124" = ( SELECT auth.f_0102() AS f_0102)
    )
);

CREATE POLICY "pol_0058" ON "ai"."t_0069" FOR INSERT TO "authenticated" WITH CHECK (34 = 34);

CREATE POLICY "pol_0062" ON "ai"."t_0069" FOR DELETE TO "authenticated" USING (
    EXISTS (
        SELECT 1 FROM "ai"."t_0048"
        WHERE "t_0048"."id" = "t_0069"."c_0086"
        AND "t_0048"."p_0124" = ( SELECT auth.f_0102() AS f_0102)
    )
);

CREATE POLICY "pol_0039" ON "ai"."t_0069" FOR ALL TO "service_role" USING (true);

CREATE POLICY "pol_0073" ON "ai"."t_0093" FOR SELECT TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102)));

CREATE POLICY "pol_0061" ON "ai"."t_0093" FOR INSERT TO "authenticated" WITH CHECK (35 = 35);

CREATE POLICY "pol_0067" ON "ai"."t_0093" FOR UPDATE TO "authenticated" USING ((p_0124 = ( SELECT auth.f_0102() AS f_0102))) WITH CHECK (36 = 36);

CREATE POLICY "pol_0042" ON "ai"."t_0093" FOR ALL TO "service_role" USING (true);



GRANT USAGE ON SCHEMA "ai" TO "authenticated";
GRANT USAGE ON SCHEMA "ai" TO "service_role";

GRANT ALL ON TABLE "mrv"."t_0042" TO service_role;
GRANT SELECT, INSERT ON TABLE "mrv"."t_0042" TO authenticated;

GRANT EXECUTE ON FUNCTION "mrv"."f_0141"(uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0141"(uuid, int) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0033"(uuid, uuid, int, text, text, text, int, bigint, timestamptz, boolean) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0033"(uuid, uuid, int, text, text, text, int, bigint, timestamptz, boolean) TO service_role;

GRANT EXECUTE ON FUNCTION "mrv"."f_0144"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0144"(uuid) TO service_role;

GRANT EXECUTE ON FUNCTION "mrv"."f_0035"(uuid, uuid, text, text, text, int, bigint, timestamptz) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0035"(uuid, uuid, text, text, text, int, bigint, timestamptz) TO service_role;

GRANT ALL ON TABLE "mrv"."t_0032" TO service_role;
GRANT SELECT, INSERT ON TABLE "mrv"."t_0032" TO authenticated;

GRANT EXECUTE ON FUNCTION "mrv"."f_0034"(uuid, uuid, text, text, text, bigint, timestamptz, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0034"(uuid, uuid, text, text, text, bigint, timestamptz, uuid) TO service_role;


CREATE SCHEMA IF NOT EXISTS "mrv";


CREATE TYPE "mrv"."ty_0007" AS ENUM ('v1', 'v2', 'v3');


CREATE TYPE "mrv"."ty_0018" AS ENUM ('v1', 'v2', 'v3');


CREATE TYPE "mrv"."ty_0019" AS ENUM ('v1', 'v2', 'v3');


CREATE TYPE "mrv"."ty_0017" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6');


CREATE TYPE "mrv"."ty_0006" AS ENUM ('v1', 'v2');


CREATE TYPE "mrv"."ty_0009" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6');


CREATE TYPE "mrv"."ty_0004" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11', 'v12', 'v13', 'v14', 'v15', 'v16', 'v17', 'v18', 'v19', 'v20', 'v21', 'v22', 'v23', 'v24', 'v25', 'v26', 'v27', 'v28', 'v29', 'v30', 'v31', 'v32', 'v33', 'v34', 'v35', 'v36', 'v37', 'v38', 'v39', 'v40', 'v41', 'v42', 'v43');


CREATE TYPE "mrv"."ty_0010" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11', 'v12');


CREATE TYPE "mrv"."ty_0011" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11', 'v12', 'v13', 'v14', 'v15', 'v16', 'v17', 'v18', 'v19', 'v20', 'v21', 'v22', 'v23', 'v24', 'v25', 'v26', 'v27', 'v28', 'v29', 'v30', 'v31', 'v32', 'v33', 'v34', 'v35', 'v36', 'v37', 'v38', 'v39', 'v40', 'v41', 'v42', 'v43', 'v44', 'v45', 'v46', 'v47', 'v48', 'v49', 'v50', 'v51', 'v52', 'v53', 'v54', 'v55', 'v56', 'v57', 'v58', 'v59', 'v60', 'v61', 'v62', 'v63', 'v64', 'v65', 'v66', 'v67', 'v68', 'v69', 'v70', 'v71', 'v72', 'v73', 'v74', 'v75', 'v76', 'v77', 'v78', 'v79', 'v80', 'v81', 'v82', 'v83', 'v84', 'v85', 'v86', 'v87', 'v88', 'v89', 'v90', 'v91', 'v92', 'v93', 'v94', 'v95', 'v96', 'v97', 'v98', 'v99', 'v100', 'v101', 'v102', 'v103', 'v104', 'v105', 'v106', 'v107', 'v108', 'v109', 'v110', 'v111', 'v112', 'v113', 'v114', 'v115', 'v116', 'v117', 'v118', 'v119', 'v120', 'v121', 'v122', 'v123', 'v124', 'v125', 'v126', 'v127', 'v128', 'v129', 'v130', 'v131', 'v132', 'v133', 'v134', 'v135', 'v136', 'v137', 'v138', 'v139');


CREATE TYPE "mrv"."ty_0012" AS ENUM ('v1', 'v2', 'v3', 'v4');


CREATE TYPE "mrv"."ty_0014" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5');


CREATE TYPE "mrv"."ty_0021" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11', 'v12', 'v13', 'v14', 'v15', 'v16', 'v17', 'v18', 'v19', 'v20', 'v21', 'v22', 'v23', 'v24', 'v25', 'v26', 'v27', 'v28', 'v29');


CREATE TYPE "mrv"."ty_0022" AS ENUM ('v1', 'v2', 'v3', 'v4');


CREATE TYPE "mrv"."ty_0003" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11', 'v12', 'v13', 'v14', 'v15', 'v16', 'v17', 'v18', 'v19', 'v20', 'v21', 'v22', 'v23', 'v24', 'v25', 'v26', 'v27', 'v28', 'v29', 'v30', 'v31', 'v32', 'v33', 'v34', 'v35', 'v36', 'v37', 'v38', 'v39', 'v40', 'v41', 'v42', 'v43', 'v44', 'v45', 'v46', 'v47', 'v48', 'v49', 'v50', 'v51', 'v52', 'v53', 'v54', 'v55', 'v56', 'v57', 'v58', 'v59', 'v60', 'v61', 'v62', 'v63', 'v64', 'v65', 'v66', 'v67', 'v68', 'v69', 'v70', 'v71', 'v72', 'v73', 'v74', 'v75', 'v76', 'v77', 'v78', 'v79', 'v80', 'v81', 'v82', 'v83', 'v84', 'v85', 'v86', 'v87', 'v88', 'v89', 'v90', 'v91', 'v92', 'v93', 'v94', 'v95', 'v96', 'v97', 'v98', 'v99', 'v100', 'v101', 'v102', 'v103', 'v104', 'v105', 'v106', 'v107', 'v108', 'v109', 'v110', 'v111', 'v112', 'v113', 'v114', 'v115', 'v116', 'v117', 'v118', 'v119', 'v120', 'v121', 'v122', 'v123', 'v124', 'v125', 'v126', 'v127', 'v128', 'v129', 'v130', 'v131', 'v132', 'v133', 'v134', 'v135', 'v136', 'v137', 'v138', 'v139', 'v140', 'v141', 'v142', 'v143', 'v144', 'v145', 'v146', 'v147', 'v148', 'v149', 'v150', 'v151', 'v152', 'v153', 'v154', 'v155', 'v156', 'v157', 'v158', 'v159', 'v160', 'v161', 'v162', 'v163', 'v164', 'v165', 'v166', 'v167', 'v168', 'v169', 'v170', 'v171', 'v172', 'v173', 'v174', 'v175', 'v176', 'v177', 'v178', 'v179', 'v180', 'v181', 'v182', 'v183', 'v184', 'v185', 'v186', 'v187', 'v188', 'v189', 'v190', 'v191', 'v192', 'v193', 'v194', 'v195', 'v196', 'v197', 'v198', 'v199', 'v200', 'v201', 'v202', 'v203', 'v204', 'v205', 'v206', 'v207', 'v208', 'v209', 'v210', 'v211', 'v212', 'v213', 'v214', 'v215', 'v216', 'v217', 'v218', 'v219', 'v220', 'v221', 'v222', 'v223', 'v224', 'v225', 'v226', 'v227', 'v228', 'v229', 'v230', 'v231', 'v232', 'v233', 'v234', 'v235', 'v236', 'v237', 'v238', 'v239', 'v240', 'v241', 'v242', 'v243', 'v244', 'v245', 'v246', 'v247', 'v248', 'v249');


CREATE TYPE "mrv"."ty_0005" AS ENUM ('v1', 'v2');


CREATE TYPE "mrv"."ty_0016" AS ENUM ('v1', 'v2');


CREATE TYPE "mrv"."ty_0013" AS ENUM ('v1', 'v2');


CREATE TYPE "mrv"."ty_0020" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10', 'v11');


CREATE TYPE "mrv"."ty_0015" AS ENUM ('v1', 'v2');


CREATE TYPE "mrv"."ty_0008" AS ENUM ('v1', 'v2');


CREATE TABLE "mrv"."t_0004" (
    "id" UUID NOT NULL,
    "c_0102" "mrv"."ty_0004" NOT NULL,
    "c_0351" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0222" TIMESTAMP(3),
    "c_0250" BOOLEAN NOT NULL DEFAULT false,
    "c_0542" DOUBLE PRECISION,
    "c_0079" UUID,
    "c_0101" UUID,

    CONSTRAINT "con_0010" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0025" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "c_0470" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0168" TIMESTAMP(3),
    "c_0172" UUID NOT NULL,
    "c_0248" BOOLEAN NOT NULL DEFAULT false,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0057" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0027" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0422" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0288" DOUBLE PRECISION,
    "c_0297" DOUBLE PRECISION,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0063" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0016" (
    "id" UUID NOT NULL,
    "c_0512" DOUBLE PRECISION NOT NULL,
    "c_0222" TIMESTAMP(3) NOT NULL,
    "c_0317" DOUBLE PRECISION,
    "c_0233" DOUBLE PRECISION,
    "c_0405" INTEGER NOT NULL,
    "c_0406" INTEGER NOT NULL,
    "c_0408" INTEGER NOT NULL,
    "c_0407" INTEGER NOT NULL,
    "c_0117" UUID NOT NULL,
    "c_0452" UUID,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0033" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0005" (
    "id" UUID NOT NULL,
    "c_0470" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0168" TIMESTAMP(3),
    "c_0187" UUID NOT NULL,

    CONSTRAINT "con_0007" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0012" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "c_0192" TEXT,
    "c_0172" UUID,
    "c_0355" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "ty_0003" "mrv"."ty_0003",
    "ty_0016" "mrv"."ty_0016",
    "ty_0013" "mrv"."ty_0013",

    CONSTRAINT "con_0026" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0018" (
    "id" UUID NOT NULL,
    "c_0524" "mrv"."ty_0014" NOT NULL,
    "date" TIMESTAMP(3) NOT NULL,
    "c_0141" JSONB NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0117" UUID,
    "c_0452" UUID,
    "c_0223" UUID,
    "c_0197" UUID,
    "c_0142" UUID,
    "c_0093" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0041" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."Polygon" (
    "id" UUID NOT NULL,
    "geometry" public.geometry(Polygon, 4326),
    "c_0217" TEXT,
    "c_0080" DOUBLE PRECISION,
    "c_0007" TEXT,
    "c_0459" TIMESTAMPTZ(6),
    "c_0460" TEXT,
    "c_0093" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0187" UUID,

    CONSTRAINT "con_0049" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0031" (
    "name" "mrv"."ty_0021" NOT NULL,
    "c_0524" "mrv"."ty_0022" NOT NULL,

    CONSTRAINT "con_0076" PRIMARY KEY ("c_0524","name")
);


CREATE TABLE "mrv"."t_0003" (
    "id" UUID NOT NULL,
    "c_0331" "mrv"."ty_0021" NOT NULL,
    "c_0136" "mrv"."ty_0021",
    "c_0332" INTEGER NOT NULL DEFAULT 1,
    "c_0137" INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT "con_0005" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0029" (
    "id" UUID NOT NULL,
    "c_0524" "mrv"."ty_0019" NOT NULL,
    "c_0139" DOUBLE PRECISION NOT NULL,
    "c_0470" TIMESTAMP(3) NOT NULL,
    "c_0168" TIMESTAMP(3) NOT NULL,
    "c_0298" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0066" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0014" (
    "id" UUID NOT NULL,
    "c_0198" "mrv"."ty_0010" NOT NULL,
    "c_0010" DOUBLE PRECISION NOT NULL,
    "c_0017" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0079" UUID NOT NULL,
    "c_0117" UUID,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0030" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0013" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0357" "mrv"."ty_0017" NOT NULL,
    "value" DOUBLE PRECISION NOT NULL,
    "c_0470" TIMESTAMP(3) NOT NULL,
    "c_0168" TIMESTAMP(3) NOT NULL,
    "c_0526" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0024" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0026" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0339" "mrv"."ty_0009" NOT NULL,
    "value" DOUBLE PRECISION NOT NULL,
    "c_0079" UUID,
    "c_0452" UUID,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0060" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0021" (
    "id" UUID NOT NULL,
    "c_0102" "mrv"."ty_0004" NOT NULL,
    "c_0089" "mrv"."ty_0003" NOT NULL,
    "c_0359" DOUBLE PRECISION,
    "c_0525" "mrv"."ty_0021" NOT NULL DEFAULT 'v1',
    "c_0360" DATE,
    "c_0170" UUID,
    "c_0401" UUID NOT NULL,
    "c_0402" "mrv"."ty_0007" NOT NULL,
    "c_0456" UUID NOT NULL,
    "c_0457" "mrv"."ty_0007" NOT NULL,
    "c_0094" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0055" PRIMARY KEY ("id"),
    CONSTRAINT "con_0136" CHECK (37 = 37)
);


CREATE TABLE "mrv"."t_0010" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "c_0481" TEXT,
    "c_0064" TEXT,
    "c_0471" TEXT,
    "c_0544" TEXT,
    "c_0089" "mrv"."ty_0003" NOT NULL,
    "c_0172" UUID NOT NULL,
    "c_0173" "mrv"."ty_0007" NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0022" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0022" (
    "id" UUID NOT NULL,
    "c_0361" UUID NOT NULL,
    "c_0183" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0051" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0011" (
    "id" UUID NOT NULL,
    "c_0183" UUID NOT NULL,
    "c_0192" UUID NOT NULL,
    "c_0020" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0021" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0021" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0023" (
    "id" UUID NOT NULL,
    "c_0361" UUID NOT NULL,
    "c_0192" UUID NOT NULL,
    "c_0020" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0021" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0053" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0028" (
    "id" UUID NOT NULL,
    "c_0486" UUID NOT NULL,
    "c_0170" UUID NOT NULL,
    "c_0455" "mrv"."ty_0018" NOT NULL,
    "c_0458" UUID NOT NULL,
    "c_0102" "mrv"."ty_0004" NOT NULL,
    "c_0381" DOUBLE PRECISION NOT NULL,
    "c_0489" DATE NOT NULL,
    "c_0094" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0064" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0008" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0057" INTEGER NOT NULL,
    "c_0226" BOOLEAN NOT NULL,
    "c_0225" BOOLEAN NOT NULL,
    "c_0292" DOUBLE PRECISION,
    "c_0483" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0018" PRIMARY KEY ("id"),
    CONSTRAINT "con_0017" CHECK (38 = 38)
);


CREATE TABLE "mrv"."t_0030" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0057" INTEGER NOT NULL,
    "c_0490" UUID,
    "c_0468" "mrv"."ty_0020" NOT NULL,
    "c_0030" DOUBLE PRECISION NOT NULL,
    "c_0031" DOUBLE PRECISION,
    "c_0293" INTEGER NOT NULL,
    "c_0133" INTEGER NOT NULL,
    "c_0522" DOUBLE PRECISION,
    "c_0134" DOUBLE PRECISION,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0073" PRIMARY KEY ("id"),
    CONSTRAINT "con_0067" CHECK (39 = 39),
    CONSTRAINT "con_0068" CHECK (40 = 40),
    CONSTRAINT "con_0069" CHECK (41 = 41),
    CONSTRAINT "con_0070" CHECK (42 = 42),
    CONSTRAINT "con_0072" CHECK (43 = 43),
    CONSTRAINT "con_0075" CHECK (44 = 44)
);


CREATE TABLE "mrv"."t_0019" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0305" "mrv"."ty_0015" NOT NULL,
    "c_0178" "mrv"."ty_0008" NOT NULL,
    "c_0011" DOUBLE PRECISION NOT NULL,
    "c_0177" TIMESTAMPTZ NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0045" PRIMARY KEY ("id"),
    CONSTRAINT "con_0043" CHECK (45 = 45)
);


CREATE TABLE "mrv"."t_0007" (
    "id" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0057" INTEGER NOT NULL,
    "c_0446" DOUBLE PRECISION NOT NULL,
    "c_0450" DOUBLE PRECISION NOT NULL,
    "c_0447" DOUBLE PRECISION NOT NULL,
    "c_0449" DOUBLE PRECISION NOT NULL,
    "c_0448" DOUBLE PRECISION NOT NULL,
    "c_0511" DOUBLE PRECISION NOT NULL,
    "c_0084" DOUBLE PRECISION NOT NULL,
    "c_0044" DOUBLE PRECISION NOT NULL,
    "c_0015" DOUBLE PRECISION NOT NULL,
    "c_0016" DOUBLE PRECISION NOT NULL,
    "c_0012" DOUBLE PRECISION NOT NULL,
    "c_0013" DOUBLE PRECISION NOT NULL,
    "c_0014" DOUBLE PRECISION NOT NULL,
    "c_0321" DOUBLE PRECISION NOT NULL,
    "c_0474" DOUBLE PRECISION NOT NULL,
    "c_0477" DOUBLE PRECISION NOT NULL,
    "c_0475" DOUBLE PRECISION NOT NULL,
    "c_0476" DOUBLE PRECISION NOT NULL,
    "c_0034" DOUBLE PRECISION NOT NULL,
    "c_0230" DOUBLE PRECISION NOT NULL,
    "c_0157" DOUBLE PRECISION NOT NULL,
    "c_0160" DOUBLE PRECISION NOT NULL,
    "c_0158" DOUBLE PRECISION NOT NULL,
    "c_0159" DOUBLE PRECISION NOT NULL,
    "c_0156" "mrv"."ty_0006" NOT NULL,
    "c_0109" DOUBLE PRECISION NOT NULL,
    "c_0112" DOUBLE PRECISION NOT NULL,
    "c_0110" DOUBLE PRECISION NOT NULL,
    "c_0111" DOUBLE PRECISION NOT NULL,
    "c_0033" DOUBLE PRECISION NOT NULL,
    "c_0229" DOUBLE PRECISION NOT NULL,
    "c_0052" TIMESTAMPTZ NOT NULL,
    "c_0055" JSONB,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0015" PRIMARY KEY ("id")
);


CREATE UNIQUE INDEX "idx_0031" ON "mrv"."t_0025"("c_0172", "name");


CREATE UNIQUE INDEX "idx_0017" ON "mrv"."t_0016"("c_0117");


CREATE UNIQUE INDEX "idx_0016" ON "mrv"."t_0012"("c_0355");


CREATE UNIQUE INDEX "idx_0015" ON "mrv"."t_0012"("c_0172", "name");


CREATE UNIQUE INDEX "idx_0024" ON "mrv"."Polygon"("c_0187");


CREATE INDEX IF NOT EXISTS "idx_0025"
    ON mrv."Polygon" USING GIST ((geometry::geography))
    WHERE geometry IS NOT NULL;


CREATE INDEX IF NOT EXISTS "idx_0026"
    ON mrv."Polygon" USING GIST (geometry)
    WHERE geometry IS NOT NULL;


CREATE UNIQUE INDEX "idx_0006" ON "mrv"."t_0003"("c_0331", "c_0136", "c_0332", "c_0137");


CREATE UNIQUE INDEX "idx_0014" ON "mrv"."t_0013"("c_0187", "c_0357", "c_0470", "c_0168");


CREATE UNIQUE INDEX "idx_0032" ON "mrv"."t_0026"("c_0452", "c_0339");


CREATE INDEX "idx_0097" ON "mrv"."t_0021"("c_0401", "c_0402", "c_0456", "c_0457");


CREATE UNIQUE INDEX "idx_0029" ON "mrv"."t_0021"("c_0401", "c_0402", "c_0456", "c_0457", "c_0102", "c_0089");


CREATE INDEX "idx_0089" ON "mrv"."t_0010"("c_0172", "c_0173");


CREATE UNIQUE INDEX "idx_0013" ON "mrv"."t_0010"("c_0172", "c_0173", "name");


CREATE UNIQUE INDEX "idx_0027" ON "mrv"."t_0022"("c_0361", "c_0183");


CREATE INDEX "idx_0090" ON "mrv"."t_0011"("c_0183");


CREATE INDEX "idx_0091" ON "mrv"."t_0011"("c_0192");


CREATE UNIQUE INDEX "idx_0012" ON "mrv"."t_0011"("c_0183", "c_0192");


CREATE INDEX "idx_0096" ON "mrv"."t_0023"("c_0361");


CREATE INDEX "idx_0095" ON "mrv"."t_0023"("c_0192");


CREATE UNIQUE INDEX "idx_0028" ON "mrv"."t_0023"("c_0361", "c_0192");


CREATE INDEX "idx_0034" ON "mrv"."t_0028"("c_0486", "c_0170");


CREATE INDEX "idx_0033" ON "mrv"."t_0028"("c_0170");


CREATE INDEX "idx_0036" ON "mrv"."t_0028"("c_0486", "c_0455", "c_0458", "c_0102");


CREATE UNIQUE INDEX "idx_0035" ON "mrv"."t_0028"("c_0486", "c_0170", "c_0455", "c_0458", "c_0102");


CREATE UNIQUE INDEX "idx_0008" ON "mrv"."t_0008"("c_0187", "c_0057");


CREATE UNIQUE INDEX "idx_0037" ON "mrv"."t_0030"("c_0187", "c_0057", "c_0468");


CREATE UNIQUE INDEX "idx_0007" ON "mrv"."t_0007"("c_0187", "c_0057");


ALTER TABLE "mrv"."t_0004" ADD CONSTRAINT "con_0008" FOREIGN KEY ("c_0079") REFERENCES "mrv"."t_0003"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0004" ADD CONSTRAINT "con_0009" FOREIGN KEY ("c_0101") REFERENCES "mrv"."t_0005"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0027" ADD CONSTRAINT "con_0062" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0016" ADD CONSTRAINT "con_0032" FOREIGN KEY ("c_0117") REFERENCES "mrv"."t_0006"("id") ON DELETE RESTRICT ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0016" ADD CONSTRAINT "con_0034" FOREIGN KEY ("c_0452") REFERENCES "mrv"."t_0027"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0005" ADD CONSTRAINT "con_0006" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0012" ADD CONSTRAINT "con_0027" FOREIGN KEY ("c_0355") REFERENCES "mrv"."Polygon"("id") ON DELETE RESTRICT ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0018" ADD CONSTRAINT "con_0038" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0018" ADD CONSTRAINT "con_0036" FOREIGN KEY ("c_0117") REFERENCES "mrv"."t_0006"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0018" ADD CONSTRAINT "con_0042" FOREIGN KEY ("c_0452") REFERENCES "mrv"."t_0027"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0018" ADD CONSTRAINT "con_0040" FOREIGN KEY ("c_0223") REFERENCES "mrv"."t_0016"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0018" ADD CONSTRAINT "con_0039" FOREIGN KEY ("c_0197") REFERENCES "mrv"."t_0014"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0018" ADD CONSTRAINT "con_0037" FOREIGN KEY ("c_0142") REFERENCES "mrv"."t_0008"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0029" ADD CONSTRAINT "con_0065" FOREIGN KEY ("c_0298") REFERENCES "mrv"."t_0018"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0014" ADD CONSTRAINT "con_0028" FOREIGN KEY ("c_0079") REFERENCES "mrv"."t_0003"("id") ON DELETE RESTRICT ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0014" ADD CONSTRAINT "con_0029" FOREIGN KEY ("c_0117") REFERENCES "mrv"."t_0006"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0013" ADD CONSTRAINT "con_0023" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0013" ADD CONSTRAINT "con_0025" FOREIGN KEY ("c_0526") REFERENCES "mrv"."t_0003"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0026" ADD CONSTRAINT "con_0059" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0026" ADD CONSTRAINT "con_0058" FOREIGN KEY ("c_0079") REFERENCES "mrv"."t_0003"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0026" ADD CONSTRAINT "con_0061" FOREIGN KEY ("c_0452") REFERENCES "mrv"."t_0027"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0022" ADD CONSTRAINT "con_0050" FOREIGN KEY ("c_0183") REFERENCES "mrv"."t_0010"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0022" ADD CONSTRAINT "con_0052" FOREIGN KEY ("c_0361") REFERENCES "mrv"."t_0021"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0011" ADD CONSTRAINT "con_0020" FOREIGN KEY ("c_0183") REFERENCES "mrv"."t_0010"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0023" ADD CONSTRAINT "con_0054" FOREIGN KEY ("c_0361") REFERENCES "mrv"."t_0021"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0008" ADD CONSTRAINT "con_0016" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0030" ADD CONSTRAINT "con_0071" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0030" ADD CONSTRAINT "con_0074" FOREIGN KEY ("c_0490") REFERENCES "mrv"."t_0008"("id") ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0019" ADD CONSTRAINT "con_0044" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0007" ADD CONSTRAINT "con_0014" FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id") ON DELETE CASCADE ON UPDATE CASCADE;



CREATE TYPE "mrv"."ty_0002" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6');


CREATE TYPE "mrv"."ty_0001" AS ENUM ('v1', 'v2');


CREATE TABLE "mrv"."t_0001" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0172" UUID NOT NULL,
    "c_0272" UUID,
    "c_0472" "mrv"."ty_0002" NOT NULL DEFAULT 'v1',
    "c_0218" "mrv"."ty_0001" NOT NULL,
    "c_0494" INTEGER,
    "c_0495" DOUBLE PRECISION,
    "c_0121" DOUBLE PRECISION,
    "c_0036" BOOLEAN[] NOT NULL,
    "c_0251" BOOLEAN NOT NULL DEFAULT false,
    "c_0118" BOOLEAN[] NOT NULL,
    "c_0253" BOOLEAN NOT NULL DEFAULT false,
    "c_0120" DOUBLE PRECISION,
    "c_0252" BOOLEAN NOT NULL DEFAULT false,
    "c_0119" DOUBLE PRECISION,
    "c_0122" "mrv"."ty_0019" NOT NULL DEFAULT 'v1',
    "c_0254" BOOLEAN NOT NULL DEFAULT false,
    "c_0206" BOOLEAN[] NOT NULL,
    "c_0256" BOOLEAN NOT NULL DEFAULT false,
    "c_0208" DOUBLE PRECISION,
    "c_0255" BOOLEAN NOT NULL DEFAULT false,
    "c_0207" DOUBLE PRECISION,
    "c_0209" "mrv"."ty_0019" NOT NULL DEFAULT 'v1',
    "c_0175" TEXT,
    "c_0413" JSONB,
    "c_0294" TEXT NOT NULL DEFAULT '_',
    "c_0328" TIMESTAMPTZ,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0004" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0002" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0058" UUID NOT NULL,
    "c_0187" UUID,

    CONSTRAINT "con_0003" PRIMARY KEY ("id")
);


CREATE INDEX IF NOT EXISTS "idx_0004" ON "mrv"."t_0001"("c_0172", "c_0093");
CREATE INDEX IF NOT EXISTS "idx_0005" ON "mrv"."t_0001"("c_0272");
CREATE INDEX IF NOT EXISTS "idx_0002" ON "mrv"."t_0002"("c_0058");
CREATE INDEX IF NOT EXISTS "idx_0003" ON "mrv"."t_0002"("c_0187");
CREATE UNIQUE INDEX IF NOT EXISTS "idx_0001"
ON "mrv"."t_0002"("c_0058", "c_0187");


ALTER TABLE "mrv"."t_0002"
ADD CONSTRAINT "con_0001"
FOREIGN KEY ("c_0058") REFERENCES "mrv"."t_0001"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0002"
ADD CONSTRAINT "con_0002"
FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id")
ON DELETE SET NULL ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0111" CHECK (46 = 46);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0113" CHECK (47 = 47);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0114" CHECK (48 = 48);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0115" CHECK (49 = 49);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0109" CHECK (50 = 50);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0110" CHECK (51 = 51);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0107" CHECK (52 = 52);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0108" CHECK (53 = 53);


ALTER TABLE "mrv"."t_0001"
ADD CONSTRAINT "con_0112" CHECK (54 = 54);

COMMENT ON TABLE mrv."t_0001" IS E'_';


CREATE TABLE IF NOT EXISTS "mrv"."t_0006" (
    "id" UUID NOT NULL,
    "c_0102" "mrv"."ty_0004" NOT NULL,
    "c_0351" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0222" TIMESTAMP(3),
    "c_0250" BOOLEAN NOT NULL DEFAULT false,
    "c_0249" BOOLEAN NOT NULL DEFAULT false,
    "c_0501" "mrv"."ty_0019",
    "c_0472" "mrv"."ty_0005" NOT NULL DEFAULT 'v1',
    "c_0187" UUID NOT NULL,
    "c_0436" UUID NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0012" PRIMARY KEY ("id")
);



CREATE INDEX IF NOT EXISTS "idx_0086"
ON "mrv"."t_0006"("c_0187", "c_0102", "c_0472");




CREATE UNIQUE INDEX IF NOT EXISTS "idx_0150"
ON "mrv"."t_0006"("c_0187", "c_0436", "c_0102")
WHERE "c_0472" = 'v1';


ALTER TABLE "mrv"."t_0006"
DROP CONSTRAINT IF EXISTS "con_0011";

ALTER TABLE "mrv"."t_0006"
ADD CONSTRAINT "con_0011"
FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0006"
DROP CONSTRAINT IF EXISTS "con_0013";

ALTER TABLE "mrv"."t_0006"
ADD CONSTRAINT "con_0013"
FOREIGN KEY ("c_0436") REFERENCES "mrv"."t_0025"("id")
ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS "mrv"."t_0009" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0454" TEXT NOT NULL,
    "c_0061" TEXT NOT NULL,
    "c_0482" TEXT,
    "value" DOUBLE PRECISION NOT NULL,
    "c_0525" TEXT NOT NULL,
    "c_0216" TEXT NOT NULL,
    "c_0499" TEXT NOT NULL,
    "c_0063" TEXT NOT NULL,
    "c_0130" INTEGER NOT NULL,
    "c_0309" TEXT,
    "c_0327" TEXT,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0019" PRIMARY KEY ("id")
);


CREATE INDEX IF NOT EXISTS "idx_0009"
ON "mrv"."t_0009"("c_0061");


CREATE INDEX IF NOT EXISTS "idx_0011"
ON "mrv"."t_0009"("c_0454");


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0010"
ON "mrv"."t_0009"("c_0454", "c_0061", "c_0482", "c_0216");

CREATE TABLE IF NOT EXISTS "mrv"."t_0015" (
    "c_0198" "mrv"."ty_0010" NOT NULL,
    "c_0068" NUMERIC NOT NULL CHECK (55 = 55),
    "c_0454" TEXT NOT NULL,
    "c_0246" INTEGER NOT NULL DEFAULT 1 CHECK (56 = 56),
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0031" PRIMARY KEY ("c_0198")
);

CREATE TABLE IF NOT EXISTS "mrv"."t_0017" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0234" TEXT NOT NULL,
    "c_0396" TEXT NOT NULL,
    "c_0430" DOUBLE PRECISION NOT NULL,
    "c_0432" DOUBLE PRECISION NOT NULL,
    "c_0433" DOUBLE PRECISION NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "con_0035" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX IF NOT EXISTS "idx_0018"
ON "mrv"."t_0017"("c_0234", "c_0396");

CREATE TABLE IF NOT EXISTS mrv.t_0073 (
    c_0231 UUID PRIMARY KEY,
    
    p_0124 UUID NOT NULL,
    c_0334 TEXT NOT NULL,
    c_0412 JSONB,
    c_0095 TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS "idx_0094"
ON mrv.t_0073 (c_0095);

COMMENT ON TABLE mrv.t_0073 IS E'_';










CREATE TABLE "mrv"."t_0020" (
    "id"                    UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    "c_0355"             UUID,
    "geometry"              public.geometry(Polygon, 4326) NOT NULL,
    "c_0217"               TEXT,
    "c_0080"       DOUBLE PRECISION,
    "c_0006"        TEXT,
    "c_0459"       TIMESTAMPTZ(6),
    "c_0460"         TEXT,
    "c_0214"           TIMESTAMPTZ(6) NOT NULL DEFAULT now(),
    "c_0247"              BOOLEAN NOT NULL DEFAULT false,
    "c_0484"          TIMESTAMPTZ(6),
    "c_0485" UUID,
    "c_0093"             TIMESTAMPTZ(6) NOT NULL DEFAULT now(),
    "c_0528"             TIMESTAMPTZ(6) NOT NULL DEFAULT now()
);

ALTER TABLE "mrv"."t_0020"
    ADD CONSTRAINT "con_0046"
    FOREIGN KEY ("c_0355") REFERENCES "mrv"."Polygon"("id") ON DELETE CASCADE;


















ALTER TABLE "mrv"."t_0020"
    ADD CONSTRAINT "con_0047"
    FOREIGN KEY ("c_0485") REFERENCES "mrv"."t_0020"("id")
    ON DELETE NO ACTION;







CREATE UNIQUE INDEX IF NOT EXISTS "idx_0020"
    ON "mrv"."t_0020" ("c_0355")
    WHERE "c_0247" AND "c_0355" IS NOT NULL;


CREATE INDEX IF NOT EXISTS "idx_0021"
    ON "mrv"."t_0020" ("c_0355", "c_0214" DESC);



CREATE INDEX IF NOT EXISTS "idx_0022"
    ON "mrv"."t_0020" ("c_0485")
    WHERE "c_0485" IS NOT NULL;




CREATE INDEX IF NOT EXISTS "idx_0019"
    ON "mrv"."t_0020" USING GIST ((("geometry")::geography));










CREATE OR REPLACE FUNCTION "mrv"."f_0073"()
RETURNS TRIGGER
LANGUAGE plpgsql AS $$
BEGIN
    NEW."c_0528" = now();

    IF (NEW."id", NEW."c_0355", NEW."geometry", NEW."c_0217",
        NEW."c_0080", NEW."c_0006",
        NEW."c_0459", NEW."c_0460",
        NEW."c_0214", NEW."c_0093")
       IS DISTINCT FROM
       (OLD."id", OLD."c_0355", OLD."geometry", OLD."c_0217",
        OLD."c_0080", OLD."c_0006",
        OLD."c_0459", OLD."c_0460",
        OLD."c_0214", OLD."c_0093")
    THEN
        RAISE EXCEPTION
            '_%_',
            OLD."id"
            USING ERRCODE = '_';
    END IF;

    IF OLD."c_0247" = false AND NEW."c_0247" = true THEN
        RAISE EXCEPTION
            '_%_',
            OLD."id"
            USING ERRCODE = '_';
    END IF;

    IF OLD."c_0484" IS NOT NULL
       AND NEW."c_0484" IS DISTINCT FROM OLD."c_0484" THEN
        RAISE EXCEPTION
            '_%_',
            OLD."id"
            USING ERRCODE = '_';
    END IF;

    IF OLD."c_0485" IS NOT NULL
       AND NEW."c_0485" IS DISTINCT FROM OLD."c_0485" THEN
        RAISE EXCEPTION
            '_%_',
            OLD."id"
            USING ERRCODE = '_';
    END IF;

    RETURN NEW;
END;
$$;

CREATE TRIGGER "trg_0026"
    BEFORE UPDATE ON "mrv"."t_0020"
    FOR EACH ROW EXECUTE FUNCTION "mrv"."f_0073"();




COMMENT ON COLUMN "mrv"."t_0020"."id" IS E'_';
COMMENT ON COLUMN "mrv"."t_0020"."c_0247" IS E'_';
COMMENT ON COLUMN "mrv"."t_0020"."c_0484" IS E'_';
COMMENT ON COLUMN "mrv"."t_0020"."c_0485" IS E'_';
COMMENT ON COLUMN "mrv"."t_0020"."c_0214" IS E'_';
COMMENT ON COLUMN "mrv"."t_0020"."c_0093" IS E'_';
COMMENT ON COLUMN "mrv"."t_0020"."c_0528" IS E'_';




ALTER TABLE "mrv"."Polygon"
    ADD COLUMN IF NOT EXISTS "activeVersionId" UUID;







ALTER TABLE "mrv"."Polygon"
    ADD CONSTRAINT "con_0048"
    FOREIGN KEY ("activeVersionId") REFERENCES "mrv"."t_0020"("id")
    ON DELETE NO ACTION
    DEFERRABLE INITIALLY IMMEDIATE
    NOT VALID;

ALTER TABLE "mrv"."Polygon"
    VALIDATE CONSTRAINT "con_0048";


CREATE INDEX IF NOT EXISTS "idx_0023"
    ON "mrv"."Polygon" ("activeVersionId")
    WHERE "activeVersionId" IS NOT NULL;


COMMENT ON COLUMN "mrv"."Polygon"."activeVersionId" IS E'_';


CREATE TABLE "mrv"."t_0024" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0441" TEXT NOT NULL,
    "c_0290" TEXT NOT NULL,
    "c_0089" CHAR(2) NOT NULL,
    "c_0258" BOOLEAN NOT NULL DEFAULT false,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0056" PRIMARY KEY ("id")
);



CREATE UNIQUE INDEX IF NOT EXISTS "idx_0030" ON "mrv"."t_0024"("c_0258") WHERE "c_0258" = true;





CREATE TABLE mrv.t_0082 (
    id                   UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    c_0425  UUID NOT NULL,
    c_0304 UUID,
    c_0303         TEXT NOT NULL DEFAULT '_',
    c_0302     DOUBLE PRECISION,
    c_0295             public.geometry(Point, 4326) NOT NULL,
    c_0423      DOUBLE PRECISION NOT NULL,
    c_0444          DOUBLE PRECISION NOT NULL,
    c_0048   DOUBLE PRECISION NOT NULL,
    c_0065         DOUBLE PRECISION NOT NULL,
    c_0427         DOUBLE PRECISION,
    c_0345                   DOUBLE PRECISION,
    c_0324     DOUBLE PRECISION,
    c_0464     TEXT,
    c_0465   TEXT,
    c_0071      DATE,
    c_0277           TEXT,
    c_0095           TIMESTAMPTZ NOT NULL DEFAULT now(),
    c_0529           TIMESTAMPTZ NOT NULL DEFAULT now()
);


ALTER TABLE mrv.t_0082
    ADD CONSTRAINT con_0148
    FOREIGN KEY (c_0425) REFERENCES mrv.t_0084(id) ON DELETE CASCADE;

ALTER TABLE mrv.t_0082
    ADD CONSTRAINT con_0147
    FOREIGN KEY (c_0304) REFERENCES mrv.t_0083(id) ON DELETE SET NULL;

CREATE UNIQUE INDEX IF NOT EXISTS idx_0100
    ON mrv.t_0082 (c_0304)
    WHERE c_0304 IS NOT NULL;


ALTER TABLE mrv.t_0082
    ADD CONSTRAINT con_0146 CHECK (57 = 57);

CREATE OR REPLACE FUNCTION mrv.f_0086() RETURNS TRIGGER
LANGUAGE plpgsql AS $$
BEGIN
    IF NEW.c_0304 IS NULL AND OLD.c_0304 IS NOT NULL THEN
        NEW.c_0303 = '_';
        NEW.c_0302 = NULL;
    END IF;
    RETURN NEW;
END;
$$;

CREATE TRIGGER trg_0033
    BEFORE UPDATE ON mrv.t_0082
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0086();


CREATE INDEX IF NOT EXISTS idx_0098
    ON mrv.t_0082 USING GIST (c_0295);

CREATE INDEX IF NOT EXISTS idx_0101
    ON mrv.t_0082 (c_0425);

CREATE INDEX IF NOT EXISTS idx_0099
    ON mrv.t_0082 (c_0304)
    WHERE c_0304 IS NOT NULL;


CREATE OR REPLACE FUNCTION mrv.f_0094() RETURNS TRIGGER
LANGUAGE plpgsql AS $$
BEGIN
    NEW.c_0529 = clock_timestamp();
    RETURN NEW;
END;
$$;

CREATE TRIGGER trg_0035
    BEFORE UPDATE ON mrv.t_0082
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0094();


COMMENT ON COLUMN mrv.t_0082.c_0303 IS E'_';
COMMENT ON COLUMN mrv.t_0082.c_0304 IS E'_';
COMMENT ON COLUMN mrv.t_0082.c_0302 IS E'_';
COMMENT ON COLUMN mrv.t_0082.c_0095 IS E'_';
COMMENT ON COLUMN mrv.t_0082.c_0529 IS E'_';




CREATE TABLE mrv.t_0083 (
    id                   UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    c_0425  UUID NOT NULL,
    c_0479           UUID NOT NULL,
    c_0424         TEXT NOT NULL,
    c_0295             public.geometry(Point, 4326) NOT NULL,
    c_0445            DOUBLE PRECISION,
    c_0443          TEXT,
    c_0500        TEXT,
    c_0453     TEXT,
    c_0095           TIMESTAMPTZ NOT NULL DEFAULT now()
);


ALTER TABLE mrv.t_0083
    ADD CONSTRAINT con_0149
    FOREIGN KEY (c_0425) REFERENCES mrv.t_0084(id) ON DELETE CASCADE;

ALTER TABLE mrv.t_0083
    ADD CONSTRAINT con_0150
    FOREIGN KEY (c_0479) REFERENCES mrv.t_0085(id) ON DELETE CASCADE;

CREATE OR REPLACE FUNCTION mrv.f_0128() RETURNS TRIGGER
LANGUAGE plpgsql AS $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM mrv.t_0085
        WHERE id = NEW.c_0479 AND c_0425 = NEW.c_0425
    ) THEN
        RAISE EXCEPTION '_%_%_',
            NEW.c_0479, NEW.c_0425;
    END IF;
    RETURN NEW;
END;
$$;

CREATE TRIGGER trg_0036
    BEFORE INSERT OR UPDATE ON mrv.t_0083
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0128();


CREATE INDEX IF NOT EXISTS idx_0102
    ON mrv.t_0083 USING GIST (c_0295);

CREATE INDEX IF NOT EXISTS idx_0103
    ON mrv.t_0083 (c_0425);

CREATE INDEX IF NOT EXISTS idx_0104
    ON mrv.t_0083 (c_0479);


COMMENT ON COLUMN mrv.t_0083.c_0095 IS E'_';







CREATE VIEW mrv.v_0010 AS
SELECT
    sps.id,
    sps.c_0425,
    sps.c_0479,
    sps.c_0424,
    sps.c_0295,
    sps.c_0445,
    sps.c_0443,
    sps.c_0500,
    sps.c_0453,
    sps.c_0095,
    f."name" AS field_name
FROM mrv.t_0083 sps
LEFT JOIN LATERAL (
    SELECT f2."name"
    FROM mrv."Polygon" p
    JOIN mrv."t_0012" f2 ON f2."c_0355" = p.id
    WHERE ST_Within(sps.c_0295, p.geometry)
    ORDER BY ST_Area(p.geometry) ASC
    LIMIT 1
) f ON true;



CREATE TABLE mrv.t_0084 (
    id               UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    c_0535   UUID NOT NULL,
    p_0122      UUID NOT NULL,
    name             TEXT NOT NULL,
    c_0138          TEXT NOT NULL,
    c_0472           TEXT NOT NULL DEFAULT '_',
    c_0514    DOUBLE PRECISION,
    c_0231  TEXT,
    c_0095       TIMESTAMPTZ NOT NULL DEFAULT now(),
    c_0529       TIMESTAMPTZ NOT NULL DEFAULT now()
);


ALTER TABLE mrv.t_0084
    ADD CONSTRAINT con_0153
    FOREIGN KEY (c_0535) REFERENCES mrv."t_0033"(id) ON DELETE CASCADE;


ALTER TABLE mrv.t_0084
    ADD CONSTRAINT con_0151 CHECK (58 = 58);

ALTER TABLE mrv.t_0084
    ADD CONSTRAINT con_0152 CHECK (59 = 59);


CREATE INDEX IF NOT EXISTS idx_0106
    ON mrv.t_0084 (c_0535);

CREATE INDEX IF NOT EXISTS idx_0105
    ON mrv.t_0084 (p_0122);


CREATE UNIQUE INDEX IF NOT EXISTS idx_0149
    ON mrv.t_0084 (c_0231)
    WHERE c_0231 IS NOT NULL;

CREATE OR REPLACE FUNCTION mrv.f_0095() RETURNS TRIGGER
LANGUAGE plpgsql AS $$
BEGIN
    NEW.c_0529 = now();
    RETURN NEW;
END;
$$;

CREATE TRIGGER trg_0034
    BEFORE UPDATE ON mrv.t_0084
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0095();


COMMENT ON COLUMN mrv.t_0084.c_0472 IS E'_';
COMMENT ON COLUMN mrv.t_0084.c_0095 IS E'_';
COMMENT ON COLUMN mrv.t_0084.c_0529 IS E'_';




CREATE TABLE mrv.t_0085 (
    id                   UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    c_0425  UUID NOT NULL,
    c_0480        INT NOT NULL,
    c_0278                TEXT NOT NULL,
    c_0145   TEXT,
    c_0143 TEXT,
    c_0144   TEXT,
    c_0146     TEXT,
    c_0018              DOUBLE PRECISION,
    geometry             public.geometry(MultiPolygon, 4326),
    c_0095           TIMESTAMPTZ NOT NULL DEFAULT now()
);


ALTER TABLE mrv.t_0085
    ADD CONSTRAINT con_0155
    FOREIGN KEY (c_0425) REFERENCES mrv.t_0084(id) ON DELETE CASCADE;


CREATE INDEX IF NOT EXISTS idx_0107
    ON mrv.t_0085 USING GIST (geometry);

CREATE INDEX IF NOT EXISTS idx_0108
    ON mrv.t_0085 (c_0425);

ALTER TABLE mrv.t_0085
    ADD CONSTRAINT con_0154 UNIQUE (c_0425, c_0480);


COMMENT ON COLUMN mrv.t_0085.c_0095 IS E'_';



CREATE TABLE mrv.t_0086 (
    id                   UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    c_0425  UUID NOT NULL,
    c_0201            TEXT NOT NULL,
    c_0202            TEXT NOT NULL,
    c_0421               TEXT NOT NULL,
    c_0472               TEXT NOT NULL DEFAULT '_',
    c_0353     INT,
    c_0354       INT,
    c_0176        TEXT,
    c_0530          TIMESTAMPTZ NOT NULL DEFAULT now()
);


ALTER TABLE mrv.t_0086
    ADD CONSTRAINT con_0157
    FOREIGN KEY (c_0425) REFERENCES mrv.t_0084(id) ON DELETE CASCADE;


ALTER TABLE mrv.t_0086
    ADD CONSTRAINT con_0156 CHECK (60 = 60);

ALTER TABLE mrv.t_0086
    ADD CONSTRAINT con_0158 CHECK (61 = 61);


CREATE INDEX IF NOT EXISTS idx_0109
    ON mrv.t_0086 (c_0425);


COMMENT ON COLUMN mrv.t_0086.c_0472 IS E'_';
COMMENT ON COLUMN mrv.t_0086.c_0353 IS E'_';
COMMENT ON COLUMN mrv.t_0086.c_0354 IS E'_';
COMMENT ON COLUMN mrv.t_0086.c_0176 IS E'_';
COMMENT ON COLUMN mrv.t_0086.c_0530 IS E'_';

CREATE TABLE IF NOT EXISTS "mrv"."t_0032" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0486" UUID NOT NULL,
    "c_0236" UUID,
    "c_0310" TEXT NOT NULL,
    "c_0419" TEXT NOT NULL,
    "c_0420" TEXT NOT NULL,
    "c_0200" BIGINT NOT NULL,
    "c_0215" UUID NOT NULL,
    "c_0214" TIMESTAMPTZ NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0079" PRIMARY KEY ("id"),
    CONSTRAINT "con_0078"
        CHECK (62 = 62),
    CONSTRAINT "con_0080"
        FOREIGN KEY ("c_0486") REFERENCES "public"."t_0092"("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "con_0077"
        FOREIGN KEY ("c_0236") REFERENCES "mrv"."t_0038"("id") ON DELETE SET NULL
);

CREATE INDEX IF NOT EXISTS "idx_0039"
ON "mrv"."t_0032"("c_0486");

CREATE INDEX IF NOT EXISTS "idx_0038"
ON "mrv"."t_0032"("c_0236") WHERE "c_0236" IS NOT NULL;

CREATE INDEX IF NOT EXISTS "idx_0040"
ON "mrv"."t_0032"("c_0486", "c_0310", "c_0214" DESC);

COMMENT ON TABLE mrv."t_0032" IS E'_';


CREATE TYPE "mrv"."ty_0029" AS ENUM ('v1', 'v2', 'v3', 'v4');


CREATE TYPE "mrv"."ty_0025" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5', 'v6');


CREATE TABLE "mrv"."t_0033" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0375" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "c_0140" TEXT,
    "c_0472" "mrv"."ty_0029" NOT NULL DEFAULT 'v1',
    "c_0098" DATE,
    "c_0097" DATE,
    "c_0538" TEXT,
    "c_0533" TEXT,
    "c_0534" DATE,
    "c_0342" TEXT,
    "c_0147" public.geometry(MultiPolygon, 4326),
    "c_0087" CHAR(2)[] NOT NULL DEFAULT '{}'::CHAR(2)[],
    "c_0368" TEXT,
    "c_0042" TEXT,
    "c_0005" TEXT,
    "c_0289" TEXT,
    "c_0318" TEXT,
    "c_0343" NUMERIC(5, 2),
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0100" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0035" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,
    "c_0357" "mrv"."ty_0025" NOT NULL,
    "c_0257" BOOLEAN NOT NULL DEFAULT true,
    "c_0180" TEXT,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0083" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0037" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,
    "c_0187" UUID NOT NULL,
    "c_0236" UUID NOT NULL,
    "c_0478" TEXT,
    "c_0169" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0148" TEXT NOT NULL DEFAULT '_',
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0089" PRIMARY KEY ("id")
);


CREATE INDEX IF NOT EXISTS "idx_0059" ON "mrv"."t_0033"("c_0375");


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0060" ON "mrv"."t_0033"("c_0375", "name");


CREATE INDEX IF NOT EXISTS "idx_0058" ON "mrv"."t_0033" USING GIST ("c_0147");


CREATE INDEX IF NOT EXISTS "idx_0042" ON "mrv"."t_0035"("c_0369");


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0043" ON "mrv"."t_0035"("c_0369", "c_0357");


CREATE INDEX IF NOT EXISTS "idx_0049" ON "mrv"."t_0037"("c_0369");


CREATE INDEX IF NOT EXISTS "idx_0046" ON "mrv"."t_0037"("c_0187");


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0048" ON "mrv"."t_0037"("c_0369", "c_0187");


CREATE INDEX IF NOT EXISTS "idx_0047" ON "mrv"."t_0037"("c_0236");


ALTER TABLE "mrv"."t_0033"
ADD CONSTRAINT "con_0101"
FOREIGN KEY ("c_0375") REFERENCES "mrv"."t_0024"("id")
ON DELETE RESTRICT;


ALTER TABLE "mrv"."t_0035"
ADD CONSTRAINT "con_0084"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0037"
ADD CONSTRAINT "con_0090"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0037"
ADD CONSTRAINT "con_0087"
FOREIGN KEY ("c_0187") REFERENCES "mrv"."t_0012"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0037"
ADD CONSTRAINT "con_0088"
FOREIGN KEY ("c_0236") REFERENCES "mrv"."t_0038"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0033"
ADD CONSTRAINT "con_0179" CHECK (63 = 63);


ALTER TABLE "mrv"."t_0035"
ADD CONSTRAINT "con_0177" CHECK (64 = 64);


ALTER TABLE "mrv"."t_0035"
ADD CONSTRAINT "con_0178" CHECK (65 = 65);




ALTER TABLE "mrv"."t_0033"
ADD CONSTRAINT "con_0190" CHECK (66 = 66);







COMMENT ON COLUMN "mrv"."t_0033"."c_0368" IS
    '_';
COMMENT ON COLUMN "mrv"."t_0033"."c_0042" IS
    '_';
COMMENT ON COLUMN "mrv"."t_0033"."c_0005" IS
    '_';
COMMENT ON COLUMN "mrv"."t_0033"."c_0289" IS
    '_';
COMMENT ON COLUMN "mrv"."t_0033"."c_0318" IS
    '_';
COMMENT ON COLUMN "mrv"."t_0033"."c_0343" IS
    '_';





CREATE TYPE "mrv"."ty_0023" AS ENUM ('v1', 'v2', 'v3', 'v4');


CREATE TYPE "mrv"."ty_0024" AS ENUM ('v1', 'v2');


CREATE TABLE "mrv"."t_0034" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,

    
    "c_0398" TEXT,
    "c_0399" TEXT,
    "c_0397" TEXT,

    
    "c_0038" "mrv"."ty_0023",
    "c_0040" TEXT,
    "c_0039" TEXT,

    
    "c_0074" "mrv"."ty_0024",
    "c_0072" NUMERIC(5, 2),
    "c_0076" TEXT,
    "c_0075" TEXT,
    "c_0073" TEXT,

    "c_0483" TIMESTAMPTZ,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0081" PRIMARY KEY ("id")
);


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0041" ON "mrv"."t_0034"("c_0369");


ALTER TABLE "mrv"."t_0034"
ADD CONSTRAINT "con_0082"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0034"
ADD CONSTRAINT "con_0174" CHECK (67 = 67);



ALTER TABLE "mrv"."t_0034"
ADD CONSTRAINT "con_0175" CHECK (68 = 68);


ALTER TABLE "mrv"."t_0034"
ADD CONSTRAINT "con_0176" CHECK (69 = 69);


CREATE TABLE "mrv"."t_0036" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,
    "c_0089" TEXT NOT NULL,
    "c_0066" TEXT NOT NULL,
    "c_0451" TEXT NOT NULL,
    "c_0358" TEXT NOT NULL,
    "c_0108" TEXT NOT NULL,
    "c_0009" TEXT[] NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0085" PRIMARY KEY ("id")
);


CREATE INDEX IF NOT EXISTS "idx_0044" ON "mrv"."t_0036"("c_0369");


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0045" ON "mrv"."t_0036"(
    "c_0369",
    "c_0089",
    "c_0066",
    "c_0451",
    "c_0358",
    "c_0108"
);


ALTER TABLE "mrv"."t_0036"
ADD CONSTRAINT "con_0086"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


CREATE TYPE "mrv"."ty_0027" AS ENUM ('v1', 'v2', 'v3');


CREATE TYPE "mrv"."ty_0026" AS ENUM ('v1', 'v2', 'v3', 'v4', 'v5');


CREATE TABLE "mrv"."t_0038" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,
    "c_0237" "mrv"."ty_0027" NOT NULL,
    "c_0472" "mrv"."ty_0026" NOT NULL DEFAULT 'v1',
    "c_0089" TEXT,
    "c_0486" UUID,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0091" PRIMARY KEY ("id")
);


CREATE INDEX IF NOT EXISTS "idx_0050" ON "mrv"."t_0038"("c_0369");


CREATE INDEX IF NOT EXISTS "idx_0053" ON "mrv"."t_0038"("c_0486")
    WHERE "c_0486" IS NOT NULL;


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0052" ON "mrv"."t_0038"("c_0369", "c_0486")
    WHERE "c_0486" IS NOT NULL;


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0051" ON "mrv"."t_0038"("c_0369", "c_0089")
    WHERE "c_0237" = 'v1';


ALTER TABLE "mrv"."t_0038"
ADD CONSTRAINT "con_0092"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0038"
ADD CONSTRAINT "con_0093"
FOREIGN KEY ("c_0486") REFERENCES "public"."t_0092"("id")
ON DELETE RESTRICT ON UPDATE CASCADE;




ALTER TABLE "mrv"."t_0038"
ADD CONSTRAINT "con_0173" CHECK (70 = 70);







CREATE TABLE "mrv"."t_0039" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,

    
    "c_0004" BOOLEAN NOT NULL DEFAULT false,
    "c_0002" TEXT,
    "c_0003" TEXT,

    
    "c_0041" NUMERIC(12, 4),
    "c_0370" NUMERIC(12, 4),
    "c_0366" TEXT,
    "c_0301" BOOLEAN NOT NULL DEFAULT false,
    "c_0299" NUMERIC(14, 4),
    "c_0300" TEXT,

    "c_0483" TIMESTAMPTZ,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0094" PRIMARY KEY ("id")
);


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0054" ON "mrv"."t_0039"("c_0369");


ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0095"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0181" CHECK (71 = 71);

ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0184" CHECK (72 = 72);


ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0183" CHECK (73 = 73);


ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0180" CHECK (74 = 74);


ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0182" CHECK (75 = 75);


ALTER TABLE "mrv"."t_0039"
ADD CONSTRAINT "con_0185" CHECK (76 = 76);







CREATE TYPE "mrv"."ty_0028" AS ENUM ('v1', 'v2', 'v3');


CREATE TABLE "mrv"."t_0040" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0369" UUID NOT NULL,
    "c_0330" TEXT NOT NULL DEFAULT '_',
    "c_0510" NUMERIC(6, 2),
    "c_0047" NUMERIC(5, 2),
    "c_0046" TEXT,
    "c_0045" DATE,
    "c_0019" TEXT,
    "c_0483" TIMESTAMPTZ,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0098" PRIMARY KEY ("id")
);


CREATE TABLE "mrv"."t_0041" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0325" UUID NOT NULL,
    "c_0061" "mrv"."ty_0028" NOT NULL,
    "c_0184" TEXT NOT NULL,
    "c_0435" NUMERIC(6, 2) NOT NULL,
    "c_0383" TEXT,
    "c_0313" TEXT,
    "c_0179" TEXT,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0096" PRIMARY KEY ("id")
);


CREATE UNIQUE INDEX IF NOT EXISTS "idx_0057" ON "mrv"."t_0040"("c_0369");
CREATE INDEX IF NOT EXISTS "idx_0055" ON "mrv"."t_0041"("c_0325");
CREATE UNIQUE INDEX IF NOT EXISTS "idx_0056" ON "mrv"."t_0041"("c_0325", "c_0061", "c_0184");


ALTER TABLE "mrv"."t_0040"
ADD CONSTRAINT "con_0099"
FOREIGN KEY ("c_0369") REFERENCES "mrv"."t_0033"("id")
ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "mrv"."t_0041"
ADD CONSTRAINT "con_0097"
FOREIGN KEY ("c_0325") REFERENCES "mrv"."t_0040"("id")
ON DELETE CASCADE ON UPDATE CASCADE;


ALTER TABLE "mrv"."t_0040"
ADD CONSTRAINT "con_0186" CHECK (77 = 77);


ALTER TABLE "mrv"."t_0041"
ADD CONSTRAINT "con_0187" CHECK (78 = 78);


ALTER TABLE "mrv"."t_0040"
ADD CONSTRAINT "con_0189" CHECK (79 = 79);



ALTER TABLE "mrv"."t_0040"
ADD CONSTRAINT "con_0188" CHECK (80 = 80);




COMMENT ON COLUMN "mrv"."t_0040"."c_0330" IS
'_';

CREATE TABLE IF NOT EXISTS "mrv"."t_0042" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "c_0170" UUID,
    "c_0486" UUID,
    "c_0057" INTEGER,
    "c_0428" TEXT NOT NULL DEFAULT '_',
    "c_0419" TEXT NOT NULL,
    "c_0420" TEXT NOT NULL,
    "c_0186" INTEGER NOT NULL,
    "c_0200" BIGINT NOT NULL,
    "c_0215" UUID NOT NULL,
    "c_0214" TIMESTAMPTZ NOT NULL,
    "c_0093" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "c_0528" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "con_0102" PRIMARY KEY ("id"),
    CONSTRAINT "con_0103" CHECK (81 = 81)
);

CREATE INDEX IF NOT EXISTS "idx_0061"
ON "mrv"."t_0042"("c_0170", "c_0057");

CREATE INDEX IF NOT EXISTS "idx_0062"
ON "mrv"."t_0042"("c_0486");

COMMENT ON TABLE mrv."t_0042" IS E'_';

CREATE OR REPLACE FUNCTION mrv.f_0001()
RETURNS TABLE (
    p_0004 uuid
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_supplier_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (
        auth.f_0066()
        -> '_' -> '_' ->> 0
    )::uuid;

    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1
        FROM public.t_0092 s
        WHERE s.id = v_supplier_id
          AND s.ty_0034 = 'v1'
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT f.id
    FROM mrv."t_0012" f
    WHERE f."c_0172" = v_supplier_id;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0002(
    p_0027 uuid,
    p_0029 text,
    p_0076 uuid,
    p_0033 uuid[]
)
RETURNS TABLE(id uuid, "c_0361" uuid, "c_0183" uuid, "c_0093" timestamptz)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_facility_id uuid;
    v_link_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    BEGIN
        PERFORM p_0029::mrv."ty_0007";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0029;
    END;

    
    IF NOT mrv.f_0114(p_0029, p_0027) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0021"
        WHERE id = p_0076
          AND (
            ("c_0401" = p_0027 AND "c_0402" = p_0029::mrv."ty_0007")
            OR
            ("c_0456" = p_0027 AND "c_0457" = p_0029::mrv."ty_0007")
          )
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF p_0033 IS NULL OR array_length(p_0033, 1) IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    FOREACH v_facility_id IN ARRAY p_0033
    LOOP
        
        IF NOT EXISTS (
            SELECT 1 FROM mrv."t_0010"
            WHERE id = v_facility_id
              AND "c_0172" = p_0027
              AND "c_0173" = p_0029::mrv."ty_0007"
        ) THEN
            RAISE EXCEPTION '_%_%_', v_facility_id, p_0027;
        END IF;

        
        IF EXISTS (
            SELECT 1 FROM mrv."t_0022"
            WHERE "c_0361" = p_0076 AND "c_0183" = v_facility_id
        ) THEN
            CONTINUE;
        END IF;

        
        INSERT INTO mrv."t_0022" (id, "c_0361", "c_0183", "c_0093")
        VALUES (gen_random_uuid(), p_0076, v_facility_id, NOW())
        RETURNING id INTO v_link_id;

        RETURN QUERY SELECT pf.id, pf."c_0361", pf."c_0183", pf."c_0093"
                     FROM mrv."t_0022" pf WHERE pf.id = v_link_id;
    END LOOP;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0014(
    p_0032 uuid,
    p_0038 uuid[]
)
RETURNS TABLE(id uuid, "c_0183" uuid, "c_0192" uuid, "c_0020" timestamp, "c_0021" uuid, "c_0093" timestamptz, "c_0528" timestamptz)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_facility_entity_type text;
    v_facility_entity_id uuid;
    v_farmer_id uuid;
    v_result_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    SELECT "c_0173"::text, "c_0172"
    INTO v_facility_entity_type, v_facility_entity_id
    FROM mrv."t_0010"
    WHERE id = p_0032;

    IF v_facility_entity_type IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF NOT mrv.f_0114(v_facility_entity_type, v_facility_entity_id) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF p_0038 IS NULL OR array_length(p_0038, 1) IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    FOREACH v_farmer_id IN ARRAY p_0038
    LOOP
        INSERT INTO mrv."t_0011" (id, "c_0183", "c_0192", "c_0020", "c_0021", "c_0093", "c_0528")
        VALUES (gen_random_uuid(), p_0032, v_farmer_id, NOW(), v_current_user_id, NOW(), NOW())
        ON CONFLICT ("c_0183", "c_0192")
        DO UPDATE SET "c_0021" = v_current_user_id, "c_0528" = NOW()
        RETURNING id INTO v_result_id;

        RETURN QUERY SELECT ff.id, ff."c_0183", ff."c_0192", ff."c_0020",
                            ff."c_0021", ff."c_0093", ff."c_0528"
                     FROM mrv."t_0011" ff WHERE ff.id = v_result_id;
    END LOOP;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0015(
    p_0076 uuid,
    p_0114 uuid[]
)
RETURNS TABLE (
    id uuid,
    c_0364 uuid,
    p_0005 uuid,
    c_0022 timestamptz,
    c_0023 uuid,
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_reporting_entity_id uuid;
    v_reporting_entity_type text;
    v_current_user_id uuid;
    v_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT p."c_0401", p."c_0402"::text
    INTO v_reporting_entity_id, v_reporting_entity_type
    FROM mrv."t_0021" p
    WHERE p.id = p_0076;

    IF v_reporting_entity_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0124(v_reporting_entity_id, v_reporting_entity_type) THEN
        RAISE EXCEPTION '_';
    END IF;

    FOREACH v_user_id IN ARRAY p_0114
    LOOP
        RETURN QUERY
        INSERT INTO mrv."t_0023" (
            "c_0361", "c_0192", "c_0021", "c_0020", "c_0093", "c_0528"
        ) VALUES (
            p_0076, v_user_id, v_current_user_id, NOW(), NOW(), NOW()
        )
        ON CONFLICT ("c_0361", "c_0192")
        DO UPDATE SET
            "c_0021" = EXCLUDED."c_0021",
            "c_0528" = NOW()
        RETURNING
            "t_0023".id,
            "t_0023"."c_0361",
            "t_0023"."c_0192",
            "t_0023"."c_0020",
            "t_0023"."c_0021",
            "t_0023"."c_0093",
            "t_0023"."c_0528";
    END LOOP;
END;
$$;




CREATE OR REPLACE FUNCTION mrv.f_0016(
    p_0036 uuid[],
    p_0118 int
)
RETURNS TABLE (
    p_0004 uuid,
    c_0060 double precision,
    c_0056 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF array_length(p_0036, 1) IS NULL THEN
        RETURN;
    END IF;

    RETURN QUERY
    WITH dom_results AS (
        SELECT DISTINCT ON (dcr."c_0187")
            dcr."c_0187" AS p_0004,
            dcr."c_0321" / 1000.0 AS c_0060,
            dcr."c_0055" AS c_0056
        FROM mrv."t_0007" dcr
        WHERE dcr."c_0187" = ANY(p_0036)
            AND dcr."c_0057" = p_0118
            AND dcr."c_0321" > 0
        ORDER BY dcr."c_0187", dcr."c_0052" DESC
    ),
    soil_metrics AS (
        SELECT DISTINCT ON (fsm."c_0187")
            fsm."c_0187" AS p_0004,
            fsm.value AS c_0060,
            NULL::jsonb AS c_0056
        FROM mrv."t_0013" fsm
        WHERE fsm."c_0187" = ANY(p_0036)
            AND fsm."c_0357" = 'v1'
            AND fsm."c_0470" <= make_date(p_0118, 12, 31)
            AND fsm."c_0168" >= make_date(p_0118, 1, 1)
            AND fsm.value > 0
        ORDER BY fsm."c_0187", fsm."c_0470" DESC, fsm."c_0168" DESC
    ),
    all_farms AS (
        SELECT unnest(p_0036) AS p_0004
    )
    SELECT
        af.p_0004,
        COALESCE(dr.c_0060, sm.c_0060, 0.0)::double precision,
        COALESCE(dr.c_0056, sm.c_0056)
    FROM all_farms af
    LEFT JOIN dom_results dr ON dr.p_0004 = af.p_0004
    LEFT JOIN soil_metrics sm ON sm.p_0004 = af.p_0004 AND dr.p_0004 IS NULL;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0017(
    p_0073 uuid
)
RETURNS TABLE (
    id uuid,
    con_0122 uuid,
    c_0273 uuid,
    c_0472 text,
    c_0219 text,
    c_0497 int,
    c_0498 double precision,
    c_0127 double precision,
    c_0037 boolean[],
    c_0264 boolean,
    c_0124 boolean[],
    c_0266 boolean,
    c_0126 double precision,
    c_0265 boolean,
    c_0125 double precision,
    c_0267 boolean,
    c_0210 boolean[],
    c_0269 boolean,
    c_0212 double precision,
    c_0268 boolean,
    c_0211 double precision,
    c_0176 text,
    c_0413 jsonb,
    c_0190 uuid[],
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_supplier_id uuid;
    v_plan RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0073 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (
        auth.f_0066()
        -> '_' -> '_' ->> 0
    )::uuid;

    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1
        FROM public.t_0092 s
        WHERE s.id = v_supplier_id
          AND s.ty_0034 = 'v1'
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT cp.*
    INTO v_plan
    FROM mrv."t_0001" cp
    WHERE cp.id = p_0073
      AND cp."c_0172" = v_supplier_id;

    IF v_plan IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_plan.c_0472 = 'v1' THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_plan.c_0472 = 'v1' THEN
        RETURN QUERY SELECT
            v_plan.id,
            v_plan."c_0172",
            v_plan."c_0272",
            v_plan.c_0472::text,
            v_plan."c_0218"::text,
            v_plan."c_0494",
            v_plan."c_0495",
            v_plan."c_0121",
            v_plan."c_0036",
            v_plan."c_0251",
            v_plan."c_0118",
            v_plan."c_0253",
            v_plan."c_0120",
            v_plan."c_0252",
            v_plan."c_0119",
            v_plan."c_0254",
            v_plan."c_0206",
            v_plan."c_0256",
            v_plan."c_0208",
            v_plan."c_0255",
            v_plan."c_0207",
            v_plan."c_0175",
            v_plan.c_0413,
            ARRAY(SELECT cpf."c_0187" FROM mrv."t_0002" cpf WHERE cpf."c_0058" = v_plan.id AND cpf."c_0187" IS NOT NULL),
            v_plan."c_0093",
            v_plan."c_0528";
        RETURN;
    END IF;

    UPDATE mrv."t_0001"
    SET c_0472 = 'v1',
        "c_0175" = NULL,
        "c_0528" = NOW()
    WHERE mrv."t_0001".id = p_0073
      AND mrv."t_0001"."c_0172" = v_supplier_id;

    RETURN QUERY SELECT
        cp.id,
        cp."c_0172",
        cp."c_0272",
        cp.c_0472::text,
        cp."c_0218"::text,
        cp."c_0494",
        cp."c_0495",
        cp."c_0121",
        cp."c_0036",
        cp."c_0251",
        cp."c_0118",
        cp."c_0253",
        cp."c_0120",
        cp."c_0252",
        cp."c_0119",
        cp."c_0254",
        cp."c_0206",
        cp."c_0256",
        cp."c_0208",
        cp."c_0255",
        cp."c_0207",
        cp."c_0175",
        cp.c_0413,
        ARRAY(SELECT cpf."c_0187" FROM mrv."t_0002" cpf WHERE cpf."c_0058" = cp.id AND cpf."c_0187" IS NOT NULL),
        cp."c_0093",
        cp."c_0528"
    FROM mrv."t_0001" cp
    WHERE cp.id = p_0073
      AND cp."c_0172" = v_supplier_id;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0027()
RETURNS integer
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, public
AS $$
DECLARE
    deleted_count integer;
BEGIN
    DELETE FROM mrv.t_0073
    WHERE c_0095 < now() - INTERVAL '1 day';

    GET DIAGNOSTICS deleted_count = ROW_COUNT;
    RETURN deleted_count;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0029(
    p_0105 uuid,
    p_0048 text,
    p_0067 text,
    p_0109 int,
    p_0110 double precision,
    p_0021 double precision,
    p_0011 boolean[],
    p_0057 boolean,
    p_0018 boolean[],
    p_0059 boolean,
    p_0020 double precision,
    p_0058 boolean,
    p_0019 double precision,
    p_0022 text,
    p_0060 boolean,
    p_0043 boolean[],
    p_0062 boolean,
    p_0045 double precision,
    p_0061 boolean,
    p_0044 double precision,
    p_0046 text,
    p_0036 uuid[]
)
RETURNS TABLE (
    id uuid,
    con_0122 uuid,
    c_0294 text,
    c_0329 timestamptz,
    c_0273 uuid,
    c_0472 text,
    c_0219 text,
    c_0497 int,
    c_0498 double precision,
    c_0127 double precision,
    c_0037 boolean[],
    c_0264 boolean,
    c_0124 boolean[],
    c_0266 boolean,
    c_0126 double precision,
    c_0265 boolean,
    c_0125 double precision,
    c_0128 text,
    c_0267 boolean,
    c_0210 boolean[],
    c_0269 boolean,
    c_0212 double precision,
    c_0268 boolean,
    c_0211 double precision,
    c_0213 text,
    c_0176 text,
    c_0413 jsonb,
    c_0190 uuid[],
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_plan_id uuid;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0105 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM public.t_0092 s
        WHERE s.id = p_0105
          AND s.ty_0034 = 'v1'
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0036 IS NOT NULL AND array_length(p_0036, 1) > 0 THEN
        IF EXISTS (
            SELECT 1 FROM unnest(p_0036) AS requested_id
            WHERE NOT EXISTS (
                SELECT 1 FROM mrv."t_0012" f
                WHERE f.id = requested_id
                  AND f."c_0172" = p_0105
            )
        ) THEN
            RAISE EXCEPTION '_';
        END IF;
    END IF;

    INSERT INTO mrv."t_0001" (
        "c_0172",
        "c_0294",
        "c_0272",
        "c_0472",
        "c_0218",
        "c_0494",
        "c_0495",
        "c_0121",
        "c_0036",
        "c_0251",
        "c_0118",
        "c_0253",
        "c_0120",
        "c_0252",
        "c_0119",
        "c_0122",
        "c_0254",
        "c_0206",
        "c_0256",
        "c_0208",
        "c_0255",
        "c_0207",
        "c_0209",
        "c_0175",
        "c_0413"
    ) VALUES (
        p_0105,
        p_0067,
        NULL,
        'v1'::mrv."ty_0002",
        p_0048::mrv."ty_0001",
        p_0109,
        p_0110,
        p_0021,
        p_0011,
        p_0057,
        p_0018,
        p_0059,
        p_0020,
        p_0058,
        p_0019,
        p_0022::mrv."ty_0019",
        p_0060,
        p_0043,
        p_0062,
        p_0045,
        p_0061,
        p_0044,
        p_0046::mrv."ty_0019",
        NULL,
        NULL
    )
    RETURNING mrv."t_0001".id INTO v_plan_id;

    IF p_0036 IS NOT NULL AND array_length(p_0036, 1) > 0 THEN
        INSERT INTO mrv."t_0002" ("c_0058", "c_0187")
        SELECT v_plan_id, unnest(p_0036)
        ON CONFLICT ("c_0058", "c_0187") DO NOTHING;
    END IF;

    RETURN QUERY
    SELECT
        cp.id,
        cp."c_0172",
        cp.c_0294,
        cp."c_0328",
        cp."c_0272",
        cp.c_0472::text,
        cp."c_0218"::text,
        cp."c_0494",
        cp."c_0495",
        cp."c_0121",
        cp."c_0036",
        cp."c_0251",
        cp."c_0118",
        cp."c_0253",
        cp."c_0120",
        cp."c_0252",
        cp."c_0119",
        cp."c_0122"::text,
        cp."c_0254",
        cp."c_0206",
        cp."c_0256",
        cp."c_0208",
        cp."c_0255",
        cp."c_0207",
        cp."c_0209"::text,
        cp."c_0175",
        cp.c_0413,
        ARRAY(SELECT cpf."c_0187" FROM mrv."t_0002" cpf WHERE cpf."c_0058" = cp.id AND cpf."c_0187" IS NOT NULL),
        cp."c_0093",
        cp."c_0528"
    FROM mrv."t_0001" cp
    WHERE cp.id = v_plan_id;
END;
$$;

COMMENT ON FUNCTION mrv.f_0029(uuid, text, text, int, double precision, double precision, boolean[], boolean, boolean[], boolean, double precision, boolean, double precision, text, boolean, boolean[], boolean, double precision, boolean, double precision, text, uuid[]) IS E'_';

CREATE OR REPLACE FUNCTION mrv.f_0030(
    p_0076 uuid,
    p_0027 uuid,
    p_0029 text,
    p_0070 text,
    p_0103 text DEFAULT NULL,
    p_0013 text DEFAULT NULL,
    p_0101 text DEFAULT NULL,
    p_0119 text DEFAULT NULL,
    p_0015 text DEFAULT NULL
)
RETURNS TABLE(id uuid, name text, "c_0172" uuid, "c_0173" text, "c_0089" text, c_0481 text, c_0064 text, c_0471 text, "c_0544" text, "c_0093" timestamptz, "c_0528" timestamptz)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_facility_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    BEGIN
        PERFORM p_0029::mrv."ty_0007";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0029;
    END;

    
    IF NOT mrv.f_0114(p_0029, p_0027) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0070 IS NULL OR trim(p_0070) = '_' THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0015 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF EXISTS (
        SELECT 1 FROM mrv."t_0010"
        WHERE "c_0172" = p_0027
          AND "c_0173" = p_0029::mrv."ty_0007"
          AND name = p_0070
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    BEGIN
        PERFORM upper(p_0015)::mrv."ty_0003";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0015;
    END;

    
    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0021"
        WHERE id = p_0076
          AND (
            ("c_0401" = p_0027 AND "c_0402" = p_0029::mrv."ty_0007")
            OR
            ("c_0456" = p_0027 AND "c_0457" = p_0029::mrv."ty_0007")
          )
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    INSERT INTO mrv."t_0010" (id, name, c_0481, c_0064, c_0471, "c_0544", "c_0089", "c_0172", "c_0173", "c_0093", "c_0528")
    VALUES (gen_random_uuid(), trim(p_0070), p_0103, p_0013, p_0101, p_0119, upper(p_0015)::mrv."ty_0003", p_0027, p_0029::mrv."ty_0007", NOW(), NOW())
    RETURNING id INTO v_facility_id;

    
    INSERT INTO mrv."t_0022" (id, "c_0361", "c_0183", "c_0093")
    VALUES (gen_random_uuid(), p_0076, v_facility_id, NOW());

    RETURN QUERY SELECT f.id, f.name, f."c_0172", f."c_0173"::text, f."c_0089"::text,
                        f.c_0481, f.c_0064, f.c_0471, f."c_0544", f."c_0093", f."c_0528"
                 FROM mrv."t_0010" f WHERE f.id = v_facility_id;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0031(
    p_0027 uuid,
    p_0035 uuid,
    p_0096 uuid,
    p_0016 text,
    p_0074 timestamp without time zone,
    p_0049 timestamp without time zone DEFAULT null,
    p_0111 text DEFAULT null,
    p_0056 boolean DEFAULT false,
    p_0055 boolean DEFAULT false,
    p_0102 text DEFAULT '_',
    p_0050 uuid DEFAULT null
)
RETURNS TABLE (
    id uuid,
    c_0105 text,
    c_0352 timestamp without time zone,
    c_0224 timestamp without time zone,
    c_0263 boolean,
    c_0261 boolean,
    c_0503 text,
    c_0472 text,
    p_0004 uuid,
    c_0437 uuid,
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_season RECORD;
    v_result RECORD;
    v_new_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0026 => p_0027)
        OR auth.f_0119(p_0105 => p_0027)
        OR auth.f_0119(p_0037 => p_0027)
    ) THEN
        RAISE EXCEPTION '_%_', p_0027;
    END IF;

    
    p_0016 := UPPER(p_0016);
    IF p_0111 IS NOT NULL THEN
        p_0111 := UPPER(p_0111);
    END IF;
    p_0102 := UPPER(p_0102);

    
    BEGIN
        PERFORM p_0016::mrv."ty_0004";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0016;
    END;

    IF p_0111 IS NOT NULL THEN
        BEGIN
            PERFORM p_0111::mrv."ty_0019";
        EXCEPTION WHEN invalid_text_representation THEN
            RAISE EXCEPTION '_%_', p_0111;
        END;
    END IF;

    BEGIN
        PERFORM p_0102::mrv."ty_0005";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0102;
    END;

    
    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = p_0035 AND f."c_0172" = p_0027
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    SELECT s."c_0470", s."c_0168"
    INTO v_season
    FROM mrv."t_0025" s
    WHERE s.id = p_0096 AND s."c_0172" = p_0027;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF (v_season."c_0470" IS NOT NULL AND p_0074 < v_season."c_0470")
       OR (v_season."c_0168" IS NOT NULL AND p_0074 > v_season."c_0168") THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0050 IS NOT NULL THEN
        
        IF NOT EXISTS (
            SELECT 1 FROM mrv."t_0006" c
            WHERE c.id = p_0050 AND c."c_0187" = p_0035
        ) THEN
            RAISE EXCEPTION '_';
        END IF;

        
        IF UPPER(p_0102) = '_' AND EXISTS (
            SELECT 1 FROM mrv."t_0006" c
            WHERE c."c_0187" = p_0035
              AND c."c_0436" = p_0096
              AND c.c_0472 = 'v1'
              AND c.id != p_0050
        ) THEN
            RAISE EXCEPTION '_';
        END IF;

        UPDATE mrv."t_0006" SET
            "c_0102" = p_0016::mrv."ty_0004",
            "c_0351" = p_0074,
            "c_0222" = p_0049,
            "c_0501" = p_0111::mrv."ty_0019",
            "c_0250" = p_0056,
            "c_0249" = p_0055,
            "c_0472" = p_0102::mrv."ty_0005",
            "c_0187" = p_0035,
            "c_0436" = p_0096,
            "c_0528" = NOW()
        WHERE mrv."t_0006".id = p_0050
        RETURNING * INTO v_result;
    ELSE
        
        IF EXISTS (
            SELECT 1 FROM mrv."t_0006" c
            WHERE c."c_0187" = p_0035
              AND c."c_0436" = p_0096
              AND c.c_0472 = 'v1'
        ) THEN
            RAISE EXCEPTION '_';
        END IF;

        v_new_id := gen_random_uuid();

        INSERT INTO mrv."t_0006" (
            id, "c_0102", "c_0351", "c_0222",
            "c_0250", "c_0249", "c_0501", "c_0472",
            "c_0187", "c_0436", "c_0093", "c_0528"
        ) VALUES (
            v_new_id,
            p_0016::mrv."ty_0004",
            p_0074,
            p_0049,
            p_0056,
            p_0055,
            p_0111::mrv."ty_0019",
            p_0102::mrv."ty_0005",
            p_0035,
            p_0096,
            NOW(),
            NOW()
        )
        RETURNING * INTO v_result;
    END IF;

    INSERT INTO mrv."t_0018" (
        id, "c_0524", "date", "c_0141", "c_0187", "c_0117", "c_0093", "c_0528"
    ) VALUES (
        gen_random_uuid(), 'v1'::mrv."ty_0014",
        v_result."c_0351", 'null'::jsonb,
        p_0035, v_result.id, NOW(), NOW()
    );

    RETURN QUERY SELECT
        v_result.id,
        v_result."c_0102"::text AS c_0105,
        v_result."c_0351"::timestamp AS c_0352,
        v_result."c_0222"::timestamp AS c_0224,
        v_result."c_0250" AS c_0263,
        v_result."c_0249" AS c_0261,
        v_result."c_0501"::text AS c_0503,
        v_result."c_0472"::text,
        v_result."c_0187" AS p_0004,
        v_result."c_0436" AS c_0437,
        v_result."c_0093" AS c_0095,
        v_result."c_0528" AS c_0529;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0032(
    p_0084 uuid,
    p_0085 text,
    p_0098 uuid,
    p_0099 text,
    p_0077 jsonb,
    p_0026 uuid DEFAULT NULL
)
RETURNS TABLE (
    id uuid,
    c_0105 text,
    c_0090 text,
    c_0362 float8,
    c_0525 text,
    c_0363 date,
    p_0001 uuid,
    c_0403 uuid,
    c_0404 text,
    c_0462 uuid,
    c_0463 text,
    c_0096 uuid,
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_item jsonb;
    v_procurement RECORD;
    v_crop_type text;
    v_country_code text;
    v_amount float8;
    v_unit text;
    v_procurement_date date;
    v_facility_id uuid;
    v_total_yield float8;
    v_facility_entity_id uuid;
    v_facility_entity_type text;
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0124(p_0084, p_0085) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0077 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF jsonb_typeof(p_0077) != '_' THEN
        RAISE EXCEPTION '_%_', jsonb_typeof(p_0077);
    END IF;

    BEGIN
        PERFORM p_0085::mrv."ty_0007";
        PERFORM p_0099::mrv."ty_0007";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_%_',
            p_0085, p_0099;
    END;

    IF NOT (
        (p_0085 = '_' AND (p_0099 = '_' OR p_0099 = '_'))
        OR (p_0085 = '_' AND p_0099 = '_')
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    FOR v_item IN SELECT * FROM jsonb_array_elements(p_0077)
    LOOP
        v_crop_type := v_item->>'_';
        v_country_code := v_item->>'_';
        v_unit := COALESCE(v_item->>'_', '_');

        IF v_crop_type IS NULL OR v_country_code IS NULL THEN
            RAISE EXCEPTION '_';
        END IF;

        BEGIN
            v_amount := (v_item->>'_')::float8;
            v_procurement_date := (v_item->>'_')::date;
            v_facility_id := (v_item->>'_')::uuid;
            PERFORM v_crop_type::mrv."ty_0004";
            PERFORM v_country_code::mrv."ty_0003";
            PERFORM v_unit::mrv."ty_0021";
        EXCEPTION WHEN invalid_text_representation OR invalid_parameter_value THEN
            RAISE EXCEPTION '_%_%_%_',
                v_crop_type, v_country_code, v_unit;
        END;

        IF p_0099 = '_' AND v_amount IS NOT NULL AND v_amount > 0 THEN
            SELECT COALESCE(SUM(hr."c_0512"), 0)
            INTO v_total_yield
            FROM mrv."t_0016" hr
            JOIN mrv."t_0006" c ON c.id = hr."c_0117"
            JOIN mrv."t_0012" f ON f.id = c."c_0187"
            WHERE c."c_0102"::text = v_crop_type
                AND c.c_0472 = 'v1'
                AND f."c_0172" = p_0098;

            IF v_amount > v_total_yield THEN
                RAISE EXCEPTION '_%_%_%_',
                    v_amount, v_total_yield, v_crop_type;
            END IF;
        END IF;

        INSERT INTO mrv."t_0021" (
            "c_0102", "c_0089", "c_0359", "c_0525",
            "c_0360", "c_0170",
            "c_0401", "c_0402",
            "c_0456", "c_0457",
            "c_0094", "c_0093", "c_0528"
        ) VALUES (
            v_crop_type::mrv."ty_0004",
            v_country_code::mrv."ty_0003",
            v_amount,
            v_unit::mrv."ty_0021",
            v_procurement_date,
            p_0026,
            p_0084,
            p_0085::mrv."ty_0007",
            p_0098,
            p_0099::mrv."ty_0007",
            v_current_user_id,
            NOW(),
            NOW()
        )
        ON CONFLICT ("c_0401", "c_0402", "c_0456", "c_0457", "c_0102", "c_0089")
        DO UPDATE SET
            "c_0359" = EXCLUDED."c_0359",
            "c_0525" = EXCLUDED."c_0525",
            "c_0360" = COALESCE(EXCLUDED."c_0360", mrv."t_0021"."c_0360"),
            "c_0528" = NOW()
        RETURNING * INTO v_procurement;

        IF v_facility_id IS NOT NULL THEN
            SELECT f."c_0172", f."c_0173"::text
            INTO v_facility_entity_id, v_facility_entity_type
            FROM mrv."t_0010" f
            WHERE f.id = v_facility_id;

            IF v_facility_entity_id IS NULL THEN
                RAISE EXCEPTION '_%_%_%_',
                    v_facility_id, v_crop_type, v_country_code;
            END IF;

            IF v_facility_entity_id != p_0084
                OR v_facility_entity_type != p_0085 THEN
                RAISE EXCEPTION '_%_%_%_',
                    v_facility_id, v_crop_type, v_country_code;
            END IF;

            INSERT INTO mrv."t_0022" ("c_0361", "c_0183", "c_0093")
            VALUES (v_procurement.id, v_facility_id, NOW())
            ON CONFLICT ON CONSTRAINT "procurement_facility_unique"
            DO NOTHING;
        END IF;

        RETURN QUERY SELECT
            v_procurement.id,
            v_procurement."c_0102"::text,
            v_procurement."c_0089"::text,
            v_procurement."c_0359"::float8,
            v_procurement."c_0525"::text,
            v_procurement."c_0360"::date,
            v_procurement."c_0170"::uuid,
            v_procurement."c_0401"::uuid,
            v_procurement."c_0402"::text,
            v_procurement."c_0456"::uuid,
            v_procurement."c_0457"::text,
            v_procurement."c_0094"::uuid,
            v_procurement."c_0093"::timestamptz,
            v_procurement."c_0528"::timestamptz;
    END LOOP;
END;
$$;




CREATE OR REPLACE FUNCTION mrv.f_0033(
    p_0050 uuid,
    p_0026 uuid,
    p_0012 int,
    p_0094 text,
    p_0091 text,
    p_0092 text,
    p_0034 int,
    p_0042 bigint,
    p_0047 timestamptz,
    p_0063 boolean
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    EXECUTE format('_%_%_', '_', '_');

    INSERT INTO mrv."t_0042" (
        id, "c_0170", "c_0057", "c_0428",
        "c_0419", "c_0420", "c_0186", "c_0200",
        "c_0215", "c_0214"
    ) VALUES (
        p_0050, p_0026, p_0012, p_0094,
        p_0091, p_0092, p_0034, p_0042,
        v_current_user_id, p_0047
    );

    IF p_0063 THEN
        INSERT INTO audit.t_0097 (c_0537, p_0001, action, c_0141)
        VALUES (
            v_current_user_id,
            p_0026,
            '_',
            jsonb_build_object(
                '_', p_0012,
                '_', p_0034,
                '_', p_0092
            )
        );
    END IF;
END;
$$;

COMMENT ON FUNCTION mrv.f_0033(uuid, uuid, int, text, text, text, int, bigint, timestamptz, boolean) IS E'_';

CREATE OR REPLACE FUNCTION mrv.f_0034(
    p_0050 uuid,
    p_0105 uuid,
    p_0069 text,
    p_0091 text,
    p_0092 text,
    p_0042 bigint,
    p_0047 timestamptz,
    p_0052 uuid DEFAULT NULL
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0069 NOT IN ('_', '_') THEN
        RAISE EXCEPTION '_%_', p_0069;
    END IF;

    IF NOT auth.f_0062() AND NOT auth.f_0119(p_0105 => p_0105) THEN
        RAISE EXCEPTION '_';
    END IF;

    EXECUTE format('_%_%_', '_', '_');

    INSERT INTO mrv."t_0032" (
        id, "c_0486", "c_0236", "c_0310",
        "c_0419", "c_0420", "c_0200",
        "c_0215", "c_0214"
    ) VALUES (
        p_0050, p_0105, p_0052, p_0069,
        p_0091, p_0092, p_0042,
        v_current_user_id, p_0047
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0035(
    p_0050 uuid,
    p_0105 uuid,
    p_0094 text,
    p_0091 text,
    p_0092 text,
    p_0034 int,
    p_0042 bigint,
    p_0047 timestamptz
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0062() AND NOT auth.f_0119(p_0105 => p_0105) THEN
        RAISE EXCEPTION '_';
    END IF;

    EXECUTE format('_%_%_', '_', '_');

    INSERT INTO mrv."t_0042" (
        id, "c_0486", "c_0428",
        "c_0419", "c_0420", "c_0186", "c_0200",
        "c_0215", "c_0214"
    ) VALUES (
        p_0050, p_0105, p_0094,
        p_0091, p_0092, p_0034, p_0042,
        v_current_user_id, p_0047
    );
END;
$$;






CREATE OR REPLACE FUNCTION mrv.f_0038(
    p_0027 uuid
)
RETURNS TABLE (
    c_0367 double precision,
    c_0077 int,
    c_0515 int,
    p_0004 uuid,
    c_0191 text,
    c_0227 boolean,
    c_0228 boolean,
    c_0262 boolean,
    c_0105 text
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0037 => p_0027)
        OR auth.f_0119(p_0105 => p_0027)
        OR auth.f_0119(p_0026 => p_0027)
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    WITH c_0196 AS (
        SELECT f.id AS p_0004, f.name AS c_0191
        FROM mrv."t_0012" f
        WHERE f."c_0172" = p_0027
    ),
    crop_data AS (
        SELECT DISTINCT c."c_0187" AS p_0004
        FROM mrv."t_0006" c
        JOIN mrv."t_0016" hr ON hr."c_0117" = c.id
        WHERE c."c_0187" IN (SELECT p_0004 FROM c_0196)
            AND c.c_0472 = 'v1'
    ),
    soil_data AS (
        SELECT DISTINCT p_0004 FROM (
            SELECT "c_0187" AS p_0004
            FROM mrv."t_0007"
            WHERE "c_0187" IN (SELECT p_0004 FROM c_0196)
            UNION
            SELECT "c_0187" AS p_0004
            FROM mrv."t_0013"
            WHERE "c_0187" IN (SELECT p_0004 FROM c_0196)
                AND "c_0357" = 'v1'
        ) soil_union
    ),
    farm_status AS (
        SELECT
            f.p_0004,
            f.c_0191,
            (cd.p_0004 IS NOT NULL) AS c_0227,
            (sd.p_0004 IS NOT NULL) AS c_0228,
            (cd.p_0004 IS NOT NULL AND sd.p_0004 IS NOT NULL) AS c_0262
        FROM c_0196 f
        LEFT JOIN crop_data cd ON cd.p_0004 = f.p_0004
        LEFT JOIN soil_data sd ON sd.p_0004 = f.p_0004
    ),
    farm_crops AS (
        SELECT DISTINCT c."c_0187" AS p_0004, c."c_0102"::text AS c_0105
        FROM mrv."t_0006" c
        WHERE c."c_0187" IN (SELECT p_0004 FROM c_0196)
            AND c.c_0472 = 'v1'
    ),
    counts AS (
        SELECT
            COUNT(*) AS c_0515,
            COUNT(*) FILTER (WHERE c_0262) AS c_0077
        FROM farm_status
    )
    SELECT
        CASE WHEN c.c_0515 > 0
            THEN (c.c_0077::double precision / c.c_0515::double precision) * 100.0
            ELSE 0.0
        END AS c_0367,
        c.c_0077::int,
        c.c_0515::int,
        fs.p_0004,
        fs.c_0191,
        fs.c_0227,
        fs.c_0228,
        fs.c_0262,
        fc.c_0105
    FROM farm_status fs
    CROSS JOIN counts c
    LEFT JOIN farm_crops fc ON fc.p_0004 = fs.p_0004;
END;
$$;



CREATE OR REPLACE FUNCTION mrv.f_0043(
    p_0026 uuid,
    p_0118 int
)
RETURNS TABLE (
    c_0518 double precision,
    p_0122 uuid,
    c_0488 text,
    c_0487 double precision,
    c_0461 text,
    c_0062 double precision,
    c_0105 text,
    c_0103 double precision
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 < 2000 OR p_0118 > EXTRACT(YEAR FROM NOW())::int + 5 THEN
        RAISE EXCEPTION '_%_', p_0118;
    END IF;

    RETURN QUERY
    WITH supply_volumes AS (
        SELECT
            sv."c_0486",
            sv."c_0455"::text AS c_0461,
            sv."c_0458",
            sv."c_0102"::text AS c_0105,
            sv."c_0381"
        FROM mrv."t_0028" sv
        WHERE sv."c_0170" = p_0026
            AND sv."c_0489" >= make_date(p_0118, 1, 1)
            AND sv."c_0489" <= make_date(p_0118, 12, 31)
    ),
    unique_source_ids AS (
        SELECT DISTINCT "c_0458" FROM supply_volumes
        WHERE c_0461 IN ('_', '_')
    ),
    
    source_farms AS (
        SELECT f.id AS p_0004, f."c_0172" AS con_0122
        FROM mrv."t_0012" f
        WHERE f."c_0172" IN (SELECT "c_0458" FROM unique_source_ids)
    ),
    source_farm_carbon AS (
        SELECT bcd.p_0004, bcd.c_0060
        FROM mrv.f_0016(
            ARRAY(SELECT p_0004 FROM source_farms),
            p_0118
        ) bcd
    ),
    
    source_yields AS (
        SELECT
            f."c_0172",
            c."c_0102"::text AS c_0105,
            SUM(COALESCE(hr."c_0512", 0)) AS c_0521
        FROM mrv."t_0016" hr
        JOIN mrv."t_0006" c ON c.id = hr."c_0117"
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE f."c_0172" IN (SELECT "c_0458" FROM unique_source_ids)
            AND c.c_0472 = 'v1'
            AND c."c_0222" >= make_date(p_0118, 1, 1)
            AND c."c_0222" <= make_date(p_0118, 12, 31)
        GROUP BY f."c_0172", c."c_0102"::text
    ),
    
    facility_supplier_ids AS (
        SELECT DISTINCT sv."c_0486"
        FROM supply_volumes sv
        WHERE sv.c_0461 = '_'
    ),
    facility_crop_types AS (
        SELECT DISTINCT sv.c_0105
        FROM supply_volumes sv
        WHERE sv.c_0461 = '_'
    ),
    facility_procurements AS (
        SELECT
            p.id AS c_0364,
            p."c_0401" AS p_0122,
            p."c_0102"::text AS c_0105,
            p."c_0359",
            p."c_0456",
            p."c_0457"::text AS c_0463,
            pf."c_0183"
        FROM mrv."t_0021" p
        JOIN mrv."t_0022" pf ON pf."c_0361" = p.id
        WHERE p."c_0401" IN (SELECT "c_0486" FROM facility_supplier_ids)
            AND p."c_0402" = 'v1'
            AND p."c_0102"::text IN (SELECT c_0105 FROM facility_crop_types)
            AND p."c_0360" >= make_date(p_0118, 1, 1)
            AND p."c_0360" <= make_date(p_0118, 12, 31)
    ),
    
    facility_farmer_entity_ids AS (
        SELECT DISTINCT fp."c_0456"
        FROM facility_procurements fp
        WHERE fp.c_0463 = '_'
    ),
    farmer_farms_for_facility AS (
        SELECT f.id AS p_0004, f."c_0172" AS con_0122
        FROM mrv."t_0012" f
        WHERE f."c_0172" IN (SELECT "c_0456" FROM facility_farmer_entity_ids)
    ),
    farmer_carbon_for_facility AS (
        SELECT bcd.p_0004, bcd.c_0060
        FROM mrv.f_0016(
            ARRAY(SELECT p_0004 FROM farmer_farms_for_facility),
            p_0118
        ) bcd
    ),
    farmer_yields_for_facility AS (
        SELECT
            f."c_0172",
            c."c_0102"::text AS c_0105,
            SUM(COALESCE(hr."c_0512", 0)) AS c_0521
        FROM mrv."t_0016" hr
        JOIN mrv."t_0006" c ON c.id = hr."c_0117"
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE f."c_0172" IN (SELECT "c_0456" FROM facility_farmer_entity_ids)
            AND c.c_0472 = 'v1'
            AND c."c_0222" >= make_date(p_0118, 1, 1)
            AND c."c_0222" <= make_date(p_0118, 12, 31)
        GROUP BY f."c_0172", c."c_0102"::text
    ),
    
    procurement_carbon_per_id AS (
        SELECT
            fp.c_0364,
            fp.p_0122,
            fp.c_0105,
            fp."c_0359",
            fp."c_0183",
            CASE
                WHEN fp.c_0463 <> '_' THEN 0.0
                WHEN fp."c_0359" IS NULL OR fp."c_0359" = 0 THEN 0.0
                WHEN COALESCE(fy.c_0521, 0) = 0 THEN 0.0
                ELSE LEAST(fp."c_0359" / fy.c_0521, 1.0) *
                    COALESCE((
                        SELECT SUM(ffc.c_0060)
                        FROM farmer_carbon_for_facility ffc
                        WHERE ffc.p_0004 IN (
                            SELECT p_0004 FROM farmer_farms_for_facility
                            WHERE con_0122 = fp."c_0456"
                        )
                    ), 0)
            END AS c_0060
        FROM facility_procurements fp
        LEFT JOIN farmer_yields_for_facility fy
            ON fy."c_0172" = fp."c_0456"
            AND fy.c_0105 = fp.c_0105
    ),
    
    facility_totals AS (
        SELECT
            fp."c_0183",
            fp.p_0122,
            fp.c_0105,
            SUM(COALESCE(fp."c_0359", 0)) AS total_quantity,
            SUM(pc.c_0060) AS total_carbon
        FROM facility_procurements fp
        JOIN procurement_carbon_per_id pc
            ON pc.c_0364 = fp.c_0364
        GROUP BY fp."c_0183", fp.p_0122, fp.c_0105
    ),
    
    supply_volume_carbon AS (
        SELECT
            sv."c_0486",
            sv.c_0461,
            sv.c_0105,
            CASE
                WHEN sv.c_0461 IN ('_', '_') THEN
                    CASE WHEN COALESCE(sy.c_0521, 0) > 0 THEN
                        LEAST(sv."c_0381" / sy.c_0521, 1.0) *
                        COALESCE((
                            SELECT SUM(sfc.c_0060)
                            FROM source_farm_carbon sfc
                            WHERE sfc.p_0004 IN (
                                SELECT p_0004 FROM source_farms WHERE con_0122 = sv."c_0458"
                            )
                        ), 0)
                    ELSE 0.0
                    END
                WHEN sv.c_0461 = '_' THEN
                    CASE WHEN COALESCE(ft.total_quantity, 0) > 0 THEN
                        LEAST(sv."c_0381" / ft.total_quantity, 1.0) * COALESCE(ft.total_carbon, 0)
                    ELSE 0.0
                    END
                ELSE 0.0
            END AS c_0059
        FROM supply_volumes sv
        LEFT JOIN source_yields sy
            ON sy."c_0172" = sv."c_0458"
            AND sy.c_0105 = sv.c_0105
            AND sv.c_0461 IN ('_', '_')
        LEFT JOIN facility_totals ft
            ON ft."c_0183" = sv."c_0458"
            AND ft.p_0122 = sv."c_0486"
            AND ft.c_0105 = sv.c_0105
            AND sv.c_0461 = '_'
    ),
    totals AS (
        SELECT SUM(c_0059) AS grand_total FROM supply_volume_carbon
    ),
    c_0051 AS (
        SELECT svc."c_0486", COALESCE(s.name, svc."c_0486"::text) AS c_0488, SUM(svc.c_0059) AS c_0059
        FROM supply_volume_carbon svc
        LEFT JOIN public.t_0092 s ON s.id = svc."c_0486"
        GROUP BY svc."c_0486", COALESCE(s.name, svc."c_0486"::text)
    ),
    by_category AS (
        SELECT c_0461, SUM(c_0059) AS c_0059
        FROM supply_volume_carbon
        GROUP BY c_0461
    ),
    c_0049 AS (
        SELECT c_0105, SUM(c_0059) AS c_0059
        FROM supply_volume_carbon
        GROUP BY c_0105
    )
    SELECT t.grand_total, NULL::uuid, NULL::text, NULL::double precision, NULL::text, NULL::double precision, NULL::text, NULL::double precision
    FROM totals t
    UNION ALL
    SELECT NULL, bs."c_0486", bs.c_0488, bs.c_0059, NULL, NULL, NULL, NULL
    FROM c_0051 bs
    UNION ALL
    SELECT NULL, NULL, NULL, NULL, bc.c_0461, bc.c_0059, NULL, NULL
    FROM by_category bc
    UNION ALL
    SELECT NULL, NULL, NULL, NULL, NULL, NULL, bcp.c_0105, bcp.c_0059
    FROM c_0049 bcp;
END;
$$;

















CREATE OR REPLACE FUNCTION mrv.f_0044(
    p_0026 uuid
)
RETURNS TABLE (
    c_0371 uuid,
    c_0372 text,
    c_0373 text,
    c_0238 uuid,
    c_0239 text,
    p_0122 uuid,
    c_0488 text,
    c_0100 date,
    c_0099 date,
    c_0189 bigint,
    c_0043 double precision,
    c_0285 uuid,
    c_0287 text,
    c_0284 timestamptz,
    c_0286 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    WITH enterprise_supplier_ids AS (
        SELECT es.p_0122
        FROM public.t_0050 es
        WHERE es.p_0001 = p_0026
    ),
    latest_plans AS (
        SELECT DISTINCT ON (cp."c_0172")
            cp."c_0172"     AS p_0122,
            cp.id             AS carbon_plan_id,
            cp.c_0472::text   AS carbon_plan_status,
            cp."c_0093"    AS carbon_plan_created_at,
            cp.c_0413        AS carbon_plan_results
        FROM mrv."t_0001" cp
        WHERE cp."c_0172" IN (SELECT esi.p_0122 FROM enterprise_supplier_ids esi)
        ORDER BY cp."c_0172", cp."c_0093" DESC, cp.id DESC
    )
    SELECT
        vp.id                                  AS c_0371,
        vp.name                                AS c_0372,
        vp.c_0472::text                        AS c_0373,
        vpi.id                                 AS c_0238,
        vpi.c_0472::text                       AS c_0239,
        vpi."c_0486"                       AS p_0122,
        s.name                                 AS c_0488,
        vp."c_0098"              AS c_0100,
        vp."c_0097"                AS c_0099,
        COALESCE((
            SELECT COUNT(*)
            FROM mrv."t_0037" vpf
            WHERE vpf."c_0236" = vpi.id
        ), 0)                                  AS c_0189,
        CASE
            WHEN vp."c_0147" IS NULL THEN NULL
            ELSE ST_Area(vp."c_0147"::geography) / 10000.0
        END                                    AS c_0043,
        lp.carbon_plan_id                      AS c_0285,
        lp.carbon_plan_status                  AS c_0287,
        lp.carbon_plan_created_at              AS c_0284,
        lp.carbon_plan_results                 AS c_0286
    FROM mrv."t_0038" vpi
    JOIN mrv."t_0033" vp ON vp.id = vpi."c_0369"
    JOIN public.t_0092 s ON s.id = vpi."c_0486"
    LEFT JOIN latest_plans lp ON lp.p_0122 = vpi."c_0486"
    WHERE vpi."c_0486" IN (SELECT esi.p_0122 FROM enterprise_supplier_ids esi)
      AND vpi."c_0486" IS NOT NULL
      AND vpi."c_0472" <> 'v1'
    ORDER BY vp.name, s.name, vpi.id;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0045(
    p_0105 uuid
)
RETURNS TABLE (
    id uuid,
    name text
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
BEGIN
    RETURN QUERY
    SELECT DISTINCT e.id, e.name
    FROM public.t_0051 e
    WHERE e.id IN (
        SELECT p."c_0401"
        FROM mrv."t_0021" p
        WHERE p."c_0456" = p_0105
            AND p."c_0457" = 'v1'
            AND p."c_0402" = 'v1'
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0047(
    p_0004 uuid
)
RETURNS NUMERIC
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN (
        SELECT ST_Area(p.geometry::geography) / 10000
        FROM mrv."Polygon" p
        JOIN mrv."t_0012" f ON f."c_0355" = p.id
        WHERE f.id = p_0004
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0048(
    p_0035 uuid,
    p_0012 int
)
RETURNS TABLE (
    c_0314 text,
    c_0316 text,
    c_0309 text,
    c_0311 int,
    c_0067 text,
    c_0155 jsonb,
    c_0054 text
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_entity_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT f."c_0172" INTO v_entity_id
    FROM mrv."t_0012" f
    WHERE f.id = p_0035;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_entity_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0026 => v_entity_id)
        OR auth.f_0119(p_0105 => v_entity_id)
        OR auth.f_0119(p_0037 => v_entity_id)
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT
        (dcr."c_0055"->>'_')::text,
        (dcr."c_0055"->>'_')::text,
        (dcr."c_0055"->>'_')::text,
        (dcr."c_0055"->>'_')::int,
        (dcr."c_0055"->>'_')::text,
        dcr."c_0055"->'_',
        (dcr."c_0055"->>'_')::text
    FROM mrv."t_0007" dcr
    WHERE dcr."c_0187" = p_0035
        AND dcr."c_0057" = p_0012
    LIMIT 1;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0049(
    p_0037 uuid,
    p_0016 text
)
RETURNS TABLE (
    p_0005 uuid,
    c_0105 text,
    c_0521 float8
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
BEGIN
    RETURN QUERY
    SELECT
        p_0037 AS "c_0192",
        p_0016 AS "c_0102",
        COALESCE(SUM(hr."c_0512"), 0)::float8 AS "c_0512"
    FROM mrv."t_0016" hr
    JOIN mrv."t_0006" c ON c.id = hr."c_0117"
    JOIN mrv."t_0012" f ON f.id = c."c_0187"
    WHERE c."c_0102"::text = p_0016
        AND c.c_0472 = 'v1'
        AND f."c_0172" = p_0037;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0050(
    p_0026 uuid,
    p_0118 int
)
RETURNS TABLE (
    c_0517 numeric,
    c_0319 numeric,
    c_0543 numeric,
    c_0069 numeric,
    c_0050 jsonb,
    c_0051 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 < 2000 OR p_0118 > EXTRACT(YEAR FROM NOW())::int + 5 THEN
        RAISE EXCEPTION '_%_', p_0118;
    END IF;

    RETURN QUERY
    WITH enterprise_farms AS (
        SELECT
            fm.id AS p_0004,
            fm."c_0172" AS con_0122,
            p.geometry,
            ST_Area(p.geometry::geography) / 10000 AS c_0018
        FROM mrv."t_0012" fm
        JOIN mrv."Polygon" p ON p.id = fm."c_0355"
        WHERE fm."c_0172" IN (
            SELECT es.p_0122 FROM public.t_0050 es WHERE es.p_0001 = p_0026
        )
        UNION
        
        SELECT
            fm.id,
            fm."c_0172",
            p.geometry,
            ST_Area(p.geometry::geography) / 10000
        FROM mrv."t_0012" fm
        JOIN mrv."Polygon" p ON p.id = fm."c_0355"
        JOIN public.t_0091 sf ON sf.p_0005 = fm."c_0172"
        JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
        WHERE es.p_0001 = p_0026
    ),
    farm_supplier_mapping AS (
        SELECT ef.p_0004, ef.con_0122 AS p_0122
        FROM enterprise_farms ef
        WHERE ef.con_0122 IN (
            SELECT es.p_0122 FROM public.t_0050 es WHERE es.p_0001 = p_0026
        )
        UNION
        SELECT ef.p_0004, sf.p_0122
        FROM enterprise_farms ef
        JOIN public.t_0091 sf ON sf.p_0005 = ef.con_0122
    ),
    enterprise_cultivations AS (
        SELECT
            c.id AS cultivation_id,
            c."c_0187" AS p_0004,
            c."c_0436" AS c_0437
        FROM mrv."t_0006" c
        WHERE c."c_0187" IN (SELECT p_0004 FROM enterprise_farms)
            AND EXTRACT(YEAR FROM c."c_0351") = p_0118
    ),
    fertilizer_data AS (
        SELECT
            fa.id AS application_id,
            fa."c_0198"::text AS fertilizer_type,
            fa.c_0010 AS amount_kg,
            fa."c_0117" AS cultivation_id
        FROM mrv."t_0014" fa
        WHERE fa."c_0117" IN (SELECT cultivation_id FROM enterprise_cultivations)
    ),
    fertilizer_with_co2e AS (
        SELECT
            fd.application_id,
            fd.fertilizer_type,
            fd.amount_kg,
            fd.cultivation_id,
            ef."c_0068" AS co2e_per_kg
        FROM fertilizer_data fd
        LEFT JOIN mrv."t_0015" ef ON ef."c_0198"::text = fd.fertilizer_type
    ),
    harvest_data AS (
        SELECT
            hr."c_0117" AS cultivation_id,
            COALESCE(hr."c_0512", 0) AS c_0521
        FROM mrv."t_0016" hr
        WHERE hr."c_0117" IN (SELECT cultivation_id FROM enterprise_cultivations)
    ),
    fertilized_cultivations AS (
        SELECT DISTINCT cultivation_id FROM fertilizer_data
    ),
    farms_with_fertilizer AS (
        SELECT DISTINCT c.p_0004
        FROM enterprise_cultivations c
        WHERE c.cultivation_id IN (SELECT cultivation_id FROM fertilized_cultivations)
    ),
    total_area AS (
        SELECT COALESCE(SUM(ef_farm.c_0018), 0) AS total_ha
        FROM enterprise_farms ef_farm
        WHERE ef_farm.p_0004 IN (SELECT p_0004 FROM farms_with_fertilizer)
    ),
    totals AS (
        SELECT
            COALESCE(SUM(amount_kg), 0) AS total_n_kg,
            SUM(amount_kg * co2e_per_kg) AS total_co2e_kg
        FROM fertilizer_with_co2e
    ),
    c_0521 AS (
        SELECT COALESCE(SUM(c_0521), 0) AS total_yield_kg
        FROM harvest_data
        WHERE cultivation_id IN (SELECT cultivation_id FROM fertilized_cultivations)
    ),
    by_type AS (
        SELECT
            COALESCE(jsonb_agg(
                jsonb_build_object(
                    '_', fertilizer_type,
                    '_', type_amount_kg,
                    '_', type_co2e_kg,
                    '_', c_0189
                )
            ), 'null'::jsonb) AS c_0411
        FROM (
            SELECT
                fwc.fertilizer_type,
                SUM(fwc.amount_kg) AS type_amount_kg,
                SUM(fwc.amount_kg * fwc.co2e_per_kg) AS type_co2e_kg,
                COUNT(DISTINCT ec.p_0004) AS c_0189
            FROM fertilizer_with_co2e fwc
            JOIN enterprise_cultivations ec ON ec.cultivation_id = fwc.cultivation_id
            GROUP BY fwc.fertilizer_type
        ) typed
    ),
    supplier_areas AS (
        SELECT
            fsm.p_0122,
            SUM(ef_farm.c_0018) AS c_0514
        FROM farm_supplier_mapping fsm
        JOIN enterprise_farms ef_farm ON ef_farm.p_0004 = fsm.p_0004
        WHERE fsm.p_0004 IN (SELECT p_0004 FROM farms_with_fertilizer)
        GROUP BY fsm.p_0122
    ),
    supplier_yields AS (
        SELECT
            fsm.p_0122,
            COALESCE(SUM(hd.c_0521), 0) AS total_yield_kg
        FROM farm_supplier_mapping fsm
        JOIN enterprise_cultivations ec ON ec.p_0004 = fsm.p_0004
        LEFT JOIN harvest_data hd ON hd.cultivation_id = ec.cultivation_id
        WHERE ec.cultivation_id IN (SELECT cultivation_id FROM fertilized_cultivations)
        GROUP BY fsm.p_0122
    ),
    by_sup AS (
        SELECT
            COALESCE(jsonb_agg(
                jsonb_build_object(
                    '_', p_0122,
                    '_', c_0488,
                    '_', supplier_n_per_ha,
                    '_', supplier_co2e_per_tonne_yield
                )
            ), 'null'::jsonb) AS c_0411
        FROM (
            SELECT
                s.id AS p_0122,
                s.name AS c_0488,
                SUM(fwc.amount_kg) /
                    NULLIF(sa.c_0514, 0) AS supplier_n_per_ha,
                SUM(fwc.amount_kg * fwc.co2e_per_kg) /
                    NULLIF(sy.total_yield_kg / 1000, 0) AS supplier_co2e_per_tonne_yield
            FROM fertilizer_with_co2e fwc
            JOIN enterprise_cultivations ec ON ec.cultivation_id = fwc.cultivation_id
            JOIN farm_supplier_mapping fsm ON fsm.p_0004 = ec.p_0004
            JOIN public.t_0092 s ON s.id = fsm.p_0122
            JOIN public.t_0050 es ON es.p_0122 = s.id
            JOIN supplier_areas sa ON sa.p_0122 = s.id
            JOIN supplier_yields sy ON sy.p_0122 = s.id
            WHERE es.p_0001 = p_0026
            GROUP BY s.id, s.name, sa.c_0514, sy.total_yield_kg
        ) per_supplier
    )
    SELECT
        t.total_n_kg::numeric AS c_0517,
        (t.total_n_kg / NULLIF(ta.total_ha, 0))::numeric AS c_0319,
        (ty.total_yield_kg / NULLIF(t.total_n_kg, 0))::numeric AS c_0543,
        (t.total_co2e_kg / NULLIF(ty.total_yield_kg / 1000, 0))::numeric AS c_0069,
        bt.c_0411 AS c_0050,
        bs.c_0411 AS c_0051
    FROM totals t
    CROSS JOIN total_area ta
    CROSS JOIN c_0521 ty
    CROSS JOIN by_type bt
    CROSS JOIN by_sup bs;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0052(
    p_0105 uuid,
    p_0036 uuid[]
)
RETURNS TABLE (
    p_0004 uuid
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0105 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0036 IS NOT NULL AND array_length(p_0036, 1) > 0 THEN
        RETURN QUERY
        SELECT f.id
        FROM mrv."t_0012" f
        WHERE f.id = ANY(p_0036)
          AND f."c_0172" = p_0105;
    ELSE
        RETURN QUERY
        SELECT f.id
        FROM mrv."t_0012" f
        WHERE f."c_0172" = p_0105;
    END IF;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0053(
    p_0073 uuid
)
RETURNS TABLE (
    id uuid,
    con_0122 uuid,
    c_0294 text,
    c_0329 timestamptz,
    c_0273 uuid,
    c_0472 text,
    c_0219 text,
    c_0497 int,
    c_0498 double precision,
    c_0127 double precision,
    c_0037 boolean[],
    c_0264 boolean,
    c_0124 boolean[],
    c_0266 boolean,
    c_0126 double precision,
    c_0265 boolean,
    c_0125 double precision,
    c_0128 text,
    c_0267 boolean,
    c_0210 boolean[],
    c_0269 boolean,
    c_0212 double precision,
    c_0268 boolean,
    c_0211 double precision,
    c_0213 text,
    c_0176 text,
    c_0413 jsonb,
    c_0190 uuid[],
    c_0095 timestamptz,
    c_0529 timestamptz,
    c_0196 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_supplier_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0073 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (
        auth.f_0066()
        -> '_' -> '_' ->> 0
    )::uuid;

    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1
        FROM public.t_0092 s
        WHERE s.id = v_supplier_id
          AND s.ty_0034 = 'v1'
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT
        cp.id,
        cp."c_0172",
        cp.c_0294,
        cp."c_0328",
        cp."c_0272",
        cp.c_0472::text,
        cp."c_0218"::text,
        cp."c_0494",
        cp."c_0495",
        cp."c_0121",
        cp."c_0036",
        cp."c_0251",
        cp."c_0118",
        cp."c_0253",
        cp."c_0120",
        cp."c_0252",
        cp."c_0119",
        cp."c_0122"::text,
        cp."c_0254",
        cp."c_0206",
        cp."c_0256",
        cp."c_0208",
        cp."c_0255",
        cp."c_0207",
        cp."c_0209"::text,
        cp."c_0175",
        cp.c_0413,
        ARRAY(
            SELECT cpf."c_0187"
            FROM mrv."t_0002" cpf
            WHERE cpf."c_0058" = cp.id
              AND cpf."c_0187" IS NOT NULL
        ),
        cp."c_0093",
        cp."c_0528",
        (
            SELECT jsonb_agg(jsonb_build_object(
                '_', f.id,
                '_', f.name,
                '_', CASE WHEN f."c_0355" IS NOT NULL THEN (
                    SELECT ST_Area(p.geometry::geography) / 10000.0
                    FROM mrv."Polygon" p
                    WHERE p.id = f."c_0355"
                ) END,
                '_', (
                    SELECT c."c_0102"::text
                    FROM mrv."t_0006" c
                    WHERE c."c_0187" = f.id
                    ORDER BY c."c_0351" DESC
                    LIMIT 1
                )
            ) ORDER BY f.id)
            FROM mrv."t_0002" cpf2
            JOIN mrv."t_0012" f ON f.id = cpf2."c_0187"
            WHERE cpf2."c_0058" = cp.id
        )
    FROM mrv."t_0001" cp
    WHERE cp.id = p_0073
      AND cp."c_0172" = v_supplier_id;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0054()
RETURNS TABLE (
    id uuid,
    con_0122 uuid,
    c_0294 text,
    c_0329 timestamptz,
    c_0273 uuid,
    c_0472 text,
    c_0219 text,
    c_0497 int,
    c_0498 double precision,
    c_0127 double precision,
    c_0037 boolean[],
    c_0264 boolean,
    c_0124 boolean[],
    c_0266 boolean,
    c_0126 double precision,
    c_0265 boolean,
    c_0125 double precision,
    c_0128 text,
    c_0267 boolean,
    c_0210 boolean[],
    c_0269 boolean,
    c_0212 double precision,
    c_0268 boolean,
    c_0211 double precision,
    c_0213 text,
    c_0176 text,
    c_0413 jsonb,
    c_0190 uuid[],
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_supplier_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (
        auth.f_0066()
        -> '_' -> '_' ->> 0
    )::uuid;

    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1
        FROM public.t_0092 s
        WHERE s.id = v_supplier_id
          AND s.ty_0034 = 'v1'
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT
        cp.id,
        cp."c_0172",
        cp.c_0294,
        cp."c_0328",
        cp."c_0272",
        cp.c_0472::text,
        cp."c_0218"::text,
        cp."c_0494",
        cp."c_0495",
        cp."c_0121",
        cp."c_0036",
        cp."c_0251",
        cp."c_0118",
        cp."c_0253",
        cp."c_0120",
        cp."c_0252",
        cp."c_0119",
        cp."c_0122"::text,
        cp."c_0254",
        cp."c_0206",
        cp."c_0256",
        cp."c_0208",
        cp."c_0255",
        cp."c_0207",
        cp."c_0209"::text,
        cp."c_0175",
        cp.c_0413,
        ARRAY(
            SELECT cpf."c_0187"
            FROM mrv."t_0002" cpf
            WHERE cpf."c_0058" = cp.id
              AND cpf."c_0187" IS NOT NULL
        ),
        cp."c_0093",
        cp."c_0528"
    FROM mrv."t_0001" cp
    WHERE cp."c_0172" = v_supplier_id
    ORDER BY cp."c_0093" DESC
    LIMIT 1;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0055()
RETURNS TABLE (
    id uuid,
    c_0472 text
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_supplier_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (
        auth.f_0066()
        -> '_' -> '_' ->> 0
    )::uuid;

    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1
        FROM public.t_0092 s
        WHERE s.id = v_supplier_id
          AND s.ty_0034 = 'v1'
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT
        cp.id,
        cp.c_0472::text
    FROM mrv."t_0001" cp
    WHERE cp."c_0172" = v_supplier_id
    ORDER BY cp."c_0093" DESC
    LIMIT 1;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0058(
    p_0051 text,
    p_0083 text,
    p_0088 float8 DEFAULT 1
)
RETURNS TABLE (
    c_0430 float8,
    c_0432 float8,
    c_0433 float8
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
BEGIN
    RETURN QUERY
    SELECT
        ief."c_0430" * COALESCE(p_0088, 1),
        ief."c_0432" * COALESCE(p_0088, 1),
        ief."c_0433" * COALESCE(p_0088, 1)
    FROM mrv."t_0017" ief
    WHERE ief."c_0234" = p_0051
      AND ief."c_0396" = p_0083;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_%_%_', p_0051, p_0083;
    END IF;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0060(
    p_0116 uuid,
    p_0026 uuid,
    p_0031 int
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_expires_at timestamptz;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0031 < 1 OR p_0031 > 365 THEN
        RAISE EXCEPTION '_%_', p_0031;
    END IF;

    v_expires_at := now() + make_interval(days => p_0031);

    UPDATE public.t_0096
    SET c_0415 = clock_timestamp(), c_0416 = v_current_user_id
    WHERE c_0537 = p_0116
      AND p_0001 = p_0026
      AND c_0415 IS NULL;

    INSERT INTO public.t_0096 (c_0537, p_0001, c_0221, c_0220, c_0181)
    VALUES (p_0116, p_0026, v_current_user_id, clock_timestamp(), v_expires_at);

    
    INSERT INTO audit.t_0097 (c_0537, p_0001, action, c_0141)
    VALUES (
        p_0116,
        p_0026,
        '_',
        jsonb_build_object(
            '_', v_current_user_id,
            '_', v_expires_at,
            '_', p_0031
        )
    );
END;
$$;



CREATE OR REPLACE FUNCTION mrv.f_0068()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_type text;
    v_farm_id uuid;
    v_date timestamp(3);
    v_fk_column text;
BEGIN
    CASE TG_TABLE_NAME
        WHEN '_' THEN
            v_type := '_';
            v_date := NEW."c_0017";
            v_farm_id := (SELECT c."c_0187" FROM mrv."t_0006" c WHERE c.id = NEW."c_0117");
            v_fk_column := '_';
        WHEN '_' THEN
            v_type := '_';
            v_date := NEW."c_0222";
            v_farm_id := (SELECT c."c_0187" FROM mrv."t_0006" c WHERE c.id = NEW."c_0117");
            v_fk_column := '_';
        WHEN '_' THEN
            v_type := '_';
            v_date := NEW."c_0422";
            v_farm_id := NEW."c_0187";
            v_fk_column := '_';
        WHEN '_' THEN
            v_type := '_';
            v_date := NEW."c_0483";
            v_farm_id := NEW."c_0187";
            v_fk_column := '_';
        ELSE
            RAISE EXCEPTION '_%_', TG_TABLE_NAME;
    END CASE;

    IF v_farm_id IS NULL THEN
        RAISE EXCEPTION '_%_%_', TG_TABLE_NAME, NEW.id;
    END IF;

    EXECUTE format(
        '_%_',
        v_fk_column
    ) USING v_type, v_date, 'null'::jsonb, v_farm_id, NEW.id;

    RETURN NEW;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0069(p_0093 UUID)
RETURNS TABLE (
  c_0520 INT,
  c_0513    INT,
  c_0029    INT,
  c_0540    INT,
  c_0527 INT,
  c_0182    INT
)
LANGUAGE plpgsql
VOLATILE
SET search_path = mrv, public
AS $$
DECLARE
  v_threshold_auto_meters CONSTANT FLOAT := 50.0;
  v_threshold_warn_meters CONSTANT FLOAT := 500.0;
BEGIN
  
  
  
  UPDATE mrv.t_0082
  SET
    c_0303         = '_',
    c_0304 = NULL,
    c_0302     = NULL
  WHERE c_0425 = p_0093
    AND c_0303 <> '_';

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  UPDATE mrv.t_0082 AS actual
  SET
    c_0303         = winner.c_0303,
    c_0304 = winner.suggested_id,
    c_0302     = winner.distance_m
  FROM (
    
    SELECT DISTINCT ON (nearest.suggested_id)
      nearest.actual_id,
      nearest.suggested_id,
      nearest.distance_m,
      CASE
        WHEN nearest.distance_m <= v_threshold_auto_meters THEN '_'
        ELSE '_'
      END AS c_0303
    FROM (
      
      
      SELECT DISTINCT ON (a.id)
        a.id                                                       AS actual_id,
        s.id                                                       AS suggested_id,
        ST_Distance(a.c_0295::geography, s.c_0295::geography)  AS distance_m
      FROM mrv.t_0082 a
      JOIN mrv.t_0085 st_actual
        ON st_actual.c_0425 = p_0093
        AND ST_Within(a.c_0295, st_actual.geometry)
      JOIN mrv.t_0083 s
        ON s.c_0425 = p_0093
        AND s.c_0479 = st_actual.id
      WHERE a.c_0425 = p_0093
        AND a.c_0303 <> '_'
        AND ST_Distance(a.c_0295::geography, s.c_0295::geography) <= v_threshold_warn_meters
        AND NOT EXISTS (
          SELECT 1
          FROM mrv.t_0082 manual
          WHERE manual.c_0425 = p_0093
            AND manual.c_0303 = '_'
            AND manual.c_0304 = s.id
        )
      ORDER BY a.id, ST_Distance(a.c_0295::geography, s.c_0295::geography), s.id
    ) AS nearest
    ORDER BY nearest.suggested_id, nearest.distance_m, nearest.actual_id
  ) AS winner
  WHERE winner.actual_id = actual.id;

  
  RETURN QUERY
  SELECT
    (SELECT COUNT(*)::INT FROM mrv.t_0083   WHERE c_0425 = p_0093),
    (SELECT COUNT(*)::INT FROM mrv.t_0082      WHERE c_0425 = p_0093),
    (SELECT COUNT(*)::INT FROM mrv.t_0082      WHERE c_0425 = p_0093 AND c_0303 IN ('_', '_')),
    (SELECT COUNT(*)::INT FROM mrv.t_0082      WHERE c_0425 = p_0093 AND c_0303 = '_'),
    (
      
      SELECT COUNT(*)::INT
      FROM mrv.t_0083 sug
      WHERE sug.c_0425 = p_0093
        AND NOT EXISTS (
          SELECT 1
          FROM mrv.t_0082 act
          WHERE act.c_0304 = sug.id
        )
    ),
    (
      
      SELECT COUNT(*)::INT
      FROM mrv.t_0082
      WHERE c_0425 = p_0093
        AND c_0303 = '_'
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0072(
    p_0095 text,
    p_0027 uuid DEFAULT NULL
)
RETURNS TABLE (
    c_0090 text,
    c_0195 int,
    c_0496 int
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
BEGIN
    RETURN QUERY
    WITH farm_counts AS (
        SELECT
            f."ty_0003"::text AS c_0090,
            COUNT(f.id)::int AS c_0189
        FROM mrv."t_0012" f
        WHERE f."ty_0003" IS NOT NULL
            AND (p_0027 IS NULL OR f."c_0172" = p_0027)
            AND (p_0095 IS NULL OR f."ty_0016"::text = p_0095)
        GROUP BY f."ty_0003"
    ),
    crop_counts AS (
        SELECT
            f."ty_0003"::text AS c_0090,
            COUNT(DISTINCT c."c_0102")::int AS crop_count
        FROM mrv."t_0012" f
        LEFT JOIN mrv."t_0006" c ON c."c_0187" = f.id
        WHERE f."ty_0003" IS NOT NULL
            AND (p_0027 IS NULL OR f."c_0172" = p_0027)
            AND (p_0095 IS NULL OR f."ty_0016"::text = p_0095)
        GROUP BY f."ty_0003"
    )
    SELECT
        fc.c_0090,
        fc.c_0189,
        COALESCE(cc.crop_count, 0)
    FROM farm_counts fc
    LEFT JOIN crop_counts cc ON cc.c_0090 = fc.c_0090;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0074(
    p_0066 float8,
    p_0068 float8,
    p_0081 float8
)
RETURNS TABLE (
    id uuid,
    p_0004 uuid,
    geometry text
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_user_id uuid;
    v_rbac jsonb;
    v_entity_ids uuid[];
BEGIN
    v_user_id := auth.f_0102();
    IF v_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_rbac := auth.f_0066()->'_';

    v_entity_ids := ARRAY[v_user_id];
    IF v_rbac IS NOT NULL THEN
        v_entity_ids := v_entity_ids || ARRAY(
            SELECT x::uuid FROM jsonb_array_elements_text(
                COALESCE(v_rbac->'_', 'null'::jsonb) ||
                COALESCE(v_rbac->'_', 'null'::jsonb) ||
                COALESCE(v_rbac->'_', 'null'::jsonb)
            ) x
            WHERE x IS NOT NULL AND x <> '_'
        );
    END IF;

    RETURN QUERY
    SELECT
        p.id,
        p."c_0187" AS p_0004,
        ST_AsGeoJSON(p.geometry)::text AS geometry
    FROM mrv."Polygon" p
    LEFT JOIN mrv."t_0012" f ON f.id = p."c_0187"
    WHERE ST_DWithin(
        p.geometry::geography,
        ST_SetSRID(ST_MakePoint(p_0068, p_0066), 4326)::geography,
        p_0081
    )
    AND (
        auth.f_0062()
        OR p."c_0187" IS NULL
        OR f."c_0172" = ANY(v_entity_ids)
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0075(
    p_0026 uuid,
    p_0118 int
)
RETURNS TABLE (
    c_0516 int,
    c_0514 numeric,
    c_0091 int,
    c_0092 numeric,
    c_0502 jsonb,
    c_0106 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 < 2000 OR p_0118 > EXTRACT(YEAR FROM NOW())::int + 5 THEN
        RAISE EXCEPTION '_%_', p_0118;
    END IF;

    RETURN QUERY
    WITH enterprise_farms AS (
        
        SELECT f.id AS p_0004, f."c_0355"
        FROM mrv."t_0012" f
        JOIN public.t_0050 es ON es.p_0122 = f."c_0172"
        WHERE es.p_0001 = p_0026
        UNION
        
        SELECT f.id AS p_0004, f."c_0355"
        FROM mrv."t_0012" f
        JOIN public.t_0091 sf ON sf.p_0005 = f."c_0172"
        JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
        WHERE es.p_0001 = p_0026
    ),
    year_cultivations AS (
        SELECT
            c.id AS cultivation_id,
            c."c_0187",
            c."c_0102"::text AS c_0105,
            c."c_0250",
            c."c_0501"::text AS c_0503
        FROM mrv."t_0006" c
        JOIN enterprise_farms ef ON ef.p_0004 = c."c_0187"
        WHERE c."c_0351" >= make_date(p_0118, 1, 1)
          AND c."c_0351" < make_date(p_0118 + 1, 1, 1)
    ),
    farms_with_cultivations AS (
        SELECT DISTINCT "c_0187" FROM year_cultivations
    ),
    farm_areas AS (
        SELECT
            ef.p_0004,
            ST_Area(p.geometry::geography) / 10000 AS c_0018
        FROM enterprise_farms ef
        JOIN mrv."Polygon" p ON p.id = ef."c_0355"
        WHERE ef.p_0004 IN (SELECT "c_0187" FROM farms_with_cultivations)
    ),
    counts AS (
        SELECT
            COUNT(DISTINCT "c_0187")::int                                    AS c_0516,
            COUNT(*) FILTER (WHERE "c_0250" = true)::int               AS c_0091,
            COUNT(*)::int                                                     AS total_cultivations,
            COUNT(*) FILTER (WHERE c_0503 = '_')::int      AS till_conventional,
            COUNT(*) FILTER (WHERE c_0503 = '_')::int           AS till_reduced,
            COUNT(*) FILTER (WHERE c_0503 = '_')::int           AS till_no_till,
            COUNT(*) FILTER (WHERE c_0503 IS NULL)::int               AS till_unknown
        FROM year_cultivations
    ),
    crop_counts AS (
        SELECT c_0105, COUNT(*)::int AS cnt
        FROM year_cultivations
        GROUP BY c_0105
    )
    SELECT
        c.c_0516,
        COALESCE((SELECT SUM(c_0018) FROM farm_areas), 0)::numeric        AS c_0514,
        c.c_0091,
        CASE
            WHEN c.total_cultivations = 0 THEN 0
            ELSE (c.c_0091::numeric / c.total_cultivations::numeric)
        END                                                                  AS c_0092,
        jsonb_build_object(
            '_', c.till_conventional,
            '_',      c.till_reduced,
            '_',      c.till_no_till,
            '_',      c.till_unknown
        )                                                                    AS c_0502,
        COALESCE(
            (SELECT jsonb_object_agg(c_0105, cnt) FROM crop_counts),
            'null'::jsonb
        )                                                                    AS c_0106
    FROM counts c;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0079(p_0078 UUID)
RETURNS TABLE (
    id                     UUID,
    c_0296       TEXT,
    c_0071        DATE,
    c_0444            DOUBLE PRECISION,
    c_0048     DOUBLE PRECISION,
    c_0065           DOUBLE PRECISION,
    c_0423        DOUBLE PRECISION,
    c_0466      UUID,
    c_0467    TEXT
)
LANGUAGE plpgsql
STABLE
SET search_path = mrv, public
AS $$
DECLARE
    v_project_geometry public.geometry;
BEGIN
    
    
    
    SELECT ST_Union(s.geometry)
    INTO v_project_geometry
    FROM mrv.t_0085 s
    WHERE s.c_0425 = p_0078
      AND s.geometry IS NOT NULL;

    IF v_project_geometry IS NULL THEN
        RETURN;
    END IF;

    RETURN QUERY
    SELECT
        a.id,
        ST_AsGeoJSON(a.c_0295)::TEXT  AS c_0296,
        a.c_0071,
        a.c_0444,
        a.c_0048,
        a.c_0065,
        a.c_0423,
        p.id                            AS c_0466,
        p.name                          AS c_0467
    FROM mrv.t_0082 a
    JOIN mrv.t_0084 p ON p.id = a.c_0425
    WHERE a.c_0425 <> p_0078
      AND a.c_0071 IS NOT NULL
      AND ST_Within(a.c_0295, v_project_geometry)
    ORDER BY a.c_0095 DESC;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0080(
    p_0084 uuid,
    p_0085 text,
    p_0098 uuid,
    p_0099 text
)
RETURNS TABLE (
    id uuid,
    c_0090 text,
    c_0105 text,
    c_0362 float8,
    c_0193 int
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
BEGIN
    RETURN QUERY
    WITH country_crop_pairs AS (
        SELECT
            p."c_0089"::text,
            p."c_0102"::text,
            MIN(p.id::text)::uuid AS id
        FROM mrv."t_0021" p
        WHERE p."c_0401" = p_0084
            AND p."c_0402"::text = p_0085
            AND p."c_0456" = p_0098
            AND p."c_0457"::text = p_0099
            AND p."c_0089" IS NOT NULL
            AND p."c_0102" IS NOT NULL
        GROUP BY p."c_0089", p."c_0102"
    )
    SELECT
        ccp.id,
        ccp."c_0089",
        ccp."c_0102",
        COALESCE(SUM(fp."c_0359"), 0)::float8 AS "c_0359",
        COUNT(DISTINCT fp."c_0456")::int AS "farmerCount"
    FROM country_crop_pairs ccp
    
    
    LEFT JOIN mrv."t_0021" fp ON
        fp."c_0089"::text = ccp."c_0089"
        AND fp."c_0102"::text = ccp."c_0102"
        AND fp."c_0401" = p_0098
        AND fp."c_0402" = 'v1'
        AND fp."c_0457" = 'v1'
        AND fp."c_0456" IS NOT NULL
    GROUP BY ccp.id, ccp."c_0089", ccp."c_0102";
END;
$$;



CREATE OR REPLACE FUNCTION mrv.f_0081(
    p_0076 uuid
)
RETURNS TABLE (
    c_0364 uuid,
    c_0059 double precision,
    p_0004 uuid,
    c_0191 text,
    c_0188 double precision,
    f_0048 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_source_entity_type text;
    v_source_entity_id uuid;
    v_procurement_amount double precision;
    v_procurement_date date;
    v_crop_type text;
    v_procurement_year int;
    v_farmer_total_yield double precision;
    v_proportion double precision;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT p."c_0457", p."c_0456", p."c_0359",
           p."c_0360", p."c_0102"
    INTO v_source_entity_type, v_source_entity_id, v_procurement_amount,
         v_procurement_date, v_crop_type
    FROM mrv."t_0021" p
    WHERE p.id = p_0076
        AND (
            auth.f_0119(p_0105 => p."c_0401")
            OR EXISTS (
                SELECT 1
                FROM public.t_0050 es
                WHERE es.p_0122 = p."c_0401"
                    AND auth.f_0119(p_0026 => es.p_0001)
            )
        );

    IF NOT FOUND THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_source_entity_type <> '_' THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_procurement_amount IS NULL OR v_procurement_amount = 0 THEN
        RETURN;
    END IF;

    IF v_procurement_date IS NULL THEN
        RAISE EXCEPTION '_%_', p_0076;
    END IF;

    v_procurement_year := EXTRACT(YEAR FROM v_procurement_date)::int;

    SELECT COALESCE(SUM(hr."c_0512"), 0) INTO v_farmer_total_yield
    FROM mrv."t_0016" hr
    JOIN mrv."t_0006" c ON c.id = hr."c_0117"
    JOIN mrv."t_0012" f ON f.id = c."c_0187"
    WHERE f."c_0172" = v_source_entity_id
        AND c."c_0102"::text = v_crop_type
        AND c.c_0472 = 'v1'
        AND c."c_0222" >= make_date(v_procurement_year, 1, 1)
        AND c."c_0222" <= make_date(v_procurement_year, 12, 31);

    IF v_farmer_total_yield = 0 THEN
        RETURN;
    END IF;

    v_proportion := LEAST(v_procurement_amount / v_farmer_total_yield, 1.0);

    RETURN QUERY
    WITH c_0190 AS (
        SELECT f.id AS fid, f.name AS fname
        FROM mrv."t_0012" f
        WHERE f."c_0172" = v_source_entity_id
    ),
    carbon_data AS (
        SELECT bcd.p_0004, bcd.c_0060, bcd.c_0056
        FROM mrv.f_0016(
            ARRAY(SELECT fid FROM c_0190),
            v_procurement_year
        ) bcd
        WHERE bcd.c_0060 > 0
    )
    SELECT
        p_0076,
        SUM(v_proportion * cd.c_0060) OVER () AS c_0059,
        fi.fid AS p_0004,
        fi.fname AS c_0191,
        (v_proportion * cd.c_0060)::double precision AS c_0188,
        cd.c_0056 AS f_0048
    FROM c_0190 fi
    JOIN carbon_data cd ON cd.p_0004 = fi.fid;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0083(
    p_0035 uuid
)
RETURNS TABLE (
    id uuid,
    name text,
    c_0356 uuid
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_farm RECORD;
    v_polygon RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0035 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT f.id, f."name", f."c_0355", f."c_0172"
    INTO v_farm
    FROM mrv."t_0012" f
    WHERE f.id = p_0035;

    IF v_farm IS NULL THEN
        RAISE EXCEPTION '_%_', p_0035;
    END IF;

    IF NOT (
        auth.f_0119(p_0026 => v_farm."c_0172")
        OR auth.f_0119(p_0105 => v_farm."c_0172")
        OR auth.f_0119(p_0037 => v_farm."c_0172")
    ) THEN
        RAISE EXCEPTION '_%_', p_0035;
    END IF;

    DELETE FROM mrv."t_0012" WHERE mrv."t_0012".id = p_0035;

    SELECT p."c_0460"
    INTO v_polygon
    FROM mrv."Polygon" p
    WHERE p.id = v_farm."c_0355";

    
    
    
    IF FOUND AND v_polygon."c_0460" IS NULL THEN
        
        
        
        
        DELETE FROM mrv."Polygon" WHERE mrv."Polygon".id = v_farm."c_0355";
    END IF;

    RETURN QUERY SELECT
        v_farm.id,
        v_farm."name",
        v_farm."c_0355";
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0084(
    p_0027 uuid,
    p_0040 uuid
)
RETURNS boolean
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0026 => p_0027)
        OR auth.f_0119(p_0105 => p_0027)
        OR auth.f_0119(p_0037 => p_0027)
    ) THEN
        RAISE EXCEPTION '_%_', p_0027;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0014" fa
        JOIN mrv."t_0006" c ON c.id = fa."c_0117"
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE fa.id = p_0040 AND f."c_0172" = p_0027
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    DELETE FROM mrv."t_0014" WHERE id = p_0040;
    RETURN true;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0089(
    p_0050 uuid,
    p_0026 uuid,
    p_0080 float8,
    p_0107 date
)
RETURNS TABLE(id uuid, "c_0486" uuid, "c_0170" uuid, "c_0455" text, "c_0458" uuid, "c_0102" text, "c_0381" double precision, "c_0489" date, "c_0094" uuid, "c_0093" timestamptz, "c_0528" timestamptz)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_existing RECORD;
    v_farm RECORD;
    v_available float8 := 0;
    v_already_supplied float8;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    PERFORM pg_advisory_xact_lock(hashtext('_' || p_0050::text));

    SELECT id, "c_0486", "c_0170", "c_0455", "c_0458", "c_0102"
    INTO v_existing
    FROM mrv."t_0028"
    WHERE id = p_0050;

    IF v_existing IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0105 => v_existing."c_0486") THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0080 <= 0 OR p_0080 > 1000000 THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0021"
        WHERE "c_0401" = v_existing."c_0486"
          AND "c_0402" = 'v1'
          AND "c_0170" = p_0026
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_existing."c_0455"::text = '_' THEN
        SELECT id, "c_0172"
        INTO v_farm
        FROM mrv."t_0012"
        WHERE id = v_existing."c_0458"
        LIMIT 1;

        IF v_farm IS NULL OR v_farm."c_0172" != v_existing."c_0486" THEN
            RAISE EXCEPTION '_%_%_',
                v_existing."c_0458", v_existing."c_0486";
        END IF;

        SELECT COALESCE(SUM(hr."c_0512"), 0)
        INTO v_available
        FROM mrv."t_0016" hr
        JOIN mrv."t_0006" c ON c.id = hr."c_0117"
        WHERE c."c_0102"::text = v_existing."c_0102"::text
          AND c.c_0472 = 'v1'
          AND c."c_0187" = v_farm.id;

    ELSIF v_existing."c_0455"::text = '_' THEN
        SELECT COALESCE(SUM("c_0359"), 0)
        INTO v_available
        FROM mrv."t_0021"
        WHERE "c_0401" = v_existing."c_0486"
          AND "c_0402" = 'v1'
          AND "c_0456" = v_existing."c_0458"
          AND "c_0457" = 'v1'
          AND "c_0102"::text = v_existing."c_0102"::text;

    ELSIF v_existing."c_0455"::text = '_' THEN
        SELECT COALESCE(SUM(pr."c_0359"), 0)
        INTO v_available
        FROM mrv."t_0021" pr
        JOIN mrv."t_0022" pf ON pf."c_0361" = pr.id
        WHERE pr."c_0401" = v_existing."c_0486"
          AND pr."c_0402" = 'v1'
          AND pf."c_0183" = v_existing."c_0458"
          AND pr."c_0102"::text = v_existing."c_0102"::text;

    ELSE
        RAISE EXCEPTION '_%_', v_existing."c_0455"::text;
    END IF;

    SELECT COALESCE(SUM("c_0381"), 0)
    INTO v_already_supplied
    FROM mrv."t_0028"
    WHERE "c_0486" = v_existing."c_0486"
      AND "c_0455"::text = v_existing."c_0455"::text
      AND "c_0458" = v_existing."c_0458"
      AND "c_0102"::text = v_existing."c_0102"::text
      AND id != p_0050;

    IF p_0080 > (v_available - v_already_supplied) THEN
        RAISE EXCEPTION '_%_%_%_%_%_',
            p_0080, (v_available - v_already_supplied),
            v_existing."c_0455"::text, v_existing."c_0458", v_existing."c_0102"::text;
    END IF;

    BEGIN
        UPDATE mrv."t_0028"
        SET
            "c_0170" = p_0026,
            "c_0381" = p_0080,
            "c_0489" = p_0107,
            "c_0528" = NOW()
        WHERE id = p_0050;

        IF NOT FOUND THEN
            RAISE EXCEPTION '_%_', p_0050;
        END IF;
    EXCEPTION WHEN unique_violation THEN
        RAISE EXCEPTION '_';
    END;

    RETURN QUERY
        SELECT sv.id, sv."c_0486", sv."c_0170", sv."c_0455"::text, sv."c_0458",
               sv."c_0102"::text, sv."c_0381", sv."c_0489", sv."c_0094",
               sv."c_0093", sv."c_0528"
        FROM mrv."t_0028" sv WHERE sv.id = p_0050;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0090(
    p_0115 uuid
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_enterprise_id uuid;
    v_verifier_user_id uuid;
    v_updated_rows int;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT p_0001, c_0537
    INTO v_enterprise_id, v_verifier_user_id
    FROM public.t_0096
    WHERE id = p_0115;

    IF v_enterprise_id IS NULL THEN
        RAISE EXCEPTION '_%_', p_0115;
    END IF;

    IF NOT auth.f_0119(p_0026 => v_enterprise_id) THEN
        RAISE EXCEPTION '_';
    END IF;

    UPDATE public.t_0096
    SET c_0415 = now(), c_0416 = v_current_user_id
    WHERE id = p_0115
      AND c_0415 IS NULL;

    GET DIAGNOSTICS v_updated_rows = ROW_COUNT;
    IF v_updated_rows = 0 THEN
        RAISE EXCEPTION '_%_', p_0115;
    END IF;

    
    INSERT INTO audit.t_0097 (c_0537, p_0001, action, c_0141)
    VALUES (
        v_verifier_user_id,
        v_enterprise_id,
        '_',
        jsonb_build_object(
            '_', v_current_user_id,
            '_', p_0115
        )
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0091(
    p_0035 uuid
)
RETURNS TABLE (
    c_0425 uuid,
    c_0426 text
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_entity_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT f."c_0172" INTO v_entity_id
    FROM mrv."t_0012" f
    WHERE f.id = p_0035;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_entity_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0026 => v_entity_id)
        OR auth.f_0119(p_0105 => v_entity_id)
        OR auth.f_0119(p_0037 => v_entity_id)
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN QUERY
    SELECT sp.id, sp.name
    FROM mrv."t_0037" vpf
    JOIN mrv."t_0033" vp ON vp.id = vpf."c_0369"
    JOIN mrv.t_0084 sp ON sp.c_0535 = vp.id
    WHERE vpf."c_0187" = p_0035
    LIMIT 1;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0099(
    p_0105 uuid,
    p_0026 uuid,
    p_0097 text,
    p_0100 uuid,
    p_0016 text,
    p_0080 float8,
    p_0107 date
)
RETURNS TABLE(id uuid, "c_0486" uuid, "c_0170" uuid, "c_0455" text, "c_0458" uuid, "c_0102" text, "c_0381" double precision, "c_0489" date, "c_0094" uuid, "c_0093" timestamptz, "c_0528" timestamptz)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_farm RECORD;
    v_available float8 := 0;
    v_already_supplied float8;
    v_new_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0105 => p_0105) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    
    PERFORM pg_advisory_xact_lock(
        hashtext(p_0105::text || p_0097 || p_0100::text || p_0016)
    );

    BEGIN
        PERFORM p_0097::mrv."ty_0018";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0097;
    END;

    BEGIN
        PERFORM p_0016::mrv."ty_0004";
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_%_', p_0016;
    END;

    IF p_0080 <= 0 OR p_0080 > 1000000 THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0021"
        WHERE "c_0401" = p_0105
          AND "c_0402" = 'v1'
          AND "c_0170" = p_0026
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0097 = '_' THEN
        SELECT id, "c_0172"
        INTO v_farm
        FROM mrv."t_0012"
        WHERE id = p_0100
        LIMIT 1;

        IF v_farm IS NULL OR v_farm."c_0172" != p_0105 THEN
            RAISE EXCEPTION '_%_%_',
                p_0100, p_0105;
        END IF;

        SELECT COALESCE(SUM(hr."c_0512"), 0)
        INTO v_available
        FROM mrv."t_0016" hr
        JOIN mrv."t_0006" c ON c.id = hr."c_0117"
        WHERE c."c_0102"::text = p_0016
          AND c.c_0472 = 'v1'
          AND c."c_0187" = v_farm.id;

    ELSIF p_0097 = '_' THEN
        SELECT COALESCE(SUM("c_0359"), 0)
        INTO v_available
        FROM mrv."t_0021"
        WHERE "c_0401" = p_0105
          AND "c_0402" = 'v1'
          AND "c_0456" = p_0100
          AND "c_0457" = 'v1'
          AND "c_0102"::text = p_0016;

    ELSIF p_0097 = '_' THEN
        SELECT COALESCE(SUM(pr."c_0359"), 0)
        INTO v_available
        FROM mrv."t_0021" pr
        JOIN mrv."t_0022" pf ON pf."c_0361" = pr.id
        WHERE pr."c_0401" = p_0105
          AND pr."c_0402" = 'v1'
          AND pf."c_0183" = p_0100
          AND pr."c_0102"::text = p_0016;

    ELSE
        RAISE EXCEPTION '_%_', p_0097;
    END IF;

    SELECT COALESCE(SUM("c_0381"), 0)
    INTO v_already_supplied
    FROM mrv."t_0028"
    WHERE "c_0486" = p_0105
      AND "c_0455"::text = p_0097
      AND "c_0458" = p_0100
      AND "c_0102"::text = p_0016;

    IF p_0080 > (v_available - v_already_supplied) THEN
        RAISE EXCEPTION '_%_%_%_%_%_',
            p_0080, (v_available - v_already_supplied),
            p_0097, p_0100, p_0016;
    END IF;

    v_new_id := gen_random_uuid();

    BEGIN
        INSERT INTO mrv."t_0028" (
            id,
            "c_0486",
            "c_0170",
            "c_0455",
            "c_0458",
            "c_0102",
            "c_0381",
            "c_0489",
            "c_0094",
            "c_0093",
            "c_0528"
        ) VALUES (
            v_new_id,
            p_0105,
            p_0026,
            p_0097::mrv."ty_0018",
            p_0100,
            p_0016::mrv."ty_0004",
            p_0080,
            p_0107,
            v_current_user_id,
            NOW(),
            NOW()
        );
    EXCEPTION WHEN unique_violation THEN
        RAISE EXCEPTION '_';
    END;

    RETURN QUERY
        SELECT sv.id, sv."c_0486", sv."c_0170", sv."c_0455"::text, sv."c_0458",
               sv."c_0102"::text, sv."c_0381", sv."c_0489", sv."c_0094",
               sv."c_0093", sv."c_0528"
        FROM mrv."t_0028" sv WHERE sv.id = v_new_id;
END;
$$;



CREATE OR REPLACE FUNCTION mrv.f_0100(
    p_0105 uuid,
    p_0118 int,
    p_0026 uuid DEFAULT NULL
)
RETURNS TABLE (
    c_0518 double precision,
    c_0431 double precision,
    c_0434 double precision,
    c_0365 double precision,
    c_0105 text,
    c_0103 double precision
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0105 => p_0105)
        OR EXISTS (
            SELECT 1
            FROM public.t_0050 es
            WHERE es.p_0122 = p_0105
              AND auth.f_0119(p_0026 => es.p_0001)
        )
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0026 IS NOT NULL AND NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_%_', p_0026;
    END IF;

    IF p_0118 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 < 2000 OR p_0118 > EXTRACT(YEAR FROM NOW())::int + 5 THEN
        RAISE EXCEPTION '_%_', p_0118;
    END IF;

    RETURN QUERY
    WITH scope1_farms AS (
        SELECT f.id AS p_0004
        FROM mrv."t_0012" f
        WHERE f."c_0172" = p_0105
            AND f."ty_0016" = 'v1'
    ),
    scope3_farms AS (
        SELECT f.id AS p_0004
        FROM mrv."t_0012" f
        WHERE f."c_0172" = p_0105
            AND f."ty_0016" = 'v1'
    ),
    all_supplier_farm_ids AS (
        SELECT p_0004 FROM scope1_farms
        UNION ALL
        SELECT p_0004 FROM scope3_farms
    ),
    
    supplier_farm_carbon AS (
        SELECT bcd.p_0004, bcd.c_0060
        FROM mrv.f_0016(
            ARRAY(SELECT p_0004 FROM all_supplier_farm_ids),
            p_0118
        ) bcd
    ),
    
    supplier_farm_cultivations AS (
        SELECT c."c_0187" AS p_0004, c."c_0102"::text AS c_0105
        FROM mrv."t_0006" c
        WHERE c."c_0187" IN (SELECT p_0004 FROM all_supplier_farm_ids)
            AND c.c_0472 = 'v1'
            AND c."c_0222" >= make_date(p_0118, 1, 1)
            AND c."c_0222" <= make_date(p_0118, 12, 31)
    ),
    
    scope1_by_crop AS (
        SELECT
            sfc.c_0105,
            SUM(
                COALESCE(sfc2.c_0060, 0) / NULLIF(cnt.cultivation_count, 0)
            ) AS c_0059
        FROM supplier_farm_cultivations sfc
        JOIN scope1_farms sf ON sf.p_0004 = sfc.p_0004
        JOIN supplier_farm_carbon sfc2 ON sfc2.p_0004 = sfc.p_0004
        JOIN (
            SELECT p_0004, COUNT(*) AS cultivation_count
            FROM supplier_farm_cultivations
            GROUP BY p_0004
        ) cnt ON cnt.p_0004 = sfc.p_0004
        GROUP BY sfc.c_0105
    ),
    
    scope3_by_crop AS (
        SELECT
            sfc.c_0105,
            SUM(
                COALESCE(sfc2.c_0060, 0) / NULLIF(cnt.cultivation_count, 0)
            ) AS c_0059
        FROM supplier_farm_cultivations sfc
        JOIN scope3_farms sf ON sf.p_0004 = sfc.p_0004
        JOIN supplier_farm_carbon sfc2 ON sfc2.p_0004 = sfc.p_0004
        JOIN (
            SELECT p_0004, COUNT(*) AS cultivation_count
            FROM supplier_farm_cultivations
            GROUP BY p_0004
        ) cnt ON cnt.p_0004 = sfc.p_0004
        GROUP BY sfc.c_0105
    ),
    
    scope1_total AS (
        SELECT COALESCE(SUM(c_0060), 0) AS total
        FROM supplier_farm_carbon sfc
        WHERE sfc.p_0004 IN (SELECT p_0004 FROM scope1_farms)
    ),
    scope3_total AS (
        SELECT COALESCE(SUM(c_0060), 0) AS total
        FROM supplier_farm_carbon sfc
        WHERE sfc.p_0004 IN (SELECT p_0004 FROM scope3_farms)
    ),
    
    procurements AS (
        SELECT
            p.id,
            p."c_0102"::text AS c_0105,
            p."c_0456",
            p."c_0359"
        FROM mrv."t_0021" p
        WHERE p."c_0401" = p_0105
            AND p."c_0402" = 'v1'
            AND p."c_0457" = 'v1'
            AND p."c_0360" >= make_date(p_0118, 1, 1)
            AND p."c_0360" <= make_date(p_0118, 12, 31)
            AND p."c_0359" IS NOT NULL
            AND p."c_0359" > 0
            AND (p_0026 IS NULL OR p."c_0170" = p_0026)
    ),
    
    farmer_farms AS (
        SELECT f.id AS p_0004, f."c_0172" AS con_0122
        FROM mrv."t_0012" f
        WHERE f."c_0172" IN (SELECT DISTINCT "c_0456" FROM procurements)
    ),
    farmer_farm_carbon AS (
        SELECT bcd.p_0004, bcd.c_0060
        FROM mrv.f_0016(
            ARRAY(SELECT p_0004 FROM farmer_farms),
            p_0118
        ) bcd
    ),
    
    farmer_yields AS (
        SELECT
            f."c_0172",
            c."c_0102"::text AS c_0105,
            SUM(COALESCE(hr."c_0512", 0)) AS c_0521
        FROM mrv."t_0016" hr
        JOIN mrv."t_0006" c ON c.id = hr."c_0117"
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE f."c_0172" IN (SELECT DISTINCT "c_0456" FROM procurements)
            AND c.c_0472 = 'v1'
            AND c."c_0222" >= make_date(p_0118, 1, 1)
            AND c."c_0222" <= make_date(p_0118, 12, 31)
        GROUP BY f."c_0172", c."c_0102"::text
    ),
    
    procurement_carbon AS (
        SELECT
            proc.c_0105,
            SUM(
                CASE WHEN fy.c_0521 > 0 THEN
                    LEAST(proc."c_0359" / fy.c_0521, 1.0) *
                    COALESCE((
                        SELECT SUM(ffc.c_0060)
                        FROM farmer_farm_carbon ffc
                        WHERE ffc.p_0004 IN (
                            SELECT p_0004 FROM farmer_farms WHERE con_0122 = proc."c_0456"
                        )
                    ), 0)
                ELSE 0
                END
            ) AS c_0059
        FROM procurements proc
        LEFT JOIN farmer_yields fy
            ON fy."c_0172" = proc."c_0456"
            AND fy.c_0105 = proc.c_0105
        GROUP BY proc.c_0105
    ),
    
    all_by_crop AS (
        SELECT c_0105, c_0059 FROM scope1_by_crop
        UNION ALL
        SELECT c_0105, c_0059 FROM scope3_by_crop
        UNION ALL
        SELECT c_0105, c_0059 FROM procurement_carbon
    ),
    merged_by_crop AS (
        SELECT c_0105, SUM(c_0059) AS c_0059
        FROM all_by_crop
        GROUP BY c_0105
    )
    SELECT
        (SELECT total FROM scope1_total) + (SELECT total FROM scope3_total) +
            COALESCE((SELECT SUM(c_0059) FROM procurement_carbon), 0) AS c_0518,
        (SELECT total FROM scope1_total) AS c_0431,
        (SELECT total FROM scope3_total) AS c_0434,
        COALESCE((SELECT SUM(c_0059) FROM procurement_carbon), 0) AS c_0365,
        mbc.c_0105,
        mbc.c_0059 AS c_0103
    FROM merged_by_crop mbc
    UNION ALL
    
    SELECT
        (SELECT total FROM scope1_total) + (SELECT total FROM scope3_total) +
            COALESCE((SELECT SUM(c_0059) FROM procurement_carbon), 0),
        (SELECT total FROM scope1_total),
        (SELECT total FROM scope3_total),
        COALESCE((SELECT SUM(c_0059) FROM procurement_carbon), 0),
        NULL,
        NULL
    WHERE NOT EXISTS (SELECT 1 FROM merged_by_crop);
END;
$$;




CREATE OR REPLACE FUNCTION mrv.f_0101()
RETURNS TRIGGER AS $$
DECLARE
  existing_farm_id UUID;
  existing_polygon_id UUID;
BEGIN
  
  IF pg_trigger_depth() > 1 THEN
    RETURN NEW;
  END IF;

  
  IF TG_TABLE_NAME = 'v1' THEN
    IF NEW."c_0355" IS NOT NULL AND (TG_OP = '_' OR NEW."c_0355" IS DISTINCT FROM OLD."c_0355") THEN
      SELECT "c_0187" INTO existing_farm_id
      FROM mrv."Polygon"
      WHERE id = NEW."c_0355";

      IF existing_farm_id IS NOT NULL AND existing_farm_id <> NEW.id THEN
        RAISE EXCEPTION '_%_%_', NEW."c_0355", existing_farm_id;
      END IF;

      IF TG_OP = '_' AND OLD."c_0355" IS NOT NULL AND OLD."c_0355" <> NEW."c_0355" THEN
        UPDATE mrv."Polygon" SET "c_0187" = NULL, "c_0528" = NOW() WHERE id = OLD."c_0355";
      END IF;

      UPDATE mrv."Polygon"
      SET "c_0187" = NEW.id, "c_0528" = NOW()
      WHERE id = NEW."c_0355";
    END IF;

  ELSIF TG_TABLE_NAME = 'v1' THEN
    IF NEW."c_0187" IS NOT NULL AND (TG_OP = '_' OR NEW."c_0187" IS DISTINCT FROM OLD."c_0187") THEN
      SELECT "c_0355" INTO existing_polygon_id
      FROM mrv."t_0012"
      WHERE id = NEW."c_0187";

      IF existing_polygon_id IS NOT NULL AND existing_polygon_id <> NEW.id THEN
        RAISE EXCEPTION '_%_%_', NEW."c_0187", existing_polygon_id;
      END IF;

      IF TG_OP = '_' AND OLD."c_0187" IS NOT NULL AND OLD."c_0187" <> NEW."c_0187" THEN
        RAISE EXCEPTION '_%_%_', NEW.id, OLD."c_0187";
      END IF;

      UPDATE mrv."t_0012"
      SET "c_0355" = NEW.id, "c_0528" = NOW()
      WHERE id = NEW."c_0187";
    END IF;
  END IF;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE OR REPLACE FUNCTION mrv.f_0103(
    p_0073 uuid,
    p_0087 jsonb
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_supplier_id uuid;
    v_updated_rows int;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (auth.f_0066() -> '_' -> '_' ->> 0)::uuid;
    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    UPDATE mrv."t_0001"
    SET
        c_0413 = p_0087,
        "c_0528" = NOW()
    WHERE mrv."t_0001".id = p_0073
      AND mrv."t_0001"."c_0172" = v_supplier_id;

    GET DIAGNOSTICS v_updated_rows = ROW_COUNT;
    IF v_updated_rows = 0 THEN
        RAISE EXCEPTION '_%_', p_0073;
    END IF;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0104(
    p_0073 uuid,
    p_0102 text,
    p_0064 uuid,
    p_0030 text
)
RETURNS TABLE (
    id uuid,
    con_0122 uuid,
    c_0294 text,
    c_0329 timestamptz,
    c_0273 uuid,
    c_0472 text,
    c_0219 text,
    c_0497 int,
    c_0498 double precision,
    c_0127 double precision,
    c_0037 boolean[],
    c_0264 boolean,
    c_0124 boolean[],
    c_0266 boolean,
    c_0126 double precision,
    c_0265 boolean,
    c_0125 double precision,
    c_0128 text,
    c_0267 boolean,
    c_0210 boolean[],
    c_0269 boolean,
    c_0212 double precision,
    c_0268 boolean,
    c_0211 double precision,
    c_0213 text,
    c_0176 text,
    c_0413 jsonb,
    c_0190 uuid[],
    c_0095 timestamptz,
    c_0529 timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_supplier_id uuid;
    v_updated_rows int;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_supplier_id := (auth.f_0066() -> '_' -> '_' ->> 0)::uuid;
    IF v_supplier_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    UPDATE mrv."t_0001"
    SET
        c_0472 = p_0102::mrv."ty_0002",
        "c_0272" = COALESCE(p_0064, "c_0272"),
        "c_0175" = p_0030,
        "c_0528" = NOW()
    WHERE mrv."t_0001".id = p_0073
      AND mrv."t_0001"."c_0172" = v_supplier_id;

    GET DIAGNOSTICS v_updated_rows = ROW_COUNT;
    IF v_updated_rows = 0 THEN
        RAISE EXCEPTION '_%_', p_0073;
    END IF;

    RETURN QUERY
    SELECT
        cp.id,
        cp."c_0172",
        cp.c_0294,
        cp."c_0328",
        cp."c_0272",
        cp.c_0472::text,
        cp."c_0218"::text,
        cp."c_0494",
        cp."c_0495",
        cp."c_0121",
        cp."c_0036",
        cp."c_0251",
        cp."c_0118",
        cp."c_0253",
        cp."c_0120",
        cp."c_0252",
        cp."c_0119",
        cp."c_0122"::text,
        cp."c_0254",
        cp."c_0206",
        cp."c_0256",
        cp."c_0208",
        cp."c_0255",
        cp."c_0207",
        cp."c_0209"::text,
        cp."c_0175",
        cp.c_0413,
        ARRAY(SELECT cpf."c_0187" FROM mrv."t_0002" cpf WHERE cpf."c_0058" = cp.id AND cpf."c_0187" IS NOT NULL),
        cp."c_0093",
        cp."c_0528"
    FROM mrv."t_0001" cp
    WHERE cp.id = p_0073;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0106(p_0023 jsonb)
RETURNS TABLE (
    id uuid,
    "c_0187" uuid,
    "c_0057" int,
    "c_0226" boolean,
    "c_0225" boolean,
    "c_0292" float8,
    "c_0483" timestamptz,
    "c_0093" timestamptz,
    "c_0528" timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_entity_id uuid;
    v_farm_id uuid;
    v_calendar_year int;
    v_has_trees boolean;
    v_has_tree_mortality boolean;
    v_live_tree_area_ha float8;
    v_tree_species_inventories jsonb;
    v_survey_id uuid;
    v_item jsonb;
    v_result RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_entity_id := (p_0023->>'_')::uuid;

    IF v_entity_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_farm_id := (p_0023->>'_')::uuid;
    v_calendar_year := (p_0023->>'_')::int;
    v_has_trees := (p_0023->>'_')::boolean;
    v_has_tree_mortality := (p_0023->>'_')::boolean;
    v_live_tree_area_ha := (p_0023->>'_')::float8;
    v_tree_species_inventories := p_0023->'_';

    
    
    IF NOT (
        auth.f_0119(p_0026 => v_entity_id)
        OR auth.f_0119(p_0105 => v_entity_id)
        OR auth.f_0119(p_0037 => v_entity_id)
    ) THEN
        RAISE EXCEPTION '_%_', v_entity_id;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0012"
        WHERE id = v_farm_id AND "c_0172" = v_entity_id
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT id INTO v_survey_id
    FROM mrv."t_0008"
    WHERE "c_0187" = v_farm_id AND "c_0057" = v_calendar_year;

    IF v_survey_id IS NOT NULL THEN
        UPDATE mrv."t_0008"
        SET "c_0226" = v_has_trees,
            "c_0225" = v_has_tree_mortality,
            "c_0292" = v_live_tree_area_ha,
            "c_0483" = NOW(),
            "c_0528" = NOW()
        WHERE mrv."t_0008".id = v_survey_id
        RETURNING * INTO v_result;
    ELSE
        INSERT INTO mrv."t_0008" (
            id, "c_0187", "c_0057",
            "c_0226", "c_0225", "c_0292",
            "c_0483", "c_0093", "c_0528"
        ) VALUES (
            gen_random_uuid(),
            v_farm_id,
            v_calendar_year,
            v_has_trees,
            v_has_tree_mortality,
            v_live_tree_area_ha,
            NOW(), NOW(), NOW()
        )
        RETURNING * INTO v_result;

        v_survey_id := v_result.id;
    END IF;

    IF v_tree_species_inventories IS NOT NULL
        AND jsonb_typeof(v_tree_species_inventories) = '_'
        AND jsonb_array_length(v_tree_species_inventories) > 0
    THEN
        DELETE FROM mrv."t_0030"
        WHERE "c_0490" = v_survey_id;

        FOR v_item IN SELECT * FROM jsonb_array_elements(v_tree_species_inventories)
        LOOP
            INSERT INTO mrv."t_0030" (
                id, "c_0187", "c_0057", "c_0490",
                "c_0468", "c_0030", "c_0031",
                "c_0293", "c_0133",
                "c_0522", "c_0134",
                "c_0093", "c_0528"
            ) VALUES (
                gen_random_uuid(),
                v_farm_id,
                v_calendar_year,
                v_survey_id,
                (v_item->>'_')::mrv."ty_0020",
                (v_item->>'_')::float8,
                (v_item->>'_')::float8,
                (v_item->>'_')::int,
                (v_item->>'_')::int,
                (v_item->>'_')::float8,
                (v_item->>'_')::float8,
                NOW(), NOW()
            );
        END LOOP;
    END IF;

    
    IF v_has_trees AND v_tree_species_inventories IS NOT NULL AND jsonb_array_length(v_tree_species_inventories) = 0 THEN
        DELETE FROM mrv."t_0030"
        WHERE "c_0187" = v_farm_id
        AND "c_0057" = v_calendar_year;
    END IF;

    
    IF NOT v_has_trees THEN
        DELETE FROM mrv."t_0030"
        WHERE "c_0187" = v_farm_id
        AND "c_0057" = v_calendar_year;
    END IF;

    RETURN QUERY SELECT
        v_result.id,
        v_result."c_0187"::uuid,
        v_result."c_0057"::int,
        v_result."c_0226"::boolean,
        v_result."c_0225"::boolean,
        v_result."c_0292"::float8,
        v_result."c_0483"::timestamptz,
        v_result."c_0093"::timestamptz,
        v_result."c_0528"::timestamptz;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0107(
    p_0039 jsonb
)
RETURNS TABLE (
    id uuid,
    name text,
    c_0356 uuid,
    c_0279 text,
    c_0104 uuid,
    c_0107 text[]
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_item jsonb;
    v_farm_id uuid;
    v_polygon_id uuid;
    v_entity_id uuid;
    v_name text;
    v_country_code text;
    v_scope text;
    v_land_ownership text;
    v_geometry jsonb;
    v_new_geometry geometry;
    v_existing_polygon RECORD;
    v_overlapping_farm RECORD;
    v_farm RECORD;
    v_crop_plan_id uuid;
    v_crop_histories jsonb;
    v_crop_item jsonb;
    v_crop_records jsonb;
    v_crop_type text;
    v_crop_types text[];
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0039 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF jsonb_typeof(p_0039) != '_' THEN
        RAISE EXCEPTION '_%_', jsonb_typeof(p_0039);
    END IF;

    FOR v_item IN SELECT * FROM jsonb_array_elements(p_0039)
    LOOP
        v_name := v_item->>'_';
        v_country_code := v_item->>'_';
        v_scope := v_item->>'_';
        v_land_ownership := v_item->>'_';
        v_geometry := v_item->'_'->'_';
        v_crop_histories := v_item->'_';

        v_entity_id := (v_item->>'_')::uuid;
        IF NOT (
            auth.f_0119(p_0026 => v_entity_id)
            OR auth.f_0119(p_0105 => v_entity_id)
            OR auth.f_0119(p_0037 => v_entity_id)
        ) THEN
            RAISE EXCEPTION '_%_', v_entity_id;
        END IF;

        IF v_name IS NULL OR v_name = 'v1' THEN
            RAISE EXCEPTION '_';
        END IF;

        
        BEGIN
            v_farm_id := (v_item->>'_')::uuid;
        EXCEPTION WHEN invalid_text_representation THEN
            v_farm_id := NULL;
        END;

        IF v_farm_id IS NULL THEN
            
            SELECT f.id INTO v_farm_id
            FROM mrv."t_0012" f
            WHERE f."c_0172" = v_entity_id AND f."name" = v_name;
        END IF;

        IF v_farm_id IS NULL THEN
            v_farm_id := gen_random_uuid();
        END IF;

        
        BEGIN
            v_polygon_id := COALESCE(
                (v_item->>'_')::uuid,
                (v_item->'_'->>'_')::uuid
            );
        EXCEPTION WHEN invalid_text_representation THEN
            v_polygon_id := NULL;
        END;

        IF v_polygon_id IS NULL THEN
            v_polygon_id := gen_random_uuid();
        END IF;

        
        IF v_geometry IS NOT NULL AND v_geometry::text != '_' THEN
            v_new_geometry := ST_SetSRID(ST_GeomFromGeoJSON(v_geometry::text), 4326);

            SELECT f.id, f."name" INTO v_overlapping_farm
            FROM mrv."t_0012" f
            JOIN mrv."Polygon" p ON p.id = f."c_0355"
            WHERE f."c_0172" = v_entity_id
              AND f.id != v_farm_id
              AND ST_Intersects(p.geometry::geography, v_new_geometry::geography)
              AND NOT ST_Touches(p.geometry, v_new_geometry)
            LIMIT 1;

            IF FOUND THEN
                RAISE EXCEPTION '_%_%_', v_overlapping_farm."name", v_overlapping_farm.id;
            END IF;

            INSERT INTO mrv."Polygon" (id, geometry, "c_0093", "c_0528")
            VALUES (
                v_polygon_id,
                v_new_geometry,
                NOW(),
                NOW()
            )
            ON CONFLICT (id) DO UPDATE SET
                geometry = v_new_geometry,
                "c_0528" = NOW();
        ELSE
            SELECT p.id, p."c_0187"
            INTO v_existing_polygon
            FROM mrv."Polygon" p
            WHERE p.id = v_polygon_id;

            IF v_existing_polygon IS NULL THEN
                RAISE EXCEPTION '_%_', v_polygon_id;
            END IF;

            IF v_existing_polygon."c_0187" IS NOT NULL AND v_existing_polygon."c_0187" != v_farm_id THEN
                RAISE EXCEPTION '_%_', v_polygon_id;
            END IF;
        END IF;

        
        IF v_country_code IS NOT NULL THEN
            BEGIN
                PERFORM v_country_code::mrv."ty_0003";
            EXCEPTION WHEN invalid_text_representation THEN
                RAISE EXCEPTION '_%_', v_country_code;
            END;
        END IF;

        IF v_scope IS NOT NULL THEN
            BEGIN
                PERFORM v_scope::mrv."ty_0016";
            EXCEPTION WHEN invalid_text_representation THEN
                RAISE EXCEPTION '_%_', v_scope;
            END;
        END IF;

        IF v_land_ownership IS NOT NULL THEN
            BEGIN
                PERFORM v_land_ownership::mrv."ty_0013";
            EXCEPTION WHEN invalid_text_representation THEN
                RAISE EXCEPTION '_%_', v_land_ownership;
            END;
        END IF;

        
        INSERT INTO mrv."t_0012" (
            id, "name", "c_0355", "c_0172",
            "ty_0003", "ty_0016", "ty_0013",
            "c_0093", "c_0528"
        ) VALUES (
            v_farm_id,
            v_name,
            v_polygon_id,
            v_entity_id,
            v_country_code::mrv."ty_0003",
            v_scope::mrv."ty_0016",
            v_land_ownership::mrv."ty_0013",
            NOW(),
            NOW()
        )
        ON CONFLICT (id) DO UPDATE SET
            "name" = EXCLUDED."name",
            "c_0355" = EXCLUDED."c_0355",
            "ty_0003" = COALESCE(EXCLUDED."ty_0003", mrv."t_0012"."ty_0003"),
            "ty_0016" = COALESCE(EXCLUDED."ty_0016", mrv."t_0012"."ty_0016"),
            "ty_0013" = COALESCE(EXCLUDED."ty_0013", mrv."t_0012"."ty_0013"),
            "c_0528" = NOW()
        RETURNING * INTO v_farm;

        
        v_crop_plan_id := NULL;
        v_crop_types := ARRAY[]::text[];

        IF v_crop_histories IS NOT NULL AND jsonb_array_length(v_crop_histories) > 0 THEN
            v_crop_item := v_crop_histories->0;
            v_crop_records := v_crop_item->'_';

            
            SELECT cp.id INTO v_crop_plan_id
            FROM mrv."t_0005" cp
            WHERE cp."c_0187" = v_farm.id
            LIMIT 1;

            IF v_crop_plan_id IS NULL THEN
                v_crop_plan_id := gen_random_uuid();
                INSERT INTO mrv."t_0005" (id, "c_0187", "c_0470")
                VALUES (v_crop_plan_id, v_farm.id, NOW());
            END IF;

            
            DELETE FROM mrv."t_0004" WHERE "c_0101" = v_crop_plan_id;

            
            IF v_crop_records IS NOT NULL AND jsonb_array_length(v_crop_records) > 0 THEN
                FOR v_crop_item IN SELECT * FROM jsonb_array_elements(v_crop_records)
                LOOP
                    v_crop_type := v_crop_item->>'_';

                    IF v_crop_type IS NOT NULL THEN
                        BEGIN
                            PERFORM v_crop_type::mrv."ty_0004";
                        EXCEPTION WHEN invalid_text_representation THEN
                            RAISE EXCEPTION '_%_', v_crop_type;
                        END;

                        INSERT INTO mrv."t_0004" (id, "c_0102", "c_0101", "c_0351")
                        VALUES (gen_random_uuid(), v_crop_type::mrv."ty_0004", v_crop_plan_id, NOW());

                        v_crop_types := array_append(v_crop_types, v_crop_type);
                    END IF;
                END LOOP;
            END IF;
        END IF;

        RETURN QUERY SELECT
            v_farm.id,
            v_farm."name",
            v_farm."c_0355",
            v_farm."ty_0013"::text,
            v_crop_plan_id,
            v_crop_types;
    END LOOP;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0108(
    p_0027 uuid,
    p_0017 uuid,
    p_0041 jsonb
)
RETURNS TABLE (
    id uuid,
    "c_0198" text,
    c_0010 float8,
    "c_0017" timestamptz,
    "c_0079" uuid,
    "c_0117" uuid,
    "c_0093" timestamptz,
    "c_0528" timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_current_user_id uuid;
    v_cultivation RECORD;
    v_item jsonb;
    v_fertilizer_id uuid;
    v_fertilizer_type text;
    v_amount float8;
    v_application_date timestamptz;
    v_compound_unit_id uuid;
    v_result RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT (
        auth.f_0119(p_0026 => p_0027)
        OR auth.f_0119(p_0105 => p_0027)
        OR auth.f_0119(p_0037 => p_0027)
    ) THEN
        RAISE EXCEPTION '_%_', p_0027;
    END IF;

    IF p_0041 IS NULL OR jsonb_typeof(p_0041) != '_' THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT c.id, c."c_0187"
    INTO v_cultivation
    FROM mrv."t_0006" c
    JOIN mrv."t_0012" f ON f.id = c."c_0187"
    WHERE c.id = p_0017 AND f."c_0172" = p_0027;

    IF v_cultivation IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF EXISTS (
        SELECT 1 FROM jsonb_array_elements(p_0041) AS fert
        WHERE NOT EXISTS (
            SELECT 1 FROM mrv."t_0003" cu
            WHERE cu.id = (fert->>'_')::uuid
        )
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    FOR v_item IN SELECT * FROM jsonb_array_elements(p_0041)
    LOOP
        v_fertilizer_id := (v_item->>'_')::uuid;
        v_fertilizer_type := v_item->>'_';
        v_amount := (v_item->>'_')::float8;
        v_application_date := (v_item->>'_')::timestamptz;
        v_compound_unit_id := (v_item->>'_')::uuid;

        BEGIN
            PERFORM v_fertilizer_type::mrv."ty_0010";
        EXCEPTION WHEN invalid_text_representation THEN
            RAISE EXCEPTION '_%_', v_fertilizer_type;
        END;

        IF v_fertilizer_id IS NOT NULL THEN
            IF NOT EXISTS (
                SELECT 1 FROM mrv."t_0014" fa
                JOIN mrv."t_0006" c ON c.id = fa."c_0117"
                JOIN mrv."t_0012" f ON f.id = c."c_0187"
                WHERE fa.id = v_fertilizer_id AND f."c_0172" = p_0027
            ) THEN
                RAISE EXCEPTION '_%_', v_fertilizer_id;
            END IF;

            UPDATE mrv."t_0014"
            SET "c_0198" = v_fertilizer_type::mrv."ty_0010",
                c_0010 = v_amount,
                "c_0017" = v_application_date,
                "c_0079" = v_compound_unit_id,
                "c_0117" = p_0017,
                "c_0528" = NOW()
            WHERE mrv."t_0014".id = v_fertilizer_id
            RETURNING * INTO v_result;
        ELSE
            INSERT INTO mrv."t_0014" (
                id, "c_0198", c_0010, "c_0017", "c_0079", "c_0117",
                "c_0093", "c_0528"
            ) VALUES (
                gen_random_uuid(), v_fertilizer_type::mrv."ty_0010", v_amount, v_application_date,
                v_compound_unit_id, p_0017, NOW(), NOW()
            )
            RETURNING * INTO v_result;
        END IF;

        RETURN QUERY SELECT
            v_result.id,
            v_result."c_0198"::text,
            v_result.c_0010::float8,
            v_result."c_0017"::timestamptz,
            v_result."c_0079"::uuid,
            v_result."c_0117"::uuid,
            v_result."c_0093"::timestamptz,
            v_result."c_0528"::timestamptz;
    END LOOP;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0109(p_0023 jsonb)
RETURNS TABLE (
    id uuid,
    "c_0512" float8,
    "c_0222" timestamptz,
    "c_0405" int,
    "c_0406" int,
    "c_0408" int,
    "c_0407" int,
    "c_0117" uuid,
    "c_0452" uuid,
    "c_0093" timestamptz,
    "c_0528" timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_entity_id uuid;
    v_cultivation_id uuid;
    v_total_yield float8;
    v_harvest_date timestamptz;
    v_residue_burned int;
    v_residue_incorporated int;
    v_residue_removed int;
    v_residue_other int;
    v_soil_report_id uuid;
    v_farm_id uuid;
    v_result RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_entity_id := (p_0023->>'_')::uuid;

    
    
    IF NOT (
        auth.f_0119(p_0026 => v_entity_id)
        OR auth.f_0119(p_0105 => v_entity_id)
        OR auth.f_0119(p_0037 => v_entity_id)
    ) THEN
        RAISE EXCEPTION '_%_', v_entity_id;
    END IF;

    v_cultivation_id := (p_0023->>'_')::uuid;
    v_total_yield := (p_0023->>'_')::float8;
    v_harvest_date := (p_0023->>'_')::timestamptz;
    v_residue_burned := (p_0023->>'_')::int;
    v_residue_incorporated := (p_0023->>'_')::int;
    v_residue_removed := (p_0023->>'_')::int;
    v_residue_other := (p_0023->>'_')::int;
    v_soil_report_id := (p_0023->>'_')::uuid;

    SELECT c."c_0187" INTO v_farm_id
    FROM mrv."t_0006" c
    JOIN mrv."t_0012" f ON f.id = c."c_0187"
    WHERE c.id = v_cultivation_id AND f."c_0172" = v_entity_id;

    IF v_farm_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_soil_report_id IS NOT NULL THEN
        IF NOT EXISTS (
            SELECT 1 FROM mrv."t_0027"
            WHERE id = v_soil_report_id
            AND "c_0187" = v_farm_id
        ) THEN
            RAISE EXCEPTION '_';
        END IF;
    END IF;

    IF v_residue_burned + v_residue_incorporated + v_residue_removed + v_residue_other != 100 THEN
        RAISE EXCEPTION '_';
    END IF;

    UPDATE mrv."t_0006"
    SET c_0472 = 'v1',
        "c_0222" = v_harvest_date,
        "c_0528" = NOW()
    WHERE mrv."t_0006".id = v_cultivation_id;

    INSERT INTO mrv."t_0016" (
        id, "c_0512", "c_0222", "c_0405", "c_0406",
        "c_0408", "c_0407", "c_0117", "c_0452",
        "c_0093", "c_0528"
    ) VALUES (
        gen_random_uuid(), v_total_yield, v_harvest_date, v_residue_burned, v_residue_incorporated,
        v_residue_removed, v_residue_other, v_cultivation_id, v_soil_report_id,
        NOW(), NOW()
    )
    ON CONFLICT ("c_0117")
    DO UPDATE SET
        "c_0512" = EXCLUDED."c_0512",
        "c_0222" = EXCLUDED."c_0222",
        "c_0405" = EXCLUDED."c_0405",
        "c_0406" = EXCLUDED."c_0406",
        "c_0408" = EXCLUDED."c_0408",
        "c_0407" = EXCLUDED."c_0407",
        "c_0452" = COALESCE(EXCLUDED."c_0452", mrv."t_0016"."c_0452"),
        "c_0528" = NOW()
    RETURNING * INTO v_result;

    RETURN QUERY SELECT
        v_result.id,
        v_result."c_0512"::float8,
        v_result."c_0222"::timestamptz,
        v_result."c_0405"::int,
        v_result."c_0406"::int,
        v_result."c_0408"::int,
        v_result."c_0407"::int,
        v_result."c_0117"::uuid,
        v_result."c_0452"::uuid,
        v_result."c_0093"::timestamptz,
        v_result."c_0528"::timestamptz;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0110(p_0023 jsonb)
RETURNS TABLE (
    id uuid,
    "c_0187" uuid,
    "c_0422" timestamptz,
    "c_0093" timestamptz,
    "c_0528" timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_entity_id uuid;
    v_farm_id uuid;
    v_soil_report_id uuid;
    v_sample_collection_date timestamptz;
    v_measurements jsonb;
    v_item jsonb;
    v_parameter text;
    v_value float8;
    v_compound_unit_id uuid;
    v_result RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    v_entity_id := (p_0023->>'_')::uuid;

    
    
    IF NOT (
        auth.f_0119(p_0026 => v_entity_id)
        OR auth.f_0119(p_0105 => v_entity_id)
        OR auth.f_0119(p_0037 => v_entity_id)
    ) THEN
        RAISE EXCEPTION '_%_', v_entity_id;
    END IF;

    v_farm_id := (p_0023->>'_')::uuid;
    v_soil_report_id := (p_0023->>'_')::uuid;
    v_sample_collection_date := (p_0023->>'_')::timestamptz;
    v_measurements := p_0023->'_';

    IF v_measurements IS NULL OR jsonb_typeof(v_measurements) != '_' OR jsonb_array_length(v_measurements) = 0 THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0012"
        WHERE id = v_farm_id AND "c_0172" = v_entity_id
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF EXISTS (
        SELECT 1 FROM jsonb_array_elements(v_measurements) AS m
        WHERE NOT EXISTS (
            SELECT 1 FROM mrv."t_0003" cu
            WHERE cu.id = (m->>'_')::uuid
        )
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_soil_report_id IS NOT NULL THEN
        IF NOT EXISTS (
            SELECT 1 FROM mrv."t_0027"
            WHERE id = v_soil_report_id AND "c_0187" = v_farm_id
        ) THEN
            RAISE EXCEPTION '_';
        END IF;

        UPDATE mrv."t_0027"
        SET "c_0422" = v_sample_collection_date,
            "c_0528" = NOW()
        WHERE mrv."t_0027".id = v_soil_report_id
        RETURNING * INTO v_result;

        DELETE FROM mrv."t_0026"
        WHERE "c_0452" = v_soil_report_id;
    ELSE
        INSERT INTO mrv."t_0027" (
            id, "c_0187", "c_0422", "c_0093", "c_0528"
        ) VALUES (
            gen_random_uuid(), v_farm_id, v_sample_collection_date, NOW(), NOW()
        )
        RETURNING * INTO v_result;

        v_soil_report_id := v_result.id;
    END IF;

    FOR v_item IN SELECT * FROM jsonb_array_elements(v_measurements)
    LOOP
        v_parameter := v_item->>'_';
        v_value := (v_item->>'_')::float8;
        v_compound_unit_id := (v_item->>'_')::uuid;

        BEGIN
            PERFORM v_parameter::mrv."ty_0009";
        EXCEPTION WHEN invalid_text_representation THEN
            RAISE EXCEPTION '_%_', v_parameter;
        END;

        INSERT INTO mrv."t_0026" (
            id, "c_0187", "c_0452", c_0339, value, "c_0079",
            "c_0093", "c_0528"
        ) VALUES (
            gen_random_uuid(), v_farm_id, v_soil_report_id,
            v_parameter::mrv."ty_0009", v_value, v_compound_unit_id,
            NOW(), NOW()
        );
    END LOOP;

    RETURN QUERY SELECT
        v_result.id,
        v_result."c_0187"::uuid,
        v_result."c_0422"::timestamptz,
        v_result."c_0093"::timestamptz,
        v_result."c_0528"::timestamptz;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0114(
    p_0029 text,
    p_0027 uuid
)
RETURNS boolean
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_metadata_str text;
    v_rbac_context jsonb;
    v_entities jsonb;
BEGIN
    v_metadata_str := nullif(current_setting('_', true), '_');
    IF v_metadata_str IS NULL THEN
        RETURN false;
    END IF;

    BEGIN
        v_rbac_context := v_metadata_str::jsonb -> '_';
    EXCEPTION WHEN invalid_text_representation THEN
        RETURN false;
    END;

    IF v_rbac_context IS NULL THEN
        RETURN false;
    END IF;

    IF p_0029 = '_' THEN
        v_entities := v_rbac_context -> '_';
    ELSIF p_0029 = '_' THEN
        v_entities := v_rbac_context -> '_';
    ELSIF p_0029 = '_' THEN
        v_entities := v_rbac_context -> '_';
    ELSE
        RETURN false;
    END IF;

    RETURN v_entities IS NOT NULL
        AND v_entities @> to_jsonb(lower(p_0027::text));
END;
$$;





CREATE OR REPLACE FUNCTION mrv.f_0127(
    p_0025 text,
    p_0089 text,           
    p_0054 text, 
    p_0072 uuid DEFAULT NULL,
    p_0028 text DEFAULT NULL
)
RETURNS TABLE(
    f_0041 text,
    role text,
    c_0240 text,
    c_0132 text,
    c_0242 uuid,
    p_0001 uuid,
    c_0171 text,
    p_0122 uuid,
    c_0488 text,
    p_0005 uuid,
    c_0194 text,
    c_0336 text,
    c_0532 text,
    c_0243 text
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_metadata_str text;
    v_rbac_context jsonb;
    v_permissions jsonb;
    v_level text;
    v_user_enterprise_id uuid;
    v_user_supplier_id uuid;
    v_inviter_email text;
    v_db_role text;
    v_enterprise_id uuid;
    v_enterprise_name text;
    v_supplier_id uuid;
    v_supplier_name text;
    v_farmer_id uuid;
    v_farmer_name text;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    v_metadata_str := nullif(current_setting('_', true), '_');
    IF v_metadata_str IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    BEGIN
        v_rbac_context := v_metadata_str::jsonb -> '_';
    EXCEPTION WHEN invalid_text_representation THEN
        RAISE EXCEPTION '_';
    END;

    IF v_rbac_context IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    
    v_permissions := v_rbac_context -> '_';
    IF v_permissions IS NULL OR NOT (v_permissions ? '_') THEN
        RAISE EXCEPTION '_';
    END IF;

    
    IF p_0025 IS NULL OR p_0025 !~ '_' THEN
        RAISE EXCEPTION '_%_', COALESCE(p_0025, '_');
    END IF;

    
    IF p_0089 NOT IN ('_', '_') THEN
        RAISE EXCEPTION '_%_', p_0089;
    END IF;

    
    IF p_0054 NOT IN ('_', '_', '_') THEN
        RAISE EXCEPTION '_%_', p_0054;
    END IF;

    
    
    v_level := v_rbac_context -> '_' ->> 0;

    BEGIN
        v_user_enterprise_id := (v_rbac_context -> '_' ->> 0)::uuid;
    EXCEPTION WHEN invalid_text_representation THEN
        v_user_enterprise_id := NULL;
    END;

    BEGIN
        v_user_supplier_id := (v_rbac_context -> '_' ->> 0)::uuid;
    EXCEPTION WHEN invalid_text_representation THEN
        v_user_supplier_id := NULL;
    END;

    
    SELECT f_0041 INTO v_inviter_email
    FROM auth.t_0095
    WHERE id = v_current_user_id;

    
    v_db_role := p_0054 || '_' || p_0089;

    
    IF p_0054 = '_' THEN
        IF p_0072 IS NOT NULL THEN
            v_enterprise_id := p_0072;
        ELSE
            v_enterprise_name := COALESCE(p_0028, '_' || p_0025);
        END IF;

    ELSIF p_0054 = '_' THEN
        IF v_user_enterprise_id IS NULL AND p_0072 IS NULL THEN
            RAISE EXCEPTION '_';
        END IF;

        v_enterprise_id := v_user_enterprise_id;

        IF v_level = '_' THEN
            v_supplier_name := COALESCE(p_0028, '_' || p_0025);
        ELSE
            v_supplier_id := p_0072;
        END IF;

    ELSIF p_0054 = '_' THEN
        IF v_level = '_' THEN
            
            
            IF p_0089 = '_' AND p_0072 IS NOT NULL THEN
                v_supplier_id := p_0072;
            END IF;
        ELSIF v_level = '_' THEN
            v_supplier_id := v_user_supplier_id;
        ELSIF v_level = '_' THEN
            IF v_rbac_context -> '_' ? '_' THEN
                v_supplier_id := v_user_supplier_id;
                IF v_supplier_id IS NULL AND v_user_enterprise_id IS NULL THEN
                    RAISE EXCEPTION '_';
                END IF;
            ELSE
                RAISE EXCEPTION '_';
            END IF;
        ELSE
            RAISE EXCEPTION '_';
        END IF;

        v_enterprise_id := v_user_enterprise_id;

        IF p_0089 = '_' THEN
            v_farmer_name := COALESCE(p_0028, '_' || p_0025);
        ELSE
            IF p_0072 IS NULL THEN
                RAISE EXCEPTION '_';
            END IF;
            IF v_supplier_id IS NOT NULL THEN
                IF NOT EXISTS (
                    SELECT 1 FROM public.t_0091
                    WHERE p_0005 = p_0072 AND p_0122 = v_supplier_id
                ) THEN
                    RAISE EXCEPTION '_';
                END IF;
            ELSE
                IF NOT EXISTS (
                    SELECT 1 FROM public.t_0049
                    WHERE p_0005 = p_0072 AND p_0001 = v_enterprise_id
                    UNION ALL
                    SELECT 1 FROM public.t_0091 sf
                    JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
                    WHERE sf.p_0005 = p_0072 AND es.p_0001 = v_enterprise_id
                ) THEN
                    RAISE EXCEPTION '_';
                END IF;
            END IF;
            v_farmer_id := p_0072;
        END IF;
    END IF;

    RETURN QUERY SELECT
        p_0025,
        p_0089,
        p_0054,
        v_db_role,
        v_current_user_id,
        v_enterprise_id,
        v_enterprise_name,
        v_supplier_id,
        v_supplier_name,
        v_farmer_id,
        v_farmer_name,
        p_0028,
        v_level,
        v_inviter_email;
END;
$$;









CREATE OR REPLACE FUNCTION mrv.f_0129(
    p_0078 uuid,
    p_0023 jsonb,
    p_0104 boolean DEFAULT false
)
RETURNS JSONB
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_existing_id uuid;
    v_existing_submitted_at timestamptz;
    v_row RECORD;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0111(p_0078) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0033" p WHERE p."id" = p_0078
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    
    
    
    SELECT "id", "c_0483"
    INTO v_existing_id, v_existing_submitted_at
    FROM mrv."t_0034"
    WHERE "c_0369" = p_0078
    FOR UPDATE;

    IF v_existing_submitted_at IS NOT NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF v_existing_id IS NULL THEN
        INSERT INTO mrv."t_0034" (
            "c_0369",
            "c_0398",
            "c_0399",
            "c_0397",
            "c_0038",
            "c_0040",
            "c_0039",
            "c_0074",
            "c_0072",
            "c_0076",
            "c_0075",
            "c_0073",
            "c_0483"
        )
        VALUES (
            p_0078,
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_')::mrv."ty_0023",
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_')::mrv."ty_0024",
            NULLIF(p_0023->>'_', '_')::numeric,
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_'),
            NULLIF(p_0023->>'_', '_'),
            CASE WHEN p_0104 THEN now() ELSE NULL END
        )
        RETURNING * INTO v_row;
    ELSE
        
        
        
        
        UPDATE mrv."t_0034"
        SET
            "c_0398"     = NULLIF(p_0023->>'_', '_'),
            "c_0399"        = NULLIF(p_0023->>'_', '_'),
            "c_0397"      = NULLIF(p_0023->>'_', '_'),
            "c_0038"                   = NULLIF(p_0023->>'_', '_')::mrv."ty_0023",
            "c_0040"                  = NULLIF(p_0023->>'_', '_'),
            "c_0039"                = NULLIF(p_0023->>'_', '_'),
            "c_0074"              = NULLIF(p_0023->>'_', '_')::mrv."ty_0024",
            "c_0072"     = NULLIF(p_0023->>'_', '_')::numeric,
            "c_0076"  = NULLIF(p_0023->>'_', '_'),
            "c_0075"           = NULLIF(p_0023->>'_', '_'),
            "c_0073"         = NULLIF(p_0023->>'_', '_'),
            "c_0483"                       = CASE WHEN p_0104 THEN now() ELSE "c_0483" END,
            "c_0528"                         = now()
        WHERE "id" = v_existing_id AND "c_0483" IS NULL
        RETURNING * INTO v_row;

        IF NOT FOUND THEN
            RAISE EXCEPTION '_';
        END IF;
    END IF;

    RETURN to_jsonb(v_row);
END;
$$;

COMMENT ON FUNCTION mrv.f_0129(uuid, jsonb, boolean) IS
    '_';




















CREATE OR REPLACE FUNCTION mrv.f_0130(
    p_0078 uuid,
    p_0035    uuid
)
RETURNS jsonb
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_eligibility_boundary public.geometry;
    v_farm_polygon_id      uuid;
    v_farm_geometry        public.geometry;
    v_boundary_normalized  public.geometry;
    v_farm_normalized      public.geometry;
    v_matched_row_id       uuid;
    v_has_matrix_rows      boolean;
BEGIN
    
    SELECT "c_0147"
    INTO v_eligibility_boundary
    FROM mrv."t_0033"
    WHERE id = p_0078;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_%_', p_0078;
    END IF;

    
    
    SELECT "c_0355"
    INTO v_farm_polygon_id
    FROM mrv."t_0012"
    WHERE id = p_0035;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_%_', p_0035;
    END IF;

    
    
    
    
    IF v_eligibility_boundary IS NOT NULL THEN
        IF v_farm_polygon_id IS NULL THEN
            RETURN jsonb_build_object(
                '_',       false,
                '_',        '_',
                '_',  NULL
            );
        END IF;

        SELECT geometry
        INTO v_farm_geometry
        FROM mrv."Polygon"
        WHERE id = v_farm_polygon_id;

        IF NOT FOUND OR v_farm_geometry IS NULL THEN
            RETURN jsonb_build_object(
                '_',       false,
                '_',        '_',
                '_',  NULL
            );
        END IF;

        
        
        v_boundary_normalized := CASE WHEN ST_SRID(v_eligibility_boundary) = 0
            THEN ST_SetSRID(v_eligibility_boundary, 4326)
            ELSE ST_Transform(v_eligibility_boundary, 4326)
        END;
        v_farm_normalized := CASE WHEN ST_SRID(v_farm_geometry) = 0
            THEN ST_SetSRID(v_farm_geometry, 4326)
            ELSE ST_Transform(v_farm_geometry, 4326)
        END;

        IF NOT ST_Intersects(v_boundary_normalized, v_farm_normalized) THEN
            RETURN jsonb_build_object(
                '_',       false,
                '_',        '_',
                '_',  NULL
            );
        END IF;
    END IF;

    
    SELECT EXISTS (
        SELECT 1
        FROM mrv."t_0036"
        WHERE "c_0369" = p_0078
    ) INTO v_has_matrix_rows;

    IF NOT v_has_matrix_rows THEN
        RETURN jsonb_build_object(
            '_',       true,
            '_',        '_',
            '_',  NULL
        );
    END IF;

    
    
    
    
    
    
    
    
    SELECT id
    INTO v_matched_row_id
    FROM mrv."t_0036"
    WHERE "c_0369" = p_0078
    ORDER BY "c_0093"
    LIMIT 1;

    IF v_matched_row_id IS NOT NULL THEN
        RETURN jsonb_build_object(
            '_',       true,
            '_',        '_',
            '_',  v_matched_row_id
        );
    END IF;

    RETURN jsonb_build_object(
        '_',       false,
        '_',        '_',
        '_',  NULL
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0131(
    p_0079 uuid,
    p_0070 text,
    p_0014 char(2)[],
    p_0117 text DEFAULT NULL,
    p_0024 jsonb DEFAULT 'null'::jsonb
)
RETURNS JSONB
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_project_id uuid;
    v_proponent_id uuid;
    v_eligibility_row jsonb;
    v_row_index integer;
    v_allowed_practices text[];
BEGIN
    IF NOT auth.f_0061() THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0079 IS NULL THEN
        SELECT id INTO v_proponent_id
        FROM mrv."t_0024"
        WHERE "c_0258" = true;

        IF v_proponent_id IS NULL THEN
            RAISE EXCEPTION '_';
        END IF;
    ELSE
        v_proponent_id := p_0079;

        IF NOT EXISTS (SELECT 1 FROM mrv."t_0024" WHERE id = v_proponent_id) THEN
            RAISE EXCEPTION '_%_', v_proponent_id;
        END IF;
    END IF;

    INSERT INTO mrv."t_0033" (
        "c_0375",
        "name",
        "c_0087",
        "c_0538",
        "c_0472"
    )
    VALUES (
        v_proponent_id,
        p_0070,
        p_0014,
        p_0117,
        '_'
    )
    RETURNING "id" INTO v_project_id;

    INSERT INTO mrv."t_0035" ("c_0369", "c_0357", "c_0257")
    SELECT v_project_id, pool_type, true
    FROM unnest(ARRAY[
        '_',
        '_',
        '_',
        '_',
        '_',
        '_'
    ]::mrv."ty_0025"[]) AS pool_type;

    IF p_0024 IS NOT NULL AND jsonb_array_length(p_0024) > 0 THEN
        v_row_index := 0;
        FOR v_eligibility_row IN SELECT * FROM jsonb_array_elements(p_0024)
        LOOP
            IF (v_eligibility_row->>'_') IS NULL
                OR (v_eligibility_row->>'_') IS NULL
                OR (v_eligibility_row->>'_') IS NULL
                OR (v_eligibility_row->>'_') IS NULL
                OR (v_eligibility_row->>'_') IS NULL
            THEN
                RAISE EXCEPTION '_%_', v_row_index;
            END IF;

            SELECT ARRAY(
                SELECT jsonb_array_elements_text(v_eligibility_row->'_')
            ) INTO v_allowed_practices;

            INSERT INTO mrv."t_0036" (
                "c_0369",
                "c_0089",
                "c_0066",
                "c_0451",
                "c_0358",
                "c_0108",
                "c_0009"
            )
            VALUES (
                v_project_id,
                v_eligibility_row->>'_',
                v_eligibility_row->>'_',
                v_eligibility_row->>'_',
                v_eligibility_row->>'_',
                v_eligibility_row->>'_',
                COALESCE(v_allowed_practices, '{}'::text[])
            );

            v_row_index := v_row_index + 1;
        END LOOP;
    END IF;

    RETURN mrv.f_0138(v_project_id);
END;
$$;

COMMENT ON FUNCTION mrv.f_0131(uuid, text, char(2)[], text, jsonb) IS E'_';







CREATE OR REPLACE FUNCTION mrv.f_0132(
    p_0105 uuid,
    p_0070 text,
    p_0014 char(2)[]
)
RETURNS JSONB
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_project_id    uuid;
    v_proponent_id  uuid;
    v_rbac_context  jsonb;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0105 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0061() THEN
        v_rbac_context := auth.f_0066()->'_';
        IF v_rbac_context IS NULL
           OR NOT (v_rbac_context->'_' ? p_0105::text)
        THEN
            RAISE EXCEPTION '_';
        END IF;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM public.t_0092 WHERE id = p_0105
    ) THEN
        RAISE EXCEPTION '_%_', p_0105;
    END IF;

    SELECT id INTO v_proponent_id
    FROM mrv."t_0024"
    WHERE "c_0258" = true;

    IF v_proponent_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    INSERT INTO mrv."t_0033" (
        "c_0375",
        "name",
        "c_0087",
        "c_0472"
    )
    VALUES (
        v_proponent_id,
        p_0070,
        p_0014,
        '_'
    )
    RETURNING "id" INTO v_project_id;

    INSERT INTO mrv."t_0038" (
        "c_0369",
        "c_0237",
        "c_0486",
        "c_0472"
    )
    VALUES (
        v_project_id,
        '_',
        p_0105,
        '_'
    );

    INSERT INTO mrv."t_0035" ("c_0369", "c_0357", "c_0257")
    SELECT v_project_id, pool_type, true
    FROM unnest(ARRAY[
        '_',
        '_',
        '_',
        '_',
        '_',
        '_'
    ]::mrv."ty_0025"[]) AS pool_type;

    RETURN mrv.f_0138(v_project_id);
END;
$$;







CREATE OR REPLACE FUNCTION mrv.f_0133(
    p_0078 uuid
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0111(p_0078) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0033" p
        WHERE p."id" = p_0078
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    DELETE FROM mrv."t_0033"
    WHERE "id" = p_0078;
END;
$$;











CREATE OR REPLACE FUNCTION mrv.f_0134(
    p_0078    uuid,
    p_0053 text,
    p_0105   uuid,
    p_0015  text
)
RETURNS TABLE (
    id            uuid,
    "c_0369"   uuid,
    "c_0237" text,
    c_0472        text,
    "c_0089" text,
    "c_0486"  uuid,
    "c_0093"   timestamptz,
    "c_0528"   timestamptz
)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_proponent_id  uuid;
    v_status        text := '_';
BEGIN
    SELECT "c_0375"
    INTO v_proponent_id
    FROM mrv."t_0033"
    WHERE id = p_0078;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_%_', p_0078;
    END IF;

    IF NOT auth.f_0122(v_proponent_id) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0053 NOT IN ('_', '_', '_') THEN
        RAISE EXCEPTION '_%_', p_0053;
    END IF;

    IF EXISTS (
        SELECT 1
        FROM mrv."t_0036"
        WHERE "c_0369" = p_0078
          AND "c_0089" = p_0015
    ) THEN
        v_status := '_';
    END IF;

    RETURN QUERY
    INSERT INTO mrv."t_0038" (
        "c_0369",
        "c_0237",
        "c_0472",
        "c_0486",
        "c_0089"
    )
    VALUES (
        p_0078,
        p_0053::mrv."ty_0027",
        v_status::mrv."ty_0026",
        p_0105,
        p_0015
    )
    RETURNING
        "t_0038".id,
        "t_0038"."c_0369",
        "t_0038"."c_0237"::text,
        "t_0038".c_0472::text,
        "t_0038"."c_0089",
        "t_0038"."c_0486",
        "t_0038"."c_0093",
        "t_0038"."c_0528";
END;
$$;








CREATE OR REPLACE FUNCTION mrv.f_0135(
    p_0078 uuid
)
RETURNS JSONB
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0033" p
        WHERE p."id" = p_0078
    ) THEN
        RETURN NULL;
    END IF;

    IF NOT auth.f_0111(p_0078) THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN mrv.f_0138(p_0078);
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0136()
RETURNS JSONB
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    RETURN COALESCE(
        (
            SELECT jsonb_agg(mrv.f_0138(p."id") ORDER BY p."c_0093" DESC)
            FROM mrv."t_0033" p
            WHERE auth.f_0111(p."id")
        ),
        'null'::jsonb
    );
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0137(
    p_0078 uuid,
    p_0035 uuid
)
RETURNS JSONB
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0111(p_0078) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0033" p
        WHERE p."id" = p_0078
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    DELETE FROM mrv."t_0037"
    WHERE "c_0369" = p_0078
      AND "c_0187" = p_0035;

    UPDATE mrv."t_0033"
    SET "c_0528" = now()
    WHERE "id" = p_0078;

    RETURN mrv.f_0138(p_0078);
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0138(
    p_0078 uuid
)
RETURNS JSONB
LANGUAGE plpgsql
STABLE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_result JSONB;
BEGIN
    SELECT jsonb_build_object(
        '_', p."id",
        '_', p."name",
        '_', p."c_0140",
        '_', p."c_0472"::text,
        '_', to_char(p."c_0098", '_'),
        '_', to_char(p."c_0097", '_'),
        '_', p."c_0368",
        '_', p."c_0042",
        '_', p."c_0005",
        '_', p."c_0289",
        '_', p."c_0318",
        '_', p."c_0343",
        '_', p."c_0093",
        '_', p."c_0528",
        '_', COALESCE(
            (
                SELECT jsonb_agg(
                    jsonb_build_object(
                        '_', cp."id",
                        '_', cp."c_0357"::text,
                        '_', cp."c_0257",
                        '_', cp."c_0180"
                    )
                    ORDER BY cp."c_0357"::text
                )
                FROM mrv."t_0035" cp
                WHERE cp."c_0369" = p."id"
            ),
            'null'::jsonb
        ),
        '_', COALESCE(
            (
                SELECT jsonb_agg(
                    jsonb_build_object(
                        '_', pf."id",
                        '_', pf."c_0187",
                        '_', f."name",
                        '_', pf."c_0478"
                    )
                    ORDER BY f."name"
                )
                FROM mrv."t_0037" pf
                JOIN mrv."t_0012" f ON f."id" = pf."c_0187"
                WHERE pf."c_0369" = p."id"
            ),
            'null'::jsonb
        )
    )
    INTO v_result
    FROM mrv."t_0033" p
    WHERE p."id" = p_0078;

    RETURN v_result;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0139(
    p_0078 uuid,
    p_0112 jsonb
)
RETURNS JSONB
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_status_value text;
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0111(p_0078) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0033" p
        WHERE p."id" = p_0078
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0112 ? '_' AND p_0112->>'_' IS NOT NULL THEN
        v_status_value := p_0112->>'_';
        IF v_status_value NOT IN ('_', '_', '_', '_') THEN
            RAISE EXCEPTION '_%_', v_status_value;
        END IF;
    END IF;

    UPDATE mrv."t_0033"
    SET
        "name" = CASE WHEN p_0112 ? '_' THEN p_0112->>'_' ELSE "name" END,
        "c_0140" = CASE WHEN p_0112 ? '_' THEN p_0112->>'_' ELSE "c_0140" END,
        "c_0472" = CASE WHEN p_0112 ? '_' AND p_0112->>'_' IS NOT NULL
                        THEN (p_0112->>'_')::mrv."ty_0029"
                        ELSE "c_0472" END,
        "c_0098" = CASE WHEN p_0112 ? '_'
                                      THEN (p_0112->>'_')::date
                                      ELSE "c_0098" END,
        "c_0097" = CASE WHEN p_0112 ? '_'
                                    THEN (p_0112->>'_')::date
                                    ELSE "c_0097" END,
        "c_0368" = CASE WHEN p_0112 ? '_'
                                            THEN p_0112->>'_'
                                            ELSE "c_0368" END,
        "c_0042" = CASE WHEN p_0112 ? '_'
                                             THEN p_0112->>'_'
                                             ELSE "c_0042" END,
        "c_0005" = CASE WHEN p_0112 ? '_'
                                            THEN p_0112->>'_'
                                            ELSE "c_0005" END,
        "c_0289" = CASE WHEN p_0112 ? '_'
                                       THEN p_0112->>'_'
                                       ELSE "c_0289" END,
        "c_0318" = CASE WHEN p_0112 ? '_'
                                           THEN p_0112->>'_'
                                           ELSE "c_0318" END,
        "c_0343" = CASE WHEN p_0112 ? '_'
                                         THEN (p_0112->>'_')::numeric
                                         ELSE "c_0343" END,
        "c_0528" = now()
    WHERE "id" = p_0078;

    RETURN mrv.f_0138(p_0078);
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0140(
    p_0078 uuid,
    p_0075 jsonb
)
RETURNS JSONB
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
SET search_path = mrv, auth, public
AS $$
DECLARE
    v_pool jsonb;
    v_pool_type text;
    v_is_included boolean;
    v_justification text;
    v_all_pool_types mrv."ty_0025"[] := ARRAY[
        '_',
        '_',
        '_',
        '_',
        '_',
        '_'
    ]::mrv."ty_0025"[];
    v_provided_types text[];
BEGIN
    IF auth.f_0102() IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0111(p_0078) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM mrv."t_0033" p
        WHERE p."id" = p_0078
    ) THEN
        RAISE EXCEPTION '_';
    END IF;

    SELECT array_agg(pool->>'_')
    INTO v_provided_types
    FROM jsonb_array_elements(p_0075) AS pool;

    IF (
        SELECT count(*)
        FROM unnest(v_all_pool_types) AS required_type
        WHERE required_type::text != ALL(COALESCE(v_provided_types, ARRAY[]::text[]))
    ) > 0 THEN
        RAISE EXCEPTION '_';
    END IF;

    FOR v_pool IN SELECT * FROM jsonb_array_elements(p_0075)
    LOOP
        v_pool_type := v_pool->>'_';
        v_is_included := (v_pool->>'_')::boolean;
        v_justification := v_pool->>'_';

        IF v_pool_type = '_' AND NOT v_is_included THEN
            RAISE EXCEPTION '_';
        END IF;

        IF NOT v_is_included AND v_justification IS NULL THEN
            RAISE EXCEPTION '_%_', v_pool_type;
        END IF;
    END LOOP;

    DELETE FROM mrv."t_0035"
    WHERE "c_0369" = p_0078;

    INSERT INTO mrv."t_0035" (
        "c_0369",
        "c_0357",
        "c_0257",
        "c_0180"
    )
    SELECT
        p_0078,
        (pool->>'_')::mrv."ty_0025",
        (pool->>'_')::boolean,
        pool->>'_'
    FROM jsonb_array_elements(p_0075) AS pool;

    UPDATE mrv."t_0033"
    SET "c_0528" = now()
    WHERE "id" = p_0078;

    RETURN mrv.f_0138(p_0078);
END;
$$;


CREATE OR REPLACE FUNCTION mrv.f_0141(
    p_0026 uuid,
    p_0012 int
)
RETURNS TABLE (
    p_0004 uuid,
    c_0191 text,
    c_0090 text,
    c_0429 text,
    c_0279 text,
    c_0162 double precision,
    c_0165 double precision,
    c_0163 double precision,
    c_0164 double precision,
    c_0113 double precision,
    c_0116 double precision,
    c_0114 double precision,
    c_0115 double precision,
    c_0322 double precision,
    c_0519 double precision,
    c_0161 text,
    c_0053 timestamptz,
    c_0056 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
BEGIN
    RETURN QUERY
    SELECT
        f.id AS p_0004,
        f.name AS c_0191,
        f."ty_0003"::text AS c_0090,
        f."ty_0016"::text AS c_0429,
        f."ty_0013"::text AS c_0279,
        d."c_0157" AS c_0162,
        d."c_0160" AS c_0165,
        d."c_0158" AS c_0163,
        d."c_0159" AS c_0164,
        d."c_0109" AS c_0113,
        d."c_0112" AS c_0116,
        d."c_0110" AS c_0114,
        d."c_0111" AS c_0115,
        d."c_0321" AS c_0322,
        d."c_0511" AS c_0519,
        d."c_0156"::text AS c_0161,
        d."c_0052" AS c_0053,
        d."c_0055" AS c_0056
    FROM mrv."t_0012" f
    JOIN mrv."t_0007" d
        ON d."c_0187" = f.id
        AND d."c_0057" = p_0012
    WHERE f."c_0172" IN (
        SELECT ur.p_0124
        FROM public.t_0094 ur
        JOIN public.t_0091 sf ON sf.p_0005 = ur.p_0005
        JOIN public.t_0050 es ON es.p_0122 = sf.p_0122
        WHERE es.p_0001 = p_0026
    )
    ORDER BY f.name;
END;
$$;




CREATE OR REPLACE FUNCTION mrv.f_0144(
    p_0052 uuid
)
RETURNS TABLE (
    c_0439 text,
    c_0129 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
AS $$
DECLARE
    v_supplier_id uuid;
BEGIN
    SELECT vpi."c_0486"
    INTO v_supplier_id
    FROM mrv."t_0038" vpi
    WHERE vpi.id = p_0052;

    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', vp.id,
                        '_', vp.name,
                        '_', vp.c_0140,
                        '_', vp.c_0472,
                        '_', vp."c_0098",
                        '_', vp."c_0097",
                        '_', jsonb_build_object(
                            '_', vp."c_0368",
                            '_', vp."c_0042",
                            '_', vp."c_0005",
                            '_', vp."c_0289",
                            '_', vp."c_0318",
                            '_', vp."c_0343"
                        ),
                        '_', (
                            SELECT COALESCE(jsonb_agg(
                                jsonb_build_object(
                                    '_', cp."c_0357",
                                    '_', cp."c_0257",
                                    '_', cp."c_0180"
                                )
                            ), 'null'::jsonb)
                            FROM mrv."t_0035" cp
                            WHERE cp."c_0369" = vp.id
                        ),
                        '_', (
                            SELECT COALESCE(jsonb_agg(pf."c_0187"), 'null'::jsonb)
                            FROM mrv."t_0037" pf
                            WHERE pf."c_0369" = vp.id
                              AND pf."c_0236" = p_0052
                        )
                    )
                ), 'null'::jsonb)
            )
            FROM mrv."t_0033" vp
            WHERE vp.id IN (
                SELECT DISTINCT pf2."c_0369"
                FROM mrv."t_0037" pf2
                WHERE pf2."c_0236" = p_0052
            )
        ) AS c_0129;

    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', f.id,
                        '_', f.name,
                        '_', f."ty_0003",
                        '_', f."ty_0016",
                        '_', f."ty_0013",
                        '_', CASE
                            WHEN p.geometry IS NOT NULL
                            THEN ST_AsGeoJSON(p.geometry)::jsonb
                        END,
                        '_', (
                            SELECT pf."c_0478"
                            FROM mrv."t_0037" pf
                            WHERE pf."c_0187" = f.id
                              AND pf."c_0236" = p_0052
                            ORDER BY pf."c_0369"
                            LIMIT 1
                        )
                    )
                    ORDER BY f.id
                ), 'null'::jsonb)
            )
            FROM mrv."t_0012" f
            LEFT JOIN mrv."Polygon" p ON p.id = f."c_0355"
            WHERE f.id IN (
                SELECT pf."c_0187"
                FROM mrv."t_0037" pf
                WHERE pf."c_0236" = p_0052
            )
        ) AS c_0129;

    
    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', cp.id,
                        '_', cp.c_0472,
                        '_', cp."c_0218",
                        '_', cp."c_0494",
                        '_', cp."c_0495",
                        '_', cp."c_0121",
                        '_', cp."c_0122",
                        '_', cp."c_0209",
                        '_', cp.c_0413,
                        '_', (
                            SELECT COALESCE(jsonb_agg(cpf."c_0187"), 'null'::jsonb)
                            FROM mrv."t_0002" cpf
                            WHERE cpf."c_0058" = cp.id
                        ),
                        '_', cp."c_0093"
                    )
                ), 'null'::jsonb)
            )
            FROM mrv."t_0001" cp
            WHERE cp."c_0172" = v_supplier_id
              AND cp.c_0472 = 'v1'
        ) AS c_0129;

    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', d."c_0187",
                        '_', d."c_0057",
                        '_', d."c_0321",
                        '_', d."c_0511",
                        '_', d."c_0157",
                        '_', d."c_0160",
                        '_', d."c_0158",
                        '_', d."c_0159",
                        '_', d."c_0109",
                        '_', d."c_0112",
                        '_', d."c_0110",
                        '_', d."c_0111",
                        '_', d."c_0156",
                        '_', d."c_0052"
                    )
                    ORDER BY d."c_0187", d."c_0057"
                ), 'null'::jsonb),
                '_', (
                    SELECT COALESCE(jsonb_agg(
                        jsonb_build_object(
                            '_', sub."c_0187",
                            '_', sub.total_net,
                            '_', sub.latest_carbon,
                            '_', sub.year_count,
                            '_', sub.first_year,
                            '_', sub.last_year
                        )
                    ), 'null'::jsonb)
                    FROM (
                        SELECT
                            d2."c_0187",
                            SUM(d2."c_0321") AS total_net,
                            (ARRAY_AGG(d2."c_0511" ORDER BY d2."c_0057" DESC))[1] AS latest_carbon,
                            COUNT(*) AS year_count,
                            MIN(d2."c_0057") AS first_year,
                            MAX(d2."c_0057") AS last_year
                        FROM mrv."t_0007" d2
                        WHERE d2."c_0187" IN (
                            SELECT pf."c_0187"
                            FROM mrv."t_0037" pf
                            WHERE pf."c_0236" = p_0052
                        )
                        GROUP BY d2."c_0187"
                    ) sub
                )
            )
            FROM mrv."t_0007" d
            WHERE d."c_0187" IN (
                SELECT pf."c_0187"
                FROM mrv."t_0037" pf
                WHERE pf."c_0236" = p_0052
            )
        ) AS c_0129;

    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', c.id,
                        '_', c."c_0187",
                        '_', c."c_0102",
                        '_', c."c_0351",
                        '_', c."c_0222",
                        '_', c."c_0250",
                        '_', c."c_0501",
                        '_', c.c_0472,
                        '_', jsonb_build_object(
                            '_', s.id,
                            '_', s.name,
                            '_', s."c_0470",
                            '_', s."c_0168"
                        )
                    )
                    ORDER BY c."c_0187", c."c_0351"
                ), 'null'::jsonb)
            )
            FROM mrv."t_0006" c
            JOIN mrv."t_0025" s ON s.id = c."c_0436"
            WHERE c."c_0187" IN (
                SELECT pf."c_0187"
                FROM mrv."t_0037" pf
                WHERE pf."c_0236" = p_0052
            )
        ) AS c_0129;

    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', sr.id,
                        '_', sr."c_0187",
                        '_', sr."c_0422",
                        '_', (
                            SELECT COALESCE(jsonb_agg(
                                jsonb_build_object(
                                    '_', sm.c_0339,
                                    '_', sm.value
                                )
                            ), 'null'::jsonb)
                            FROM mrv."t_0026" sm
                            WHERE sm."c_0452" = sr.id
                        )
                    )
                    ORDER BY sr."c_0187", sr."c_0422"
                ), 'null'::jsonb)
            )
            FROM mrv."t_0027" sr
            WHERE sr."c_0187" IN (
                SELECT pf."c_0187"
                FROM mrv."t_0037" pf
                WHERE pf."c_0236" = p_0052
            )
        ) AS c_0129;

    
    
    RETURN QUERY
    SELECT
        '_'::text AS c_0439,
        (
            SELECT jsonb_build_object(
                '_', COALESCE(jsonb_agg(
                    jsonb_build_object(
                        '_', s.id,
                        '_', s.name,
                        '_', s."c_0470",
                        '_', s."c_0168"
                    )
                    ORDER BY s."c_0470"
                ), 'null'::jsonb)
            )
            FROM mrv."t_0025" s
            WHERE s."c_0172" = v_supplier_id
        ) AS c_0129;
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0145(
    p_0050 uuid
)
RETURNS TABLE(id uuid, "c_0486" uuid, "c_0170" uuid, "c_0455" text, "c_0458" uuid, "c_0102" text, "c_0381" double precision, "c_0489" date, "c_0094" uuid, "c_0093" timestamptz, "c_0528" timestamptz)
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
    v_existing RECORD;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    PERFORM pg_advisory_xact_lock(hashtext('_' || p_0050::text));

    SELECT *
    INTO v_existing
    FROM mrv."t_0028"
    WHERE id = p_0050;

    IF v_existing IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0105 => v_existing."c_0486") THEN
        RAISE EXCEPTION '_';
    END IF;

    DELETE FROM mrv."t_0028" WHERE id = p_0050;

    IF NOT FOUND THEN
        RAISE EXCEPTION '_%_', p_0050;
    END IF;

    RETURN QUERY SELECT
        v_existing.id,
        v_existing."c_0486",
        v_existing."c_0170",
        v_existing."c_0455"::text,
        v_existing."c_0458",
        v_existing."c_0102"::text,
        v_existing."c_0381",
        v_existing."c_0489",
        v_existing."c_0094",
        v_existing."c_0093",
        v_existing."c_0528";
END;
$$;

CREATE OR REPLACE FUNCTION mrv.f_0146(
    p_0026 uuid,
    p_0118 int
)
RETURNS TABLE (
    c_0032 numeric,
    c_0312 numeric,
    c_0306 numeric,
    c_0473 numeric,
    c_0051 jsonb,
    c_0049 jsonb,
    c_0523 jsonb
)
LANGUAGE plpgsql
STABLE
SECURITY DEFINER 
SET search_path = mrv, auth, public
AS $$
#variable_conflict use_column
DECLARE
    v_current_user_id uuid;
BEGIN
    v_current_user_id := auth.f_0102();
    IF v_current_user_id IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF NOT auth.f_0119(p_0026 => p_0026) THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 IS NULL THEN
        RAISE EXCEPTION '_';
    END IF;

    IF p_0118 < 2000 OR p_0118 > EXTRACT(YEAR FROM NOW())::int + 5 THEN
        RAISE EXCEPTION '_%_', p_0118;
    END IF;

    RETURN QUERY
    WITH t_0050 AS (
        SELECT s.id AS p_0122, s.name AS c_0488
        FROM public.t_0050 es
        JOIN public.t_0092 s ON s.id = es.p_0122
        WHERE es.p_0001 = p_0026
    ),
    enterprise_farms AS (
        SELECT farm.id AS p_0004, farm."c_0172" AS con_0122, p.geometry,
               ST_Area(p.geometry::geography) / 10000 AS c_0018
        FROM mrv."t_0012" farm
        JOIN mrv."Polygon" p ON p.id = farm."c_0355"
        WHERE farm."c_0172" IN (SELECT p_0122 FROM t_0050)
        UNION
        
        SELECT farm.id, farm."c_0172", p.geometry,
               ST_Area(p.geometry::geography) / 10000
        FROM mrv."t_0012" farm
        JOIN mrv."Polygon" p ON p.id = farm."c_0355"
        JOIN public.t_0091 sf ON sf.p_0005 = farm."c_0172"
        JOIN t_0050 es ON es.p_0122 = sf.p_0122
    ),
    farm_supplier_mapping AS (
        SELECT ef.p_0004, ef.con_0122 AS p_0122
        FROM enterprise_farms ef
        WHERE ef.con_0122 IN (SELECT p_0122 FROM t_0050)
        UNION
        SELECT ef.p_0004, sf.p_0122
        FROM enterprise_farms ef
        JOIN public.t_0091 sf ON sf.p_0005 = ef.con_0122
    ),
    harvest_data AS (
        SELECT
            hr."c_0512",
            c."c_0102"::text AS c_0105,
            ef.p_0004,
            ef.con_0122,
            ef.c_0018
        FROM mrv."t_0016" hr
        JOIN mrv."t_0006" c ON c.id = hr."c_0117"
        JOIN enterprise_farms ef ON ef.p_0004 = c."c_0187"
        WHERE c.c_0472 = 'v1'
            AND c."c_0222" >= make_date(p_0118, 1, 1)
            AND c."c_0222" < make_date(p_0118 + 1, 1, 1)
            AND hr."c_0512" IS NOT NULL
            AND hr."c_0512" > 0
            AND ef.c_0018 > 0
    ),
    yield_per_ha_data AS (
        SELECT
            p_0004,
            con_0122,
            c_0105,
            "c_0512",
            c_0018,
            ("c_0512" / c_0018) AS yield_per_ha
        FROM harvest_data
    ),
    overall_stats AS (
        SELECT
            AVG(yield_per_ha)::numeric    AS c_0032,
            MIN(yield_per_ha)::numeric    AS c_0312,
            MAX(yield_per_ha)::numeric    AS c_0306,
            STDDEV(yield_per_ha)::numeric AS c_0473
        FROM yield_per_ha_data
    ),
    by_supplier_agg AS (
        SELECT
            es.p_0122,
            es.c_0488,
            AVG(yphd.yield_per_ha)::numeric AS c_0032,
            COUNT(DISTINCT fsm.p_0004) AS c_0189
        FROM t_0050 es
        JOIN farm_supplier_mapping fsm ON fsm.p_0122 = es.p_0122
        JOIN yield_per_ha_data yphd ON yphd.p_0004 = fsm.p_0004
        GROUP BY es.p_0122, es.c_0488
    ),
    by_supplier_json AS (
        SELECT COALESCE(jsonb_agg(
            jsonb_build_object(
                '_', p_0122,
                '_', c_0488,
                '_', c_0032,
                '_', c_0189
            )
        ), 'null'::jsonb) AS c_0411
        FROM by_supplier_agg
    ),
    crop_area AS (
        SELECT c_0105, SUM(c_0018)::numeric AS c_0018
        FROM (SELECT DISTINCT c_0105, p_0004, c_0018 FROM yield_per_ha_data) deduped
        GROUP BY c_0105
    ),
    by_crop_agg AS (
        SELECT
            yphd.c_0105,
            AVG(yphd.yield_per_ha)::numeric AS c_0032,
            SUM(yphd."c_0512")::numeric AS c_0521,
            ca.c_0018
        FROM yield_per_ha_data yphd
        JOIN crop_area ca ON ca.c_0105 = yphd.c_0105
        GROUP BY yphd.c_0105, ca.c_0018
    ),
    by_crop_json AS (
        SELECT COALESCE(jsonb_agg(
            jsonb_build_object(
                '_', c_0105,
                '_', c_0032,
                '_', c_0521,
                '_', c_0018
            )
        ), 'null'::jsonb) AS c_0411
        FROM by_crop_agg
    ),
    trend_years AS (
        SELECT generate_series(p_0118 - 4, p_0118) AS yr
    ),
    trend_data AS (
        SELECT
            ty.yr,
            AVG(
                CASE
                    WHEN hr_trend."c_0512" IS NOT NULL
                         AND hr_trend."c_0512" > 0
                         AND ef_trend.c_0018 > 0
                    THEN hr_trend."c_0512" / ef_trend.c_0018
                    ELSE NULL
                END
            )::numeric AS c_0032
        FROM trend_years ty
        LEFT JOIN mrv."t_0006" c_trend ON
            c_trend.c_0472 = 'v1'
            AND c_trend."c_0222" >= make_date(ty.yr, 1, 1)
            AND c_trend."c_0222" < make_date(ty.yr + 1, 1, 1)
            AND c_trend."c_0187" IN (SELECT p_0004 FROM enterprise_farms)
        LEFT JOIN mrv."t_0016" hr_trend ON hr_trend."c_0117" = c_trend.id
        LEFT JOIN enterprise_farms ef_trend ON ef_trend.p_0004 = c_trend."c_0187"
        GROUP BY ty.yr
        ORDER BY ty.yr
    ),
    trend_json AS (
        SELECT COALESCE(jsonb_agg(
            jsonb_build_object(
                '_', yr,
                '_', c_0032
            )
            ORDER BY yr
        ), 'null'::jsonb) AS c_0411
        FROM trend_data
    )
    SELECT
        os.c_0032,
        os.c_0312,
        os.c_0306,
        os.c_0473,
        bsj.c_0411 AS c_0051,
        bcj.c_0411 AS c_0049,
        tj.c_0411  AS c_0523
    FROM overall_stats os
    CROSS JOIN by_supplier_json bsj
    CROSS JOIN by_crop_json bcj
    CROSS JOIN trend_json tj;
END;
$$;




CREATE TRIGGER trg_0007
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0012"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0013
    AFTER INSERT OR UPDATE OR DELETE ON mrv."Polygon"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0003
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0005"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0004
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0006"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0014
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0021"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0008
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0014"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();


CREATE TRIGGER trg_0005
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0007"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0006
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0009"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();


CREATE TRIGGER trg_0001
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0001"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0002
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0002"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE TRIGGER trg_0015
    AFTER INSERT OR UPDATE OR DELETE ON mrv."t_0042"
    FOR EACH ROW EXECUTE FUNCTION audit.f_0067();

CREATE OR REPLACE FUNCTION mrv.f_0018()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
  IF OLD.c_0472 IS DISTINCT FROM NEW.c_0472 THEN
    PERFORM pg_notify(
      '_',
      json_build_object('_', json_build_array('_', NEW.id))::text
    );
  END IF;
  RETURN NEW;
END;
$$;

CREATE OR REPLACE TRIGGER trg_0016
  AFTER UPDATE OF c_0472 ON mrv."t_0001"
  FOR EACH ROW
  WHEN (OLD.c_0472 IS DISTINCT FROM NEW.c_0472)
  EXECUTE FUNCTION mrv.f_0018();




CREATE TRIGGER trg_0018
  AFTER INSERT OR UPDATE ON mrv."t_0012"
  FOR EACH ROW
  WHEN (NEW."c_0355" IS NOT NULL)
  EXECUTE FUNCTION mrv.f_0101();

CREATE TRIGGER trg_0025
  AFTER INSERT OR UPDATE ON mrv."Polygon"
  FOR EACH ROW
  WHEN (NEW."c_0187" IS NOT NULL)
  EXECUTE FUNCTION mrv.f_0101();

DROP TRIGGER IF EXISTS trg_0030 ON mrv."t_0014";
CREATE TRIGGER trg_0030
    AFTER INSERT OR UPDATE ON mrv."t_0014"
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0068();

DROP TRIGGER IF EXISTS trg_0031 ON mrv."t_0016";
CREATE TRIGGER trg_0031
    AFTER INSERT OR UPDATE ON mrv."t_0016"
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0068();

DROP TRIGGER IF EXISTS trg_0032 ON mrv."t_0027";
CREATE TRIGGER trg_0032
    AFTER INSERT OR UPDATE ON mrv."t_0027"
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0068();

DROP TRIGGER IF EXISTS trg_0029 ON mrv."t_0008";
CREATE TRIGGER trg_0029
    AFTER INSERT OR UPDATE ON mrv."t_0008"
    FOR EACH ROW EXECUTE FUNCTION mrv.f_0068();


ALTER TABLE mrv."t_0001" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0093" ON mrv."t_0001" FOR ALL TO service_role
    USING (true) WITH CHECK (82 = 82);

CREATE POLICY "pol_0082" ON mrv."t_0001" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (83 = 83);

CREATE POLICY "pol_0091" ON mrv."t_0001" FOR SELECT TO authenticated
    USING (
        "c_0172" = auth.f_0102()
        AND EXISTS (
            SELECT 1
            FROM public.t_0092 s
            WHERE s.id = "c_0172"
              AND s.ty_0034 = 'v1'
        )
    );

CREATE POLICY "pol_0090" ON mrv."t_0001" FOR INSERT TO authenticated
    WITH CHECK (84 = 84);

CREATE POLICY "pol_0092" ON mrv."t_0001" FOR UPDATE TO authenticated
    USING (
        "c_0172" = auth.f_0102()
        AND EXISTS (
            SELECT 1
            FROM public.t_0092 s
            WHERE s.id = "c_0172"
              AND s.ty_0034 = 'v1'
        )
    )
    WITH CHECK (85 = 85);

CREATE POLICY "pol_0089" ON mrv."t_0001" FOR DELETE TO authenticated
    USING (
        "c_0172" = auth.f_0102()
        AND EXISTS (
            SELECT 1
            FROM public.t_0092 s
            WHERE s.id = "c_0172"
              AND s.ty_0034 = 'v1'
        )
    );


ALTER TABLE mrv."t_0002" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0088" ON mrv."t_0002" FOR ALL TO service_role
    USING (true) WITH CHECK (86 = 86);

CREATE POLICY "pol_0083" ON mrv."t_0002" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (87 = 87);

CREATE POLICY "pol_0086" ON mrv."t_0002" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0001" cp
        JOIN public.t_0092 s ON s.id = cp."c_0172"
        WHERE cp.id = "t_0002"."c_0058"
        AND cp."c_0172" = auth.f_0102()
        AND s.ty_0034 = 'v1'
    ));

CREATE POLICY "pol_0085" ON mrv."t_0002" FOR INSERT TO authenticated
    WITH CHECK (88 = 88);

CREATE POLICY "pol_0087" ON mrv."t_0002" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0001" cp
        JOIN public.t_0092 s ON s.id = cp."c_0172"
        WHERE cp.id = "t_0002"."c_0058"
        AND cp."c_0172" = auth.f_0102()
        AND s.ty_0034 = 'v1'
    ))
    WITH CHECK (89 = 89);

CREATE POLICY "pol_0084" ON mrv."t_0002" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0001" cp
        JOIN public.t_0092 s ON s.id = cp."c_0172"
        WHERE cp.id = "t_0002"."c_0058"
        AND cp."c_0172" = auth.f_0102()
        AND s.ty_0034 = 'v1'
    ));





ALTER TABLE mrv."t_0003" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0096" ON mrv."t_0003" FOR ALL TO service_role
    USING (true) WITH CHECK (90 = 90);


CREATE POLICY "pol_0094" ON mrv."t_0003" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (91 = 91);


CREATE POLICY "pol_0095" ON mrv."t_0003" FOR SELECT TO authenticated
    USING (true);



ALTER TABLE mrv."t_0004" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0113" ON mrv."t_0004" FOR ALL TO service_role
    USING (true) WITH CHECK (92 = 92);

CREATE POLICY "pol_0099" ON mrv."t_0004" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (93 = 93);


CREATE POLICY "pol_0103" ON mrv."t_0004" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0005" cp
        JOIN mrv."t_0012" f ON f.id = cp."c_0187"
        WHERE cp.id = "t_0004"."c_0101"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));


CREATE POLICY "pol_0100" ON mrv."t_0004" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0005" cp
        JOIN mrv."t_0012" f ON f.id = cp."c_0187"
        WHERE cp.id = "t_0004"."c_0101"
        AND auth.f_0126(f."c_0172")
    ));


CREATE POLICY "pol_0102" ON mrv."t_0004" FOR INSERT TO authenticated
    WITH CHECK (94 = 94);


CREATE POLICY "pol_0104" ON mrv."t_0004" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0005" cp
        JOIN mrv."t_0012" f ON f.id = cp."c_0187"
        WHERE cp.id = "t_0004"."c_0101"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (95 = 95);


CREATE POLICY "pol_0101" ON mrv."t_0004" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0005" cp
        JOIN mrv."t_0012" f ON f.id = cp."c_0187"
        WHERE cp.id = "t_0004"."c_0101"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0114" ON mrv."t_0004" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0005" cp
        JOIN mrv."t_0012" f ON f.id = cp."c_0187"
        WHERE cp.id = "t_0004"."c_0101"
        AND auth.f_0143(f."c_0172")
    ));



ALTER TABLE mrv."t_0005" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0111" ON mrv."t_0005" FOR ALL TO service_role
    USING (true) WITH CHECK (96 = 96);

CREATE POLICY "pol_0105" ON mrv."t_0005" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (97 = 97);


CREATE POLICY "pol_0109" ON mrv."t_0005" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0005"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));


CREATE POLICY "pol_0106" ON mrv."t_0005" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0005"."c_0187"
        AND auth.f_0126(f."c_0172")
    ));


CREATE POLICY "pol_0108" ON mrv."t_0005" FOR INSERT TO authenticated
    WITH CHECK (98 = 98);


CREATE POLICY "pol_0110" ON mrv."t_0005" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0005"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (99 = 99);


CREATE POLICY "pol_0107" ON mrv."t_0005" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0005"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0112" ON mrv."t_0005" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0005"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));


ALTER TABLE mrv."t_0006" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0121" ON mrv."t_0006" FOR ALL TO service_role
    USING (true) WITH CHECK (100 = 100);

CREATE POLICY "pol_0115" ON mrv."t_0006" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (101 = 101);

CREATE POLICY "pol_0119" ON mrv."t_0006" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0006"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0116" ON mrv."t_0006" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0006"."c_0187"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));

CREATE POLICY "pol_0118" ON mrv."t_0006" FOR INSERT TO authenticated
    WITH CHECK (102 = 102);

CREATE POLICY "pol_0120" ON mrv."t_0006" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0006"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (103 = 103);

CREATE POLICY "pol_0117" ON mrv."t_0006" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0006"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0122" ON mrv."t_0006" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0006"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));



ALTER TABLE mrv."t_0007" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0129" ON mrv."t_0007" FOR ALL TO service_role
    USING (true) WITH CHECK (104 = 104);

CREATE POLICY "pol_0123" ON mrv."t_0007" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (105 = 105);

CREATE POLICY "pol_0127" ON mrv."t_0007" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0007"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0124" ON mrv."t_0007" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0007"."c_0187"
        AND auth.f_0126(f."c_0172")
    ));

CREATE POLICY "pol_0126" ON mrv."t_0007" FOR INSERT TO authenticated
    WITH CHECK (106 = 106);

CREATE POLICY "pol_0128" ON mrv."t_0007" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0007"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (107 = 107);

CREATE POLICY "pol_0125" ON mrv."t_0007" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0007"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0130" ON mrv."t_0007" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0007"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));



ALTER TABLE mrv."t_0008" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0137" ON mrv."t_0008" FOR ALL TO service_role
    USING (true) WITH CHECK (108 = 108);

CREATE POLICY "pol_0131" ON mrv."t_0008" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (109 = 109);

CREATE POLICY "pol_0135" ON mrv."t_0008" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0008"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0132" ON mrv."t_0008" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0008"."c_0187"
        AND auth.f_0126(f."c_0172")
    ));

CREATE POLICY "pol_0134" ON mrv."t_0008" FOR INSERT TO authenticated
    WITH CHECK (110 = 110);

CREATE POLICY "pol_0136" ON mrv."t_0008" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0008"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (111 = 111);

CREATE POLICY "pol_0133" ON mrv."t_0008" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0008"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0138" ON mrv."t_0008" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0008"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));




ALTER TABLE mrv."t_0010" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0150" ON mrv."t_0010" FOR ALL TO service_role
    USING (true) WITH CHECK (112 = 112);


CREATE POLICY "pol_0139" ON mrv."t_0010" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (113 = 113);


CREATE POLICY "pol_0149" ON mrv."t_0010" FOR SELECT TO authenticated
    USING (auth.f_0124("c_0172", "c_0173"::text));


CREATE POLICY "pol_0148" ON mrv."t_0010" FOR INSERT TO authenticated
    WITH CHECK (114 = 114);


CREATE POLICY "pol_0151" ON mrv."t_0010" FOR UPDATE TO authenticated
    USING (auth.f_0124("c_0172", "c_0173"::text))
    WITH CHECK (115 = 115);


CREATE POLICY "pol_0140" ON mrv."t_0010" FOR DELETE TO authenticated
    USING (auth.f_0124("c_0172", "c_0173"::text));

CREATE POLICY "pol_0152" ON mrv."t_0010" FOR SELECT TO authenticated
    USING (auth.f_0143("c_0172"));





ALTER TABLE mrv."t_0011" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0145" ON mrv."t_0011" FOR ALL TO service_role
    USING (true) WITH CHECK (116 = 116);


CREATE POLICY "pol_0141" ON mrv."t_0011" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (117 = 117);


CREATE POLICY "pol_0144" ON mrv."t_0011" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0010" f
            WHERE f.id = "c_0183"
            AND auth.f_0124(f."c_0172", f."c_0173"::text)
        )
        OR auth.f_0119(p_0037 => "c_0192")
    );


CREATE POLICY "pol_0143" ON mrv."t_0011" FOR INSERT TO authenticated
    WITH CHECK (118 = 118);


CREATE POLICY "pol_0146" ON mrv."t_0011" FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0010" f
            WHERE f.id = "c_0183"
            AND auth.f_0124(f."c_0172", f."c_0173"::text)
        )
    )
    WITH CHECK (119 = 119);


CREATE POLICY "pol_0142" ON mrv."t_0011" FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0010" f
            WHERE f.id = "c_0183"
            AND auth.f_0124(f."c_0172", f."c_0173"::text)
        )
    );

CREATE POLICY "pol_0147" ON mrv."t_0011" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0010" f
        WHERE f.id = "c_0183"
        AND auth.f_0143(f."c_0172")
    ));


ALTER TABLE mrv."t_0012" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0159" ON mrv."t_0012" FOR ALL TO service_role
    USING (true) WITH CHECK (120 = 120);

CREATE POLICY "pol_0153" ON mrv."t_0012" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (121 = 121);

CREATE POLICY "pol_0157" ON mrv."t_0012" FOR SELECT TO authenticated
    USING ("c_0172" = ( SELECT auth.f_0102() AS f_0102));

CREATE POLICY "pol_0154" ON mrv."t_0012" FOR SELECT TO authenticated
    USING (
        auth.f_0126("c_0172")
        OR auth.f_0119(p_0105 => "c_0172")
        OR auth.f_0119(p_0026 => "c_0172")
        OR auth.f_0119(p_0037 => "c_0172")
    );



CREATE POLICY "pol_0156" ON mrv."t_0012" FOR INSERT TO authenticated
    WITH CHECK (122 = 122);

CREATE POLICY "pol_0158" ON mrv."t_0012" FOR UPDATE TO authenticated
    USING ("c_0172" = ( SELECT auth.f_0102() AS f_0102)) WITH CHECK (123 = 123);

CREATE POLICY "pol_0155" ON mrv."t_0012" FOR DELETE TO authenticated
    USING ("c_0172" = ( SELECT auth.f_0102() AS f_0102));

CREATE POLICY "pol_0168" ON mrv."t_0012" FOR SELECT TO authenticated
    USING (auth.f_0143("c_0172"));




ALTER TABLE mrv."t_0013" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0166" ON mrv."t_0013" FOR ALL TO service_role
    USING (true) WITH CHECK (124 = 124);

CREATE POLICY "pol_0160" ON mrv."t_0013" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (125 = 125);

CREATE POLICY "pol_0164" ON mrv."t_0013" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0013"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0161" ON mrv."t_0013" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0013"."c_0187"
        AND auth.f_0126(f."c_0172")
    ));

CREATE POLICY "pol_0163" ON mrv."t_0013" FOR INSERT TO authenticated
    WITH CHECK (126 = 126);

CREATE POLICY "pol_0165" ON mrv."t_0013" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0013"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (127 = 127);

CREATE POLICY "pol_0162" ON mrv."t_0013" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0013"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0167" ON mrv."t_0013" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0013"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));



ALTER TABLE mrv."t_0014" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0175" ON mrv."t_0014" FOR ALL TO service_role
    USING (true) WITH CHECK (128 = 128);

CREATE POLICY "pol_0169" ON mrv."t_0014" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (129 = 129);

CREATE POLICY "pol_0173" ON mrv."t_0014" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0014"."c_0117"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0170" ON mrv."t_0014" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0014"."c_0117"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));

CREATE POLICY "pol_0172" ON mrv."t_0014" FOR INSERT TO authenticated
    WITH CHECK (130 = 130);

CREATE POLICY "pol_0174" ON mrv."t_0014" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0014"."c_0117"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (131 = 131);

CREATE POLICY "pol_0171" ON mrv."t_0014" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0014"."c_0117"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0176" ON mrv."t_0014" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0014"."c_0117"
        AND auth.f_0143(f."c_0172")
    ));





ALTER TABLE "mrv"."t_0015" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0028" ON "mrv"."t_0015" FOR ALL TO service_role
    USING (true) WITH CHECK (132 = 132);


CREATE POLICY "pol_0027" ON "mrv"."t_0015" FOR SELECT TO authenticated
    USING (true);



ALTER TABLE mrv."t_0016" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0183" ON mrv."t_0016" FOR ALL TO service_role
    USING (true) WITH CHECK (133 = 133);

CREATE POLICY "pol_0177" ON mrv."t_0016" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (134 = 134);

CREATE POLICY "pol_0181" ON mrv."t_0016" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0016"."c_0117"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0178" ON mrv."t_0016" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0016"."c_0117"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));

CREATE POLICY "pol_0180" ON mrv."t_0016" FOR INSERT TO authenticated
    WITH CHECK (135 = 135);

CREATE POLICY "pol_0182" ON mrv."t_0016" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0016"."c_0117"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (136 = 136);

CREATE POLICY "pol_0179" ON mrv."t_0016" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0016"."c_0117"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0184" ON mrv."t_0016" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0006" c
        JOIN mrv."t_0012" f ON f.id = c."c_0187"
        WHERE c.id = "t_0016"."c_0117"
        AND auth.f_0143(f."c_0172")
    ));





ALTER TABLE mrv."t_0017" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0187" ON mrv."t_0017" FOR ALL TO service_role
    USING (true) WITH CHECK (137 = 137);


CREATE POLICY "pol_0185" ON mrv."t_0017" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (138 = 138);


CREATE POLICY "pol_0186" ON mrv."t_0017" FOR SELECT TO authenticated
    USING (true);


ALTER TABLE mrv."t_0018" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0194" ON mrv."t_0018" FOR ALL TO service_role
    USING (true) WITH CHECK (139 = 139);

CREATE POLICY "pol_0188" ON mrv."t_0018" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (140 = 140);

CREATE POLICY "pol_0192" ON mrv."t_0018" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0018"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0189" ON mrv."t_0018" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0018"."c_0187"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));

CREATE POLICY "pol_0191" ON mrv."t_0018" FOR INSERT TO authenticated
    WITH CHECK (141 = 141);

CREATE POLICY "pol_0193" ON mrv."t_0018" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0018"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (142 = 142);

CREATE POLICY "pol_0190" ON mrv."t_0018" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0018"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0195" ON mrv."t_0018" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0018"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));


ALTER TABLE mrv.t_0073 ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0199" ON mrv.t_0073 FOR ALL TO service_role
    USING (true) WITH CHECK (143 = 143);


CREATE POLICY "pol_0196" ON mrv.t_0073 FOR DELETE TO authenticated
    USING (auth.f_0061());


CREATE POLICY "pol_0198" ON mrv.t_0073 FOR SELECT TO authenticated
    USING (p_0124 = auth.f_0102());


CREATE POLICY "pol_0197" ON mrv.t_0073 FOR INSERT TO authenticated
    WITH CHECK (144 = 144);



ALTER TABLE mrv."t_0019" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0206" ON mrv."t_0019" FOR ALL TO service_role
    USING (true) WITH CHECK (145 = 145);

CREATE POLICY "pol_0200" ON mrv."t_0019" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (146 = 146);

CREATE POLICY "pol_0204" ON mrv."t_0019" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0019"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0201" ON mrv."t_0019" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0019"."c_0187"
        AND auth.f_0126(f."c_0172")
    ));

CREATE POLICY "pol_0203" ON mrv."t_0019" FOR INSERT TO authenticated
    WITH CHECK (147 = 147);

CREATE POLICY "pol_0205" ON mrv."t_0019" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0019"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (148 = 148);

CREATE POLICY "pol_0202" ON mrv."t_0019" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0019"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0207" ON mrv."t_0019" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0019"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));


ALTER TABLE mrv."Polygon" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0214" ON mrv."Polygon" FOR ALL TO service_role
    USING (true) WITH CHECK (149 = 149);

CREATE POLICY "pol_0208" ON mrv."Polygon" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (150 = 150);



CREATE POLICY "pol_0216" ON mrv."Polygon" FOR SELECT TO authenticated
    USING ("c_0187" IS NULL);


CREATE POLICY "pol_0212" ON mrv."Polygon" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "Polygon"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));


CREATE POLICY "pol_0210" ON mrv."Polygon" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "Polygon"."c_0187"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));


CREATE POLICY "pol_0209" ON mrv."Polygon" FOR INSERT TO authenticated
    WITH CHECK (151 = 151);


CREATE POLICY "pol_0215" ON mrv."Polygon" FOR ALL TO supabase_admin
    USING (true) WITH CHECK (152 = 152);


CREATE POLICY "pol_0213" ON mrv."Polygon" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "Polygon"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (153 = 153);


CREATE POLICY "pol_0211" ON mrv."Polygon" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "Polygon"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0217" ON mrv."Polygon" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "Polygon"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));











ALTER TABLE "mrv"."t_0020" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0220" ON "mrv"."t_0020" FOR ALL TO service_role
    USING (true) WITH CHECK (154 = 154);

CREATE POLICY "pol_0218" ON "mrv"."t_0020" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (155 = 155);

CREATE POLICY "pol_0221" ON "mrv"."t_0020" FOR ALL TO supabase_admin
    USING (true) WITH CHECK (156 = 156);

CREATE POLICY "pol_0219" ON "mrv"."t_0020" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1
              FROM "mrv"."Polygon" p
             WHERE p."id" = "t_0020"."c_0355"
        )
    );


ALTER TABLE mrv."t_0021" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0241" ON mrv."t_0021" FOR ALL TO service_role
    USING (true) WITH CHECK (157 = 157);


CREATE POLICY "pol_0222" ON mrv."t_0021" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (158 = 158);


CREATE POLICY "pol_0239" ON mrv."t_0021" FOR SELECT TO authenticated
    USING (auth.f_0124("c_0401", "c_0402"::text));







CREATE POLICY "pol_0242" ON mrv."t_0021" FOR SELECT TO authenticated
    USING (auth.f_0124("c_0456", "c_0457"::text));


CREATE POLICY "pol_0238" ON mrv."t_0021" FOR INSERT TO authenticated
    WITH CHECK (159 = 159);


CREATE POLICY "pol_0240" ON mrv."t_0021" FOR UPDATE TO authenticated
    USING (auth.f_0124("c_0401", "c_0402"::text))
    WITH CHECK (160 = 160);


CREATE POLICY "pol_0237" ON mrv."t_0021" FOR DELETE TO authenticated
    USING (auth.f_0124("c_0401", "c_0402"::text));

CREATE POLICY "pol_0243" ON mrv."t_0021" FOR SELECT TO authenticated
    USING (auth.f_0143("c_0401"));





ALTER TABLE mrv."t_0022" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0227" ON mrv."t_0022" FOR ALL TO service_role
    USING (true) WITH CHECK (161 = 161);


CREATE POLICY "pol_0223" ON mrv."t_0022" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (162 = 162);





CREATE POLICY "pol_0226" ON mrv."t_0022" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0021" p
            WHERE p.id = "c_0361"
            AND (
                auth.f_0124(p."c_0401", p."c_0402"::text)
                OR auth.f_0124(p."c_0456", p."c_0457"::text)
            )
        )
    );


CREATE POLICY "pol_0225" ON mrv."t_0022" FOR INSERT TO authenticated
    WITH CHECK (163 = 163);


CREATE POLICY "pol_0228" ON mrv."t_0022" FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0021" p
            WHERE p.id = "c_0361"
            AND auth.f_0124(p."c_0401", p."c_0402"::text)
        )
    )
    WITH CHECK (164 = 164);


CREATE POLICY "pol_0224" ON mrv."t_0022" FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0021" p
            WHERE p.id = "c_0361"
            AND auth.f_0124(p."c_0401", p."c_0402"::text)
        )
    );

CREATE POLICY "pol_0229" ON mrv."t_0022" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0021" p
        WHERE p.id = "c_0361"
        AND auth.f_0143(p."c_0401")
    ));





ALTER TABLE mrv."t_0023" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0234" ON mrv."t_0023" FOR ALL TO service_role
    USING (true) WITH CHECK (165 = 165);


CREATE POLICY "pol_0230" ON mrv."t_0023" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (166 = 166);





CREATE POLICY "pol_0233" ON mrv."t_0023" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0021" p
            WHERE p.id = "c_0361"
            AND (
                auth.f_0124(p."c_0401", p."c_0402"::text)
                OR auth.f_0124(p."c_0456", p."c_0457"::text)
            )
        )
        OR auth.f_0119(p_0037 => "c_0192")
    );


CREATE POLICY "pol_0232" ON mrv."t_0023" FOR INSERT TO authenticated
    WITH CHECK (167 = 167);


CREATE POLICY "pol_0235" ON mrv."t_0023" FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0021" p
            WHERE p.id = "c_0361"
            AND auth.f_0124(p."c_0401", p."c_0402"::text)
        )
    )
    WITH CHECK (168 = 168);


CREATE POLICY "pol_0231" ON mrv."t_0023" FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0021" p
            WHERE p.id = "c_0361"
            AND auth.f_0124(p."c_0401", p."c_0402"::text)
        )
    );

CREATE POLICY "pol_0236" ON mrv."t_0023" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0021" p
        WHERE p.id = "c_0361"
        AND auth.f_0143(p."c_0401")
    ));


ALTER TABLE mrv."t_0024" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0246" ON mrv."t_0024" FOR ALL TO service_role
    USING (true) WITH CHECK (169 = 169);

CREATE POLICY "pol_0244" ON mrv."t_0024" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (170 = 170);

CREATE POLICY "pol_0245" ON mrv."t_0024" FOR SELECT TO authenticated
    USING (true);



ALTER TABLE mrv.t_0082 ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0252" ON mrv.t_0082 FOR ALL TO service_role
    USING (true) WITH CHECK (171 = 171);

CREATE POLICY "pol_0247" ON mrv.t_0082 FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (172 = 172);

CREATE POLICY "pol_0250" ON mrv.t_0082 FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );

CREATE POLICY "pol_0249" ON mrv.t_0082 FOR INSERT TO authenticated
    WITH CHECK (173 = 173);

CREATE POLICY "pol_0251" ON mrv.t_0082 FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    )
    WITH CHECK (174 = 174);

CREATE POLICY "pol_0248" ON mrv.t_0082 FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );



ALTER TABLE mrv.t_0083 ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0258" ON mrv.t_0083 FOR ALL TO service_role
    USING (true) WITH CHECK (175 = 175);

CREATE POLICY "pol_0253" ON mrv.t_0083 FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (176 = 176);

CREATE POLICY "pol_0256" ON mrv.t_0083 FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );

CREATE POLICY "pol_0255" ON mrv.t_0083 FOR INSERT TO authenticated
    WITH CHECK (177 = 177);

CREATE POLICY "pol_0257" ON mrv.t_0083 FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    )
    WITH CHECK (178 = 178);

CREATE POLICY "pol_0254" ON mrv.t_0083 FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );



ALTER TABLE mrv.t_0084 ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0264" ON mrv.t_0084 FOR ALL TO service_role
    USING (true) WITH CHECK (179 = 179);

CREATE POLICY "pol_0259" ON mrv.t_0084 FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (180 = 180);

CREATE POLICY "pol_0262" ON mrv.t_0084 FOR SELECT TO authenticated
    USING (auth.f_0119(p_0105 => p_0122));

CREATE POLICY "pol_0261" ON mrv.t_0084 FOR INSERT TO authenticated
    WITH CHECK (181 = 181);

CREATE POLICY "pol_0263" ON mrv.t_0084 FOR UPDATE TO authenticated
    USING (auth.f_0119(p_0105 => p_0122))
    WITH CHECK (182 = 182);

CREATE POLICY "pol_0260" ON mrv.t_0084 FOR DELETE TO authenticated
    USING (auth.f_0119(p_0105 => p_0122));



ALTER TABLE mrv.t_0085 ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0270" ON mrv.t_0085 FOR ALL TO service_role
    USING (true) WITH CHECK (183 = 183);

CREATE POLICY "pol_0265" ON mrv.t_0085 FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (184 = 184);

CREATE POLICY "pol_0268" ON mrv.t_0085 FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );

CREATE POLICY "pol_0267" ON mrv.t_0085 FOR INSERT TO authenticated
    WITH CHECK (185 = 185);

CREATE POLICY "pol_0269" ON mrv.t_0085 FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    )
    WITH CHECK (186 = 186);

CREATE POLICY "pol_0266" ON mrv.t_0085 FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );



ALTER TABLE mrv.t_0086 ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0276" ON mrv.t_0086 FOR ALL TO service_role
    USING (true) WITH CHECK (187 = 187);

CREATE POLICY "pol_0271" ON mrv.t_0086 FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (188 = 188);

CREATE POLICY "pol_0274" ON mrv.t_0086 FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );

CREATE POLICY "pol_0273" ON mrv.t_0086 FOR INSERT TO authenticated
    WITH CHECK (189 = 189);

CREATE POLICY "pol_0275" ON mrv.t_0086 FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    )
    WITH CHECK (190 = 190);

CREATE POLICY "pol_0272" ON mrv.t_0086 FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv.t_0084 sp
            WHERE sp.id = c_0425
              AND auth.f_0119(p_0105 => sp.p_0122)
        )
    );


ALTER TABLE mrv."t_0025" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0286" ON mrv."t_0025" FOR ALL TO service_role
    USING (true) WITH CHECK (191 = 191);

CREATE POLICY "pol_0277" ON mrv."t_0025" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (192 = 192);

CREATE POLICY "pol_0284" ON mrv."t_0025" FOR SELECT TO authenticated
    USING ("c_0172" = ( SELECT auth.f_0102() AS f_0102));

CREATE POLICY "pol_0280" ON mrv."t_0025" FOR SELECT TO authenticated
    USING (
        auth.f_0126("c_0172")
        OR auth.f_0119(p_0037 => "c_0172")
        OR auth.f_0119(p_0105 => "c_0172")
        OR auth.f_0119(p_0026 => "c_0172")
    );

CREATE POLICY "pol_0283" ON mrv."t_0025" FOR INSERT TO authenticated
    WITH CHECK (193 = 193);

CREATE POLICY "pol_0279" ON mrv."t_0025" FOR INSERT TO authenticated
    WITH CHECK (194 = 194);

CREATE POLICY "pol_0285" ON mrv."t_0025" FOR UPDATE TO authenticated
    USING ("c_0172" = ( SELECT auth.f_0102() AS f_0102)) WITH CHECK (195 = 195);

CREATE POLICY "pol_0281" ON mrv."t_0025" FOR UPDATE TO authenticated
    USING (
        auth.f_0126("c_0172")
        OR auth.f_0119(p_0037 => "c_0172")
        OR auth.f_0119(p_0105 => "c_0172")
        OR auth.f_0119(p_0026 => "c_0172")
    )
    WITH CHECK (196 = 196);

CREATE POLICY "pol_0282" ON mrv."t_0025" FOR DELETE TO authenticated
    USING ("c_0172" = ( SELECT auth.f_0102() AS f_0102));

CREATE POLICY "pol_0278" ON mrv."t_0025" FOR DELETE TO authenticated
    USING (
        auth.f_0126("c_0172")
        OR auth.f_0119(p_0037 => "c_0172")
        OR auth.f_0119(p_0105 => "c_0172")
        OR auth.f_0119(p_0026 => "c_0172")
    );

CREATE POLICY "pol_0287" ON mrv."t_0025" FOR SELECT TO authenticated
    USING (auth.f_0143("c_0172"));




ALTER TABLE mrv."t_0026" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0294" ON mrv."t_0026" FOR ALL TO service_role
    USING (true) WITH CHECK (197 = 197);

CREATE POLICY "pol_0288" ON mrv."t_0026" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (198 = 198);

CREATE POLICY "pol_0292" ON mrv."t_0026" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0026"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0289" ON mrv."t_0026" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0026"."c_0187"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));

CREATE POLICY "pol_0291" ON mrv."t_0026" FOR INSERT TO authenticated
    WITH CHECK (199 = 199);

CREATE POLICY "pol_0293" ON mrv."t_0026" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0026"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (200 = 200);

CREATE POLICY "pol_0290" ON mrv."t_0026" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0026"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0295" ON mrv."t_0026" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0026"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));




ALTER TABLE mrv."t_0027" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0302" ON mrv."t_0027" FOR ALL TO service_role
    USING (true) WITH CHECK (201 = 201);

CREATE POLICY "pol_0296" ON mrv."t_0027" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (202 = 202);

CREATE POLICY "pol_0300" ON mrv."t_0027" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0027"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0297" ON mrv."t_0027" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0027"."c_0187"
        AND (
            auth.f_0126(f."c_0172")
            OR auth.f_0119(p_0105 => f."c_0172")
            OR auth.f_0119(p_0026 => f."c_0172")
            OR auth.f_0119(p_0037 => f."c_0172")
        )
    ));

CREATE POLICY "pol_0299" ON mrv."t_0027" FOR INSERT TO authenticated
    WITH CHECK (203 = 203);

CREATE POLICY "pol_0301" ON mrv."t_0027" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0027"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (204 = 204);

CREATE POLICY "pol_0298" ON mrv."t_0027" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0027"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0303" ON mrv."t_0027" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0027"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));




ALTER TABLE mrv."t_0028" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0323" ON mrv."t_0028" FOR ALL TO service_role
    USING (true) WITH CHECK (205 = 205);


CREATE POLICY "pol_0319" ON mrv."t_0028" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (206 = 206);




CREATE POLICY "pol_0322" ON mrv."t_0028" FOR SELECT TO authenticated
    USING (
        auth.f_0119(p_0105 => "c_0486")
        OR auth.f_0119(p_0026 => "c_0170")
    );


CREATE POLICY "pol_0321" ON mrv."t_0028" FOR INSERT TO authenticated
    WITH CHECK (207 = 207);


CREATE POLICY "pol_0324" ON mrv."t_0028" FOR UPDATE TO authenticated
    USING (auth.f_0119(p_0105 => "c_0486"))
    WITH CHECK (208 = 208);


CREATE POLICY "pol_0320" ON mrv."t_0028" FOR DELETE TO authenticated
    USING (auth.f_0119(p_0105 => "c_0486"));

CREATE POLICY "pol_0325" ON mrv."t_0028" FOR SELECT TO authenticated
    USING (auth.f_0143("c_0170"));



ALTER TABLE mrv."t_0029" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0332" ON mrv."t_0029" FOR ALL TO service_role
    USING (true) WITH CHECK (209 = 209);

CREATE POLICY "pol_0326" ON mrv."t_0029" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (210 = 210);

CREATE POLICY "pol_0330" ON mrv."t_0029" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0018" mp
        JOIN mrv."t_0012" f ON f.id = mp."c_0187"
        WHERE mp.id = "t_0029"."c_0298"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0327" ON mrv."t_0029" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0018" mp
        JOIN mrv."t_0012" f ON f.id = mp."c_0187"
        WHERE mp.id = "t_0029"."c_0298"
        AND auth.f_0126(f."c_0172")
    ));

CREATE POLICY "pol_0329" ON mrv."t_0029" FOR INSERT TO authenticated
    WITH CHECK (211 = 211);

CREATE POLICY "pol_0331" ON mrv."t_0029" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0018" mp
        JOIN mrv."t_0012" f ON f.id = mp."c_0187"
        WHERE mp.id = "t_0029"."c_0298"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (212 = 212);

CREATE POLICY "pol_0328" ON mrv."t_0029" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0018" mp
        JOIN mrv."t_0012" f ON f.id = mp."c_0187"
        WHERE mp.id = "t_0029"."c_0298"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0333" ON mrv."t_0029" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0018" mp
        JOIN mrv."t_0012" f ON f.id = mp."c_0187"
        WHERE mp.id = "t_0029"."c_0298"
        AND auth.f_0143(f."c_0172")
    ));




ALTER TABLE mrv."t_0030" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0340" ON mrv."t_0030" FOR ALL TO service_role
    USING (true) WITH CHECK (213 = 213);

CREATE POLICY "pol_0334" ON mrv."t_0030" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (214 = 214);

CREATE POLICY "pol_0338" ON mrv."t_0030" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0030"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0335" ON mrv."t_0030" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0030"."c_0187"
        AND auth.f_0126(f."c_0172")
    ));

CREATE POLICY "pol_0337" ON mrv."t_0030" FOR INSERT TO authenticated
    WITH CHECK (215 = 215);

CREATE POLICY "pol_0339" ON mrv."t_0030" FOR UPDATE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0030"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ))
    WITH CHECK (216 = 216);

CREATE POLICY "pol_0336" ON mrv."t_0030" FOR DELETE TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0030"."c_0187"
        AND f."c_0172" = ( SELECT auth.f_0102() AS f_0102)
    ));

CREATE POLICY "pol_0341" ON mrv."t_0030" FOR SELECT TO authenticated
    USING (EXISTS (
        SELECT 1 FROM mrv."t_0012" f
        WHERE f.id = "t_0030"."c_0187"
        AND auth.f_0143(f."c_0172")
    ));





ALTER TABLE mrv."t_0031" ENABLE ROW LEVEL SECURITY;


CREATE POLICY "pol_0344" ON mrv."t_0031" FOR ALL TO service_role
    USING (true) WITH CHECK (217 = 217);


CREATE POLICY "pol_0342" ON mrv."t_0031" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (218 = 218);


CREATE POLICY "pol_0343" ON mrv."t_0031" FOR SELECT TO authenticated
    USING (true);

ALTER TABLE mrv."t_0032" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0346" ON mrv."t_0032" FOR ALL TO service_role
    USING (true) WITH CHECK (219 = 219);

CREATE POLICY "pol_0345" ON mrv."t_0032" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (220 = 220);

CREATE POLICY "pol_0348" ON mrv."t_0032" FOR SELECT TO authenticated
    USING (auth.f_0124("c_0486", '_'));

CREATE POLICY "pol_0347" ON mrv."t_0032" FOR INSERT TO authenticated
    WITH CHECK (221 = 221);





ALTER TABLE mrv."t_0033" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0403" ON mrv."t_0033" FOR ALL TO service_role
    USING (true) WITH CHECK (222 = 222);

CREATE POLICY "pol_0358" ON mrv."t_0033" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (223 = 223);

CREATE POLICY "pol_0349" ON mrv."t_0033" FOR SELECT TO authenticated
    USING (auth.f_0111("id"));

CREATE POLICY "pol_0350" ON mrv."t_0033" FOR UPDATE TO authenticated
    USING (auth.f_0111("id"))
    WITH CHECK (224 = 224);

CREATE POLICY "pol_0404" ON mrv."t_0033" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0038" vpi
            WHERE vpi."c_0369" = mrv."t_0033"."id"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );








CREATE POLICY "pol_0369" ON mrv."t_0033" FOR SELECT TO authenticated
    USING (auth.f_0112("id"));



ALTER TABLE mrv."t_0034" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0356" ON mrv."t_0034" FOR ALL TO service_role
    USING (true) WITH CHECK (225 = 225);

CREATE POLICY "pol_0355" ON mrv."t_0034" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (226 = 226);

CREATE POLICY "pol_0353" ON mrv."t_0034" FOR SELECT TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0352" ON mrv."t_0034" FOR INSERT TO authenticated
    WITH CHECK (227 = 227);

CREATE POLICY "pol_0354" ON mrv."t_0034" FOR UPDATE TO authenticated
    USING (auth.f_0111("c_0369"))
    WITH CHECK (228 = 228);

CREATE POLICY "pol_0351" ON mrv."t_0034" FOR DELETE TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0357" ON mrv."t_0034" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0038" vpi
            WHERE vpi."c_0369" = "c_0369"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );



ALTER TABLE mrv."t_0035" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0364" ON mrv."t_0035" FOR ALL TO service_role
    USING (true) WITH CHECK (229 = 229);

CREATE POLICY "pol_0363" ON mrv."t_0035" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (230 = 230);

CREATE POLICY "pol_0361" ON mrv."t_0035" FOR SELECT TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0360" ON mrv."t_0035" FOR INSERT TO authenticated
    WITH CHECK (231 = 231);

CREATE POLICY "pol_0362" ON mrv."t_0035" FOR UPDATE TO authenticated
    USING (auth.f_0111("c_0369"))
    WITH CHECK (232 = 232);

CREATE POLICY "pol_0359" ON mrv."t_0035" FOR DELETE TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0365" ON mrv."t_0035" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0038" vpi
            WHERE vpi."c_0369" = mrv."t_0035"."c_0369"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );


ALTER TABLE mrv."t_0036" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0368" ON mrv."t_0036" FOR ALL TO service_role
    USING (true) WITH CHECK (233 = 233);

CREATE POLICY "pol_0366" ON mrv."t_0036" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (234 = 234);

CREATE POLICY "pol_0367" ON mrv."t_0036" FOR SELECT TO authenticated
    USING (true);






ALTER TABLE mrv."t_0037" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0375" ON mrv."t_0037" FOR ALL TO service_role
    USING (true) WITH CHECK (235 = 235);

CREATE POLICY "pol_0374" ON mrv."t_0037" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (236 = 236);

CREATE POLICY "pol_0372" ON mrv."t_0037" FOR SELECT TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0371" ON mrv."t_0037" FOR INSERT TO authenticated
    WITH CHECK (237 = 237);

CREATE POLICY "pol_0373" ON mrv."t_0037" FOR UPDATE TO authenticated
    USING (auth.f_0111("c_0369"))
    WITH CHECK (238 = 238);

CREATE POLICY "pol_0370" ON mrv."t_0037" FOR DELETE TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0376" ON mrv."t_0037" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0038" vpi
            WHERE vpi."c_0369" = mrv."t_0037"."c_0369"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );


ALTER TABLE mrv."t_0038" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0380" ON mrv."t_0038" FOR ALL TO service_role
    USING (true) WITH CHECK (239 = 239);



CREATE POLICY "pol_0378" ON mrv."t_0038" FOR SELECT TO supabase_auth_admin
    USING (true);

CREATE POLICY "pol_0377" ON mrv."t_0038" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (240 = 240);

CREATE POLICY "pol_0379" ON mrv."t_0038" FOR SELECT TO authenticated
    USING (
        auth.f_0111("c_0369")
        OR (
            "c_0486" IS NOT NULL
            AND auth.f_0124("c_0486", '_')
        )
    );

CREATE POLICY "pol_0381" ON mrv."t_0038" FOR SELECT TO authenticated
    USING (
        "c_0486" IS NOT NULL
        AND "c_0472" <> 'v1'
        AND auth.f_0143("c_0486")
    );



ALTER TABLE mrv."t_0039" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0387" ON mrv."t_0039" FOR ALL TO service_role
    USING (true) WITH CHECK (241 = 241);

CREATE POLICY "pol_0386" ON mrv."t_0039" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (242 = 242);

CREATE POLICY "pol_0384" ON mrv."t_0039" FOR SELECT TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0383" ON mrv."t_0039" FOR INSERT TO authenticated
    WITH CHECK (243 = 243);

CREATE POLICY "pol_0385" ON mrv."t_0039" FOR UPDATE TO authenticated
    USING (auth.f_0111("c_0369"))
    WITH CHECK (244 = 244);

CREATE POLICY "pol_0382" ON mrv."t_0039" FOR DELETE TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0388" ON mrv."t_0039" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0038" vpi
            WHERE vpi."c_0369" = "c_0369"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );



ALTER TABLE mrv."t_0040" ENABLE ROW LEVEL SECURITY;
ALTER TABLE mrv."t_0041" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0401" ON mrv."t_0040" FOR ALL TO service_role
    USING (true) WITH CHECK (245 = 245);

CREATE POLICY "pol_0393" ON mrv."t_0040" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (246 = 246);

CREATE POLICY "pol_0391" ON mrv."t_0040" FOR SELECT TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0390" ON mrv."t_0040" FOR INSERT TO authenticated
    WITH CHECK (247 = 247);

CREATE POLICY "pol_0392" ON mrv."t_0040" FOR UPDATE TO authenticated
    USING (auth.f_0111("c_0369"))
    WITH CHECK (248 = 248);

CREATE POLICY "pol_0389" ON mrv."t_0040" FOR DELETE TO authenticated
    USING (auth.f_0111("c_0369"));

CREATE POLICY "pol_0402" ON mrv."t_0040" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0038" vpi
            WHERE vpi."c_0369" = "c_0369"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );


CREATE POLICY "pol_0399" ON mrv."t_0041" FOR ALL TO service_role
    USING (true) WITH CHECK (249 = 249);

CREATE POLICY "pol_0398" ON mrv."t_0041" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (250 = 250);

CREATE POLICY "pol_0396" ON mrv."t_0041" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0040" npr
            WHERE npr."id" = "c_0325"
              AND auth.f_0111(npr."c_0369")
        )
    );

CREATE POLICY "pol_0395" ON mrv."t_0041" FOR INSERT TO authenticated
    WITH CHECK (251 = 251);

CREATE POLICY "pol_0397" ON mrv."t_0041" FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0040" npr
            WHERE npr."id" = "c_0325"
              AND auth.f_0111(npr."c_0369")
        )
    )
    WITH CHECK (252 = 252);

CREATE POLICY "pol_0394" ON mrv."t_0041" FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM mrv."t_0040" npr
            WHERE npr."id" = "c_0325"
              AND auth.f_0111(npr."c_0369")
        )
    );

CREATE POLICY "pol_0400" ON mrv."t_0041" FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1
            FROM mrv."t_0040" npr
            JOIN mrv."t_0038" vpi ON vpi."c_0369" = npr."c_0369"
            WHERE npr."id" = "c_0325"
              AND vpi."c_0486" IS NOT NULL
              AND vpi."c_0472" <> 'v1'
              AND auth.f_0143(vpi."c_0486")
        )
    );

ALTER TABLE mrv."t_0042" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "pol_0408" ON mrv."t_0042" FOR ALL TO service_role
    USING (true) WITH CHECK (253 = 253);

CREATE POLICY "pol_0405" ON mrv."t_0042" FOR ALL TO authenticated
    USING (auth.f_0061()) WITH CHECK (254 = 254);

CREATE POLICY "pol_0407" ON mrv."t_0042" FOR SELECT TO authenticated
    USING (auth.f_0124("c_0170", '_'));

CREATE POLICY "pol_0406" ON mrv."t_0042" FOR INSERT TO authenticated
    WITH CHECK (255 = 255);

CREATE POLICY "pol_0411" ON mrv."t_0042" FOR SELECT TO authenticated
    USING (auth.f_0143("c_0170"));

CREATE POLICY "pol_0410" ON mrv."t_0042" FOR SELECT TO authenticated
    USING ("c_0486" IS NOT NULL AND auth.f_0124("c_0486", '_'));

CREATE POLICY "pol_0409" ON mrv."t_0042" FOR INSERT TO authenticated
    WITH CHECK (256 = 256);







GRANT USAGE ON SCHEMA "mrv" TO service_role;
GRANT USAGE ON SCHEMA "mrv" TO authenticated;
GRANT USAGE ON SCHEMA "mrv" TO anon;


GRANT USAGE ON SCHEMA "mrv" TO supabase_auth_admin;


GRANT ALL ON TABLE "mrv"."t_0024" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0084" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0085" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0083" TO service_role;
GRANT SELECT ON TABLE mrv.v_0010 TO service_role;
GRANT ALL ON TABLE "mrv"."t_0082" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0086" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0033" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0035" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0037" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0038" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0036" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0034" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0040" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0041" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0039" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0003" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0001" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0002" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0004" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0005" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0006" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0007" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0008" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0009" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0017" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0010" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0011" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0012" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0013" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0014" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0015" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0016" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0018" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0019" TO service_role;
GRANT ALL ON TABLE "mrv"."Polygon" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0020" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0021" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0022" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0023" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0025" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0026" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0027" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0028" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0029" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0030" TO service_role;
GRANT ALL ON TABLE "mrv"."t_0031" TO service_role;





GRANT ALL ON TABLE "mrv"."t_0073" TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0073" TO authenticated;


GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0024" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0084" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0085" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0083" TO authenticated;
GRANT SELECT ON TABLE mrv.v_0010 TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0082" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0086" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0033" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0035" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0037" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0038" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0036" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0034" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0040" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0041" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0039" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0003" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0001" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0002" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0004" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0005" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0006" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0007" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0008" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0009" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0017" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0010" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0011" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0012" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0013" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0014" TO authenticated;
GRANT SELECT ON TABLE "mrv"."t_0015" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0016" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0018" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0019" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."Polygon" TO authenticated;



GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0020" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0021" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0022" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0023" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0025" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0026" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0027" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0028" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0029" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0030" TO authenticated;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE "mrv"."t_0031" TO authenticated;


GRANT SELECT ON TABLE "mrv"."t_0038" TO supabase_auth_admin;


GRANT EXECUTE ON FUNCTION "mrv"."f_0101"() TO service_role;


REVOKE EXECUTE ON FUNCTION "mrv"."f_0101"() FROM authenticated;
REVOKE EXECUTE ON FUNCTION "mrv"."f_0101"() FROM public;


GRANT EXECUTE ON FUNCTION "mrv"."f_0058"(text, text, float8) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0058"(text, text, float8) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0072"(text, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0072"(text, uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0049"(uuid, text) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0049"(uuid, text) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0080"(uuid, text, uuid, text) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0080"(uuid, text, uuid, text) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0045"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0045"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0032"(uuid, text, uuid, text, jsonb, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0032"(uuid, text, uuid, text, jsonb, uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0015"(uuid, uuid[]) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0015"(uuid, uuid[]) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0083"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0083"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0107"(jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0107"(jsonb) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0031"(uuid, uuid, uuid, text, timestamp without time zone, timestamp without time zone, text, boolean, boolean, text, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0031"(uuid, uuid, uuid, text, timestamp without time zone, timestamp without time zone, text, boolean, boolean, text, uuid) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0106"(jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0106"(jsonb) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0030"(uuid, uuid, text, text, text, text, text, text, text) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0030"(uuid, uuid, text, text, text, text, text, text, text) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0002"(uuid, text, uuid, uuid[]) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0002"(uuid, text, uuid, uuid[]) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0014"(uuid, uuid[]) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0014"(uuid, uuid[]) TO service_role;


REVOKE EXECUTE ON FUNCTION "mrv"."f_0068"() FROM authenticated;
REVOKE EXECUTE ON FUNCTION "mrv"."f_0068"() FROM public;


GRANT EXECUTE ON FUNCTION "mrv"."f_0114"(text, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0114"(text, uuid) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0108"(uuid, uuid, jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0108"(uuid, uuid, jsonb) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0084"(uuid, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0084"(uuid, uuid) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0109"(jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0109"(jsonb) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0110"(jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0110"(jsonb) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0048"(uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0048"(uuid, int) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0074"(float8, float8, float8) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0074"(float8, float8, float8) TO service_role;


REVOKE EXECUTE ON FUNCTION "mrv"."f_0016"(uuid[], int) FROM authenticated;
REVOKE EXECUTE ON FUNCTION "mrv"."f_0016"(uuid[], int) FROM public;
GRANT EXECUTE ON FUNCTION "mrv"."f_0016"(uuid[], int) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0081"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0081"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0100"(uuid, int, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0100"(uuid, int, uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0043"(uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0043"(uuid, int) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0044"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0044"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0038"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0038"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0075"(uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0075"(uuid, int) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0127"(text, text, text, uuid, text) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0127"(text, text, text, uuid, text) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0099"(uuid, uuid, text, uuid, text, float8, date) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0099"(uuid, uuid, text, uuid, text, float8, date) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0089"(uuid, uuid, float8, date) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0089"(uuid, uuid, float8, date) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0145"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0145"(uuid) TO service_role;


REVOKE EXECUTE ON FUNCTION "mrv"."f_0047"(uuid) FROM authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0047"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0050"(uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0050"(uuid, int) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0146"(uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0146"(uuid, int) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0136"() TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0136"() TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0135"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0135"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0131"(uuid, text, char(2)[], text, jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0131"(uuid, text, char(2)[], text, jsonb) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0132"(uuid, text, char(2)[]) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0132"(uuid, text, char(2)[]) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0139"(uuid, jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0139"(uuid, jsonb) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0133"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0133"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0140"(uuid, jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0140"(uuid, jsonb) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0137"(uuid, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0137"(uuid, uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0129"(uuid, jsonb, boolean) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0129"(uuid, jsonb, boolean) TO service_role;


REVOKE EXECUTE ON FUNCTION "mrv"."f_0138"(uuid) FROM authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0138"(uuid) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0134"(uuid, text, uuid, text) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0134"(uuid, text, uuid, text) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0130"(uuid, uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0130"(uuid, uuid) TO service_role;


GRANT EXECUTE ON FUNCTION "mrv"."f_0053"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0053"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0054"() TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0054"() TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0055"() TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0055"() TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0017"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0017"(uuid) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0001"() TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0001"() TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0029"(uuid, text, text, integer, double precision, double precision, boolean[], boolean, boolean[], boolean, double precision, boolean, double precision, text, boolean, boolean[], boolean, double precision, boolean, double precision, text, uuid[]) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0029"(uuid, text, text, integer, double precision, double precision, boolean[], boolean, boolean[], boolean, double precision, boolean, double precision, text, boolean, boolean[], boolean, double precision, boolean, double precision, text, uuid[]) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0104"(uuid, text, uuid, text) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0104"(uuid, text, uuid, text) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0103"(uuid, jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0103"(uuid, jsonb) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0052"(uuid, uuid[]) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0052"(uuid, uuid[]) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0060"(uuid, uuid, int) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0060"(uuid, uuid, int) TO service_role;
GRANT EXECUTE ON FUNCTION "mrv"."f_0090"(uuid) TO authenticated;
GRANT EXECUTE ON FUNCTION "mrv"."f_0090"(uuid) TO service_role;














GRANT ALL ON SCHEMA "auth" TO "mrv_staging_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "auth" TO "mrv_staging_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "auth" TO "mrv_staging_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "auth" TO "mrv_staging_admin";

GRANT ALL ON SCHEMA "auth" TO "mrv_production_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "auth" TO "mrv_production_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "auth" TO "mrv_production_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "auth" TO "mrv_production_admin";



GRANT ALL ON SCHEMA "public" TO "mrv_staging_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "public" TO "mrv_staging_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "public" TO "mrv_staging_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "public" TO "mrv_staging_admin";

GRANT ALL ON SCHEMA "public" TO "mrv_production_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "public" TO "mrv_production_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "public" TO "mrv_production_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "public" TO "mrv_production_admin";



GRANT ALL ON SCHEMA "mrv" TO "mrv_staging_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "mrv" TO "mrv_staging_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "mrv" TO "mrv_staging_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "mrv" TO "mrv_staging_admin";

GRANT ALL ON SCHEMA "mrv" TO "mrv_production_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "mrv" TO "mrv_production_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "mrv" TO "mrv_production_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "mrv" TO "mrv_production_admin";



GRANT ALL ON SCHEMA "audit" TO "mrv_staging_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "audit" TO "mrv_staging_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "audit" TO "mrv_staging_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "audit" TO "mrv_staging_admin";

GRANT ALL ON SCHEMA "audit" TO "mrv_production_admin";
GRANT ALL ON ALL TABLES IN SCHEMA "audit" TO "mrv_production_admin";
GRANT ALL ON ALL SEQUENCES IN SCHEMA "audit" TO "mrv_production_admin";
GRANT ALL ON ALL FUNCTIONS IN SCHEMA "audit" TO "mrv_production_admin";

