-- Migration 026: Update dispensary_crawl_status view
-- Use provider_type and scrape_enabled from dispensaries table (migration 025)

DROP VIEW IF EXISTS dispensary_crawl_status;

CREATE OR REPLACE VIEW dispensary_crawl_status AS
SELECT
  d.id AS dispensary_id,
  COALESCE(d.dba_name, d.name) AS dispensary_name,
  d.city,
  d.state,
  d.slug,
  d.website,
  d.menu_url,
  -- Use provider_type from 025 if product_provider is null
  COALESCE(d.product_provider, d.provider_type) AS product_provider,
  d.provider_type,
  d.product_confidence,
  d.product_crawler_mode,
  d.last_product_scan_at,
  -- Use scrape_enabled from 025 if no schedule exists
  COALESCE(dcs.is_active, d.scrape_enabled, FALSE) AS schedule_active,
  COALESCE(dcs.interval_minutes, 240) AS interval_minutes,
  COALESCE(dcs.priority, 0) AS priority,
  -- Use crawl timestamps from 025 if no schedule exists
  COALESCE(dcs.last_run_at, d.last_crawl_at) AS last_run_at,
  COALESCE(dcs.next_run_at, d.next_crawl_at) AS next_run_at,
  COALESCE(dcs.last_status, d.crawl_status) AS last_status,
  dcs.last_summary,
  COALESCE(dcs.last_error, d.crawl_error) AS last_error,
  COALESCE(dcs.consecutive_failures, d.consecutive_failures, 0) AS consecutive_failures,
  COALESCE(dcs.total_runs, d.total_crawls, 0) AS total_runs,
  COALESCE(dcs.successful_runs, d.successful_crawls, 0) AS successful_runs,
  dcj.id AS latest_job_id,
  dcj.job_type AS latest_job_type,
  dcj.status AS latest_job_status,
  dcj.started_at AS latest_job_started,
  dcj.products_found AS latest_products_found
FROM dispensaries d
LEFT JOIN dispensary_crawl_schedule dcs ON dcs.dispensary_id = d.id
LEFT JOIN LATERAL (
  SELECT * FROM dispensary_crawl_jobs
  WHERE dispensary_id = d.id
  ORDER BY created_at DESC
  LIMIT 1
) dcj ON true
ORDER BY
  CASE WHEN d.scrape_enabled = TRUE THEN 0 ELSE 1 END,
  COALESCE(dcs.priority, 0) DESC,
  COALESCE(d.dba_name, d.name);

-- Grant permissions
GRANT SELECT ON dispensary_crawl_status TO dutchie;
