-- Migration: Add failure tracking to dispensaries
-- Tracks consecutive crawl failures and flags problematic dispensaries for review

-- Add failure tracking columns to dispensaries
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS consecutive_failures INTEGER DEFAULT 0;
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS last_failure_at TIMESTAMPTZ;
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS last_failure_reason TEXT;
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS failed_at TIMESTAMPTZ;  -- NULL = active, set = failed/suspended
ALTER TABLE dispensaries ADD COLUMN IF NOT EXISTS failure_notes TEXT;     -- Admin notes about the failure

-- Index for finding failed dispensaries
CREATE INDEX IF NOT EXISTS idx_dispensaries_failed
ON dispensaries(failed_at) WHERE failed_at IS NOT NULL;

-- Index for finding dispensaries with failures
CREATE INDEX IF NOT EXISTS idx_dispensaries_consecutive_failures
ON dispensaries(consecutive_failures) WHERE consecutive_failures > 0;

-- View for failed dispensaries (for admin dashboard)
CREATE OR REPLACE VIEW v_failed_dispensaries AS
SELECT
  id,
  name,
  city,
  state,
  menu_url,
  menu_type,
  platform_dispensary_id,
  consecutive_failures,
  last_failure_at,
  last_failure_reason,
  failed_at,
  failure_notes,
  last_crawl_at,
  updated_at
FROM dispensaries
WHERE failed_at IS NOT NULL
ORDER BY failed_at DESC;

-- View for dispensaries needing attention (high failures but not yet failed)
CREATE OR REPLACE VIEW v_dispensaries_at_risk AS
SELECT
  id,
  name,
  city,
  state,
  menu_url,
  menu_type,
  consecutive_failures,
  last_failure_at,
  last_failure_reason,
  last_crawl_at
FROM dispensaries
WHERE consecutive_failures >= 2
  AND failed_at IS NULL
ORDER BY consecutive_failures DESC, last_failure_at DESC;
