-- Phase D trial setup for Cuesta Fence LLC (Tom DeVor).
-- Slug: cuesta-fence
--
-- Background: Tom paid for an annual plan (status was flipped from
-- 'trial' to 'active' by Jake before he ran the customer-setup form).
-- He attempted onboarding 2026-05-04 but the form's Submit silently
-- failed (hidden-required-field bug, fixed in commit 5cff23e). Rather
-- than have Tom redo the 37-minute form a second time, this seed lands
-- his data directly using the spreadsheet + call notes he provided.
--
-- Uses INSERT...ON CONFLICT (slug) DO UPDATE — preserves stripe_customer_id,
-- owner_user_id, plan, created_at, status (currently 'active') because
-- the SET clause only touches business_name, ghl_webhook_url,
-- branding_json, pricing_json.

-- ============================================================
-- STEP 0: VERIFY the trial row exists with the matching slug.
--          Run this SELECT first; do NOT run STEP 1 if it returns
--          0 rows or a different status/business_name than expected.
-- ============================================================
select id, slug, status, business_name, plan, stripe_customer_id, created_at
  from public.customers
 where slug = 'cuesta-fence';
-- Expected: 1 row.
--   status            = 'active' (since Jake flipped from trial)
--   plan              = 'pro'    (annual)
--   business_name     = (whatever Stripe captured at signup, will be overwritten in STEP 1)
--   stripe_customer_id = populated, do NOT lose this
--
-- If 0 rows: the trial row was created with a different slug. Find it:
--   select id, slug, status, business_name from public.customers
--    where business_name ilike '%cuesta%' or slug ilike '%cuesta%';
-- Then either rename that row's slug to 'cuesta-fence' OR change every
-- occurrence of 'cuesta-fence' below to the actual slug, and re-verify.

-- ============================================================
-- STEP 1: SEED the customer config. Idempotent via ON CONFLICT.
-- ============================================================
insert into public.customers (
  slug,
  business_name,
  status,
  plan,
  ghl_webhook_url,
  branding_json,
  pricing_json
) values (
  'cuesta-fence',
  'Cuesta Fence LLC',
  'trial',
  'basic',
  '',
  '{
    "company_name":      "Cuesta Fence LLC",
    "company_phone":     "8052961073",
    "company_email":     "info@cuestafence.com",
    "service_area":      "Central Coast, California",
    "notify_email":      "info@cuestafence.com",
    "notify_phone":      "8052961073",
    "brand_color":       "#1E5F33",
    "brand_color_light": "#3F8556",
    "logo_url":          "/estimator/cuesta-fence/logo.png",
    "logo_style":        "height:64px;width:64px;display:block;object-fit:cover;border-radius:50%",
    "warranty_badges":   ["Proudly Serving Central Coast", "Licensed, Insured, & Bonded", "3-Year Workmanship Warranty"],
    "crm": {"type": "none", "endpoint": "https://app.jobnimbus.com/api1/contacts", "api_key": ""}
  }'::jsonb,
  '{
    "gate_cost":     600,
    "gate_double":   1000,
    "teardown_rate": 7,
    "min_ft":        50,
    "min_fee":       1200,
    "concrete_lbs":  90,
    "post_depth_in": 30,
    "products": [
      {
        "key": "cedar-dog-ear",
        "name": "Cedar Dog Ear Privacy",
        "material": "Wood",
        "desc": "Cedar Fence with vertical boards. Most common pick for backyards.",
        "heights": [6],
        "default_height": 6,
        "prices_by_height": {"6": {"lo": 48, "hi": 62}},
        "enabled": true,
        "photo_url": null
      },
      {
        "key": "redwood-dog-ear",
        "name": "Redwood Dogear Privacy",
        "material": "Wood",
        "desc": "Redwood Fence with vertical boards. Most common pick for backyards.",
        "heights": [6],
        "default_height": 6,
        "prices_by_height": {"6": {"lo": 65, "hi": 80}},
        "enabled": true,
        "photo_url": null
      },
      {
        "key": "cedar-cap-trim",
        "name": "Cedar Cap and Trim Privacy",
        "material": "Wood",
        "desc": "Cedar Fence with Vertical Boards. Horizontal trim and top-cap.",
        "heights": [6],
        "default_height": 6,
        "prices_by_height": {"6": {"lo": 52, "hi": 66}},
        "enabled": true,
        "photo_url": null
      },
      {
        "key": "vinyl-privacy",
        "name": "Vinyl Privacy",
        "material": "Vinyl",
        "desc": "Standard white vinyl privacy fence, a low-maintenance affordable option.",
        "heights": [6],
        "default_height": 6,
        "prices_by_height": {"6": {"lo": 48, "hi": 56}},
        "enabled": true,
        "photo_url": null
      },
      {
        "key": "cedar-board-on-board",
        "name": "Cedar Board on Board Privacy Fence",
        "material": "Wood",
        "desc": "Cedar Fence with vertical overlapping boards. Horizontal trim and top-cap.",
        "heights": [6],
        "default_height": 6,
        "prices_by_height": {"6": {"lo": 30, "hi": 55}},
        "enabled": true,
        "photo_url": null
      },
      {
        "key": "vinyl-ranch-rail",
        "name": "Vinyl Ranch Rail",
        "material": "Vinyl",
        "desc": "A simple low cost, low maintenance option for large properties.",
        "heights": [4],
        "default_height": 4,
        "prices_by_height": {"4": {"lo": 24, "hi": 28}},
        "enabled": true,
        "photo_url": null
      },
      {
        "key": "redwood-board-on-board",
        "name": "Redwood Board on Board Privacy",
        "material": "Wood",
        "desc": "Redwood Fence with vertical overlapping boards. Horizontal trim and top-cap.",
        "heights": [6],
        "default_height": 6,
        "prices_by_height": {"6": {"lo": 100, "hi": 108}},
        "enabled": true,
        "photo_url": null
      }
    ]
  }'::jsonb
)
on conflict (slug) do update set
  business_name   = excluded.business_name,
  ghl_webhook_url = excluded.ghl_webhook_url,
  branding_json   = excluded.branding_json,
  pricing_json    = excluded.pricing_json;

-- ============================================================
-- STEP 2: CLAIM any product photos Tom uploaded during signup.
--          No-op if he uploaded none (he didn't get past the
--          form before Submit failed, but he may have attached
--          photos in earlier attempts that landed in storage).
-- ============================================================
update public.product_images
   set customer_id  = (select id from public.customers where slug = 'cuesta-fence'),
       pending_slug = null,
       claimed_at   = now()
 where pending_slug = 'cuesta-fence' and customer_id is null;

-- ============================================================
-- STEP 3: CONFIRM
-- ============================================================
-- select id, slug, status, plan, business_name, stripe_customer_id
--   from public.customers where slug = 'cuesta-fence';
-- Expected: status preserved (likely 'active'), business_name='Cuesta Fence LLC',
-- stripe_customer_id unchanged from STEP 0.
--
-- Smoke test:
--   https://nextestimateagency.com/e/cuesta-fence
