-- 0062_pipeline_refactor.sql
-- ----------------------------------------------------------------------------
-- Pipeline refactor: 7 canonical stages, doc sub-status columns, qualification
-- criteria, payment records, assigned_to ownership, expected deposit amount.
-- Dummy-data only at this point; legacy-to-new migration for prod cutover is
-- a separate one-shot tool to be written when production data is ready.

-- ─── interests: new columns ────────────────────────────────────────────────

ALTER TABLE interests
  ADD COLUMN IF NOT EXISTS assigned_to text REFERENCES user_profiles(user_id),
  ADD COLUMN IF NOT EXISTS deposit_expected_amount numeric,
  ADD COLUMN IF NOT EXISTS deposit_expected_currency text DEFAULT 'EUR',
  ADD COLUMN IF NOT EXISTS eoi_doc_status text,
  ADD COLUMN IF NOT EXISTS reservation_doc_status text,
  ADD COLUMN IF NOT EXISTS contract_doc_status text,
  ADD COLUMN IF NOT EXISTS reservation_documenso_id text,
  ADD COLUMN IF NOT EXISTS contract_documenso_id text,
  ADD COLUMN IF NOT EXISTS date_reservation_signed timestamptz;

CREATE INDEX IF NOT EXISTS idx_interests_assigned_to ON interests (assigned_to);

-- ─── stage value migration (collapse Sent/Signed pairs) ────────────────────
-- Dummy-data only - destructive UPDATE is safe.

UPDATE interests SET pipeline_stage = 'enquiry'
  WHERE pipeline_stage IN ('open', 'details_sent', 'in_communication');

UPDATE interests
  SET pipeline_stage = 'eoi', eoi_doc_status = 'sent'
  WHERE pipeline_stage = 'eoi_sent';

UPDATE interests
  SET pipeline_stage = 'eoi', eoi_doc_status = 'signed'
  WHERE pipeline_stage = 'eoi_signed';

UPDATE interests SET pipeline_stage = 'deposit_paid'
  WHERE pipeline_stage = 'deposit_10pct';

UPDATE interests
  SET pipeline_stage = 'contract', contract_doc_status = 'sent'
  WHERE pipeline_stage = 'contract_sent';

UPDATE interests
  SET pipeline_stage = 'contract', contract_doc_status = 'signed'
  WHERE pipeline_stage = 'contract_signed';

-- `completed` collapses into contract+signed+won (the old terminal stage
-- always implied outcome=won; outcome field carries that forward).
UPDATE interests
  SET pipeline_stage = 'contract',
      contract_doc_status = 'signed',
      outcome = COALESCE(outcome, 'won'),
      outcome_at = COALESCE(outcome_at, updated_at)
  WHERE pipeline_stage = 'completed';

-- ─── Qualification criteria (per-port, admin-configurable) ──────────────────

CREATE TABLE IF NOT EXISTS qualification_criteria (
  id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
  port_id text NOT NULL REFERENCES ports(id) ON DELETE CASCADE,
  key text NOT NULL,
  label text NOT NULL,
  description text,
  enabled boolean NOT NULL DEFAULT true,
  display_order int NOT NULL DEFAULT 0,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now(),
  UNIQUE (port_id, key)
);

CREATE INDEX IF NOT EXISTS idx_qualification_criteria_port
  ON qualification_criteria (port_id);

-- Seed the default criteria for every existing port. Port admins can later
-- enable/disable or add port-specific entries.
INSERT INTO qualification_criteria (port_id, key, label, description, enabled, display_order)
  SELECT p.id, 'dimensions', 'Dimensions confirmed',
    'We know the vessel''s length, width, and draft.', true, 1
  FROM ports p
  ON CONFLICT (port_id, key) DO NOTHING;

INSERT INTO qualification_criteria (port_id, key, label, description, enabled, display_order)
  SELECT p.id, 'intent', 'Intent confirmed',
    'Client has explicitly confirmed they want a berth at this marina.', true, 2
  FROM ports p
  ON CONFLICT (port_id, key) DO NOTHING;

-- These are built but disabled - admins enable per port when relevant.
INSERT INTO qualification_criteria (port_id, key, label, description, enabled, display_order)
  SELECT p.id, 'signatory', 'Buyer signatory confirmed',
    'We know who is authorized to sign the EOI on the buyer side (owner / lawyer / company rep).',
    false, 3
  FROM ports p
  ON CONFLICT (port_id, key) DO NOTHING;

INSERT INTO qualification_criteria (port_id, key, label, description, enabled, display_order)
  SELECT p.id, 'timeline', 'Move-in timeline confirmed',
    'Client has indicated when they want to start using the berth.',
    false, 4
  FROM ports p
  ON CONFLICT (port_id, key) DO NOTHING;

-- ─── Per-interest qualification state ──────────────────────────────────────

CREATE TABLE IF NOT EXISTS interest_qualifications (
  interest_id text NOT NULL REFERENCES interests(id) ON DELETE CASCADE,
  criterion_key text NOT NULL,
  confirmed boolean NOT NULL DEFAULT false,
  confirmed_at timestamptz,
  confirmed_by text,
  notes text,
  PRIMARY KEY (interest_id, criterion_key)
);

CREATE INDEX IF NOT EXISTS idx_interest_qualifications_interest
  ON interest_qualifications (interest_id);

-- ─── Payment records (no invoice generation) ───────────────────────────────

CREATE TABLE IF NOT EXISTS payments (
  id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
  port_id text NOT NULL REFERENCES ports(id) ON DELETE CASCADE,
  interest_id text NOT NULL REFERENCES interests(id) ON DELETE CASCADE,
  client_id text NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
  payment_type text NOT NULL,         -- 'deposit' | 'balance' | 'refund' | 'other'
  amount numeric NOT NULL,
  currency text NOT NULL DEFAULT 'EUR',
  received_at timestamptz NOT NULL,
  receipt_file_id text REFERENCES files(id),
  notes text,
  recorded_by text NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_payments_interest ON payments (interest_id);
CREATE INDEX IF NOT EXISTS idx_payments_client ON payments (client_id);
CREATE INDEX IF NOT EXISTS idx_payments_port ON payments (port_id);
CREATE INDEX IF NOT EXISTS idx_payments_type ON payments (port_id, payment_type);
