-- CEO OS: Contracts, Obligations, Pipeline Deals
-- Migration 0011_contracts_pipeline

--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "contracts" (
  "id" uuid DEFAULT gen_random_uuid() PRIMARY KEY NOT NULL,
  "tenant_id" uuid NOT NULL,
  "workspace_id" uuid,
  "organization_id" uuid,
  "title" text NOT NULL,
  "contract_type" text NOT NULL DEFAULT 'other',
  "status" text NOT NULL DEFAULT 'draft',
  "parties" text[] DEFAULT '{}' NOT NULL,
  "start_date" date,
  "end_date" date,
  "value_minor_units" bigint,
  "currency" text DEFAULT 'RON',
  "notes" text,
  "tags" text[] DEFAULT '{}' NOT NULL,
  "created_at" timestamp DEFAULT now() NOT NULL,
  "updated_at" timestamp DEFAULT now() NOT NULL,
  "deleted_at" timestamp
);

--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "obligations" (
  "id" uuid DEFAULT gen_random_uuid() PRIMARY KEY NOT NULL,
  "tenant_id" uuid NOT NULL,
  "workspace_id" uuid,
  "title" text NOT NULL,
  "description" text,
  "category" text NOT NULL DEFAULT 'contractual',
  "status" text NOT NULL DEFAULT 'pending',
  "due_date" date,
  "owner" text,
  "contract_id" uuid,
  "evidence_url" text,
  "risk_level" text NOT NULL DEFAULT 'medium',
  "created_at" timestamp DEFAULT now() NOT NULL,
  "updated_at" timestamp DEFAULT now() NOT NULL
);

--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "pipeline_deals" (
  "id" uuid DEFAULT gen_random_uuid() PRIMARY KEY NOT NULL,
  "tenant_id" uuid NOT NULL,
  "workspace_id" uuid,
  "organization_id" uuid,
  "contact_id" uuid,
  "title" text NOT NULL,
  "stage" text NOT NULL DEFAULT 'identified',
  "value_minor_units" bigint,
  "currency" text DEFAULT 'RON',
  "probability" integer DEFAULT 50,
  "expected_close_date" date,
  "notes" text,
  "tags" text[] DEFAULT '{}' NOT NULL,
  "created_at" timestamp DEFAULT now() NOT NULL,
  "updated_at" timestamp DEFAULT now() NOT NULL,
  "deleted_at" timestamp
);
