-- ============================================================
-- Phase 4 additions
--   - mailconfig settings row (editable sending email in dashboard)
--   - quote columns for tracking the offer + buyer-send status
-- Run once via seed-phase4.php (key-guarded).
-- The buyers table already exists from the Phase 1 schema.
-- ============================================================

-- Editable email/sending config (falls back to config.php constants if empty)
INSERT INTO settings (name, value) VALUES
    ('mailconfig', '{}'::jsonb)
ON CONFLICT (name) DO NOTHING;

-- Track the offer amount and where the quote is in the buyer flow.
-- (offer_amount + price already partially exist; add clean dedicated columns.)
ALTER TABLE quotes ADD COLUMN IF NOT EXISTS offer_amount     NUMERIC(10,2);
ALTER TABLE quotes ADD COLUMN IF NOT EXISTS sent_to_buyer_at TIMESTAMP;
ALTER TABLE quotes ADD COLUMN IF NOT EXISTS sent_buyer_id    INTEGER REFERENCES buyers(id) ON DELETE SET NULL;
ALTER TABLE quotes ADD COLUMN IF NOT EXISTS offer_texted_at  TIMESTAMP;

-- Make sure buyers has the fields we use (all already in Phase 1 schema, but safe-guard)
ALTER TABLE buyers ADD COLUMN IF NOT EXISTS phone   VARCHAR(32);
ALTER TABLE buyers ADD COLUMN IF NOT EXISTS address VARCHAR(255);
