-- Phase 3 - EOI field override foundation (POST-AUDIT-SPEC §1 + MASTER-PLAN §C).
--
-- This migration is foundation-only. The EOI generate-and-sign endpoint
-- + the dialog UI extensions land in subsequent sub-sessions (3a-3d).
--
-- Adds:
--   1. client_contacts.{source, source_document_id}
--   2. client_addresses.{source, source_document_id}
--   3. yachts.{source, source_document_id}
--   4. documents.override_* columns mirroring the AcroForm field map
--
-- audit_actions is a free-text column (text, not enum) - new action verbs
-- 'eoi_field_override', 'promote_to_primary', 'eoi_spawn_yacht' don't
-- need DDL; they just appear in inserted rows.

-- ─── client_contacts ──────────────────────────────────────────────────
ALTER TABLE client_contacts
  ADD COLUMN IF NOT EXISTS source text NOT NULL DEFAULT 'manual',
  ADD COLUMN IF NOT EXISTS source_document_id text;

-- Add FK only if it doesn't exist yet. ON DELETE SET NULL - keep the
-- contact row around even if the originating document is deleted.
DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'client_contacts_source_document_id_fkey') THEN
    ALTER TABLE client_contacts
      ADD CONSTRAINT client_contacts_source_document_id_fkey
      FOREIGN KEY (source_document_id) REFERENCES documents(id) ON DELETE SET NULL;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'chk_client_contacts_source') THEN
    ALTER TABLE client_contacts
      ADD CONSTRAINT chk_client_contacts_source
      CHECK (source IN ('manual', 'imported', 'eoi-custom-input'));
  END IF;
END $$;

-- ─── client_addresses ──────────────────────────────────────────────────
ALTER TABLE client_addresses
  ADD COLUMN IF NOT EXISTS source text NOT NULL DEFAULT 'manual',
  ADD COLUMN IF NOT EXISTS source_document_id text;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'client_addresses_source_document_id_fkey') THEN
    ALTER TABLE client_addresses
      ADD CONSTRAINT client_addresses_source_document_id_fkey
      FOREIGN KEY (source_document_id) REFERENCES documents(id) ON DELETE SET NULL;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'chk_client_addresses_source') THEN
    ALTER TABLE client_addresses
      ADD CONSTRAINT chk_client_addresses_source
      CHECK (source IN ('manual', 'imported', 'eoi-custom-input'));
  END IF;
END $$;

-- ─── yachts ────────────────────────────────────────────────────────────
ALTER TABLE yachts
  ADD COLUMN IF NOT EXISTS source text NOT NULL DEFAULT 'manual',
  ADD COLUMN IF NOT EXISTS source_document_id text;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'yachts_source_document_id_fkey') THEN
    ALTER TABLE yachts
      ADD CONSTRAINT yachts_source_document_id_fkey
      FOREIGN KEY (source_document_id) REFERENCES documents(id) ON DELETE SET NULL;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'chk_yachts_source') THEN
    ALTER TABLE yachts
      ADD CONSTRAINT chk_yachts_source
      CHECK (source IN ('manual', 'imported', 'eoi-generated'));
  END IF;
END $$;

-- ─── documents - per-document overrides ────────────────────────────────
-- These columns mirror the AcroForm field set per
-- docs/eoi-documenso-field-mapping.md. When NULL, the canonical
-- client/yacht values flow through unchanged. When set, the EOI uses
-- the override value without touching the underlying record.
ALTER TABLE documents
  ADD COLUMN IF NOT EXISTS override_client_email text,
  ADD COLUMN IF NOT EXISTS override_client_phone text,
  ADD COLUMN IF NOT EXISTS override_client_address_line_1 text,
  ADD COLUMN IF NOT EXISTS override_client_address_line_2 text,
  ADD COLUMN IF NOT EXISTS override_client_city text,
  ADD COLUMN IF NOT EXISTS override_client_state text,
  ADD COLUMN IF NOT EXISTS override_client_postal_code text,
  ADD COLUMN IF NOT EXISTS override_client_country text,
  ADD COLUMN IF NOT EXISTS override_yacht_name text,
  ADD COLUMN IF NOT EXISTS override_yacht_length_ft numeric(10, 2),
  ADD COLUMN IF NOT EXISTS override_yacht_width_ft numeric(10, 2),
  ADD COLUMN IF NOT EXISTS override_yacht_draft_ft numeric(10, 2);

-- ─── Comments ──────────────────────────────────────────────────────────
COMMENT ON COLUMN client_contacts.source IS
  'Phase 3: origin of this contact row. manual = rep typed it; imported = bulk import; eoi-custom-input = inline-typed during EOI generation.';
COMMENT ON COLUMN client_contacts.source_document_id IS
  'Phase 3: when source=eoi-custom-input, points at the EOI that created it.';
COMMENT ON COLUMN yachts.source IS
  'Phase 3: origin. eoi-generated = spawned from the EOI dialog''s "+ New yacht" inline form.';
COMMENT ON COLUMN documents.override_client_email IS
  'Phase 3: per-document override of the client''s email field on this EOI. NULL = use canonical client_contacts row.';
