CREATE SCHEMA "risks";
--> statement-breakpoint
CREATE TABLE "risks"."risk_events" (
	"id" serial PRIMARY KEY NOT NULL,
	"vendor_id" integer NOT NULL,
	"provider" varchar(128) NOT NULL,
	"event_type" varchar(50) NOT NULL,
	"content" text,
	"event_status" boolean DEFAULT true NOT NULL,
	"manager_id" integer,
	"admin_comment" text,
	"occurred_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	"created_by" integer,
	"updated_by" integer
);
--> statement-breakpoint
ALTER TABLE "risks"."risk_events" ADD CONSTRAINT "risk_events_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "risks"."risk_events" ADD CONSTRAINT "risk_events_manager_id_users_id_fk" FOREIGN KEY ("manager_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "risks"."risk_events" ADD CONSTRAINT "risk_events_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "risks"."risk_events" ADD CONSTRAINT "risk_events_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE VIEW "risks"."risks_view" AS (select "risks"."risk_events"."id", "risks"."risk_events"."event_type", "risks"."risk_events"."vendor_id", "vendors"."vendor_code", "vendors"."vendor_name", "vendors"."tax_id", "risks"."risk_events"."provider", "risks"."risk_events"."content", "risks"."risk_events"."event_status", "risks"."risk_events"."manager_id", "users"."name", "risks"."risk_events"."admin_comment", "risks"."risk_events"."occurred_at" from "risks"."risk_events" left join "vendors" on "vendors"."id" = "risks"."risk_events"."vendor_id" left join "users" on "users"."id" = "risks"."risk_events"."manager_id");