import type { Database } from '../../supabase/database.types'

// =============================================================================
// Supabase-Generated Types (Re-exported from database.types)
// =============================================================================

/** Raw database row type for acq_seo_pages table */
export type SeoPageRow = Database['public']['Tables']['acq_seo_pages']['Row']
/** Insert type for acq_seo_pages table */
export type SeoPageInsert = Database['public']['Tables']['acq_seo_pages']['Insert']
/** Update type for acq_seo_pages table */
export type SeoPageUpdate = Database['public']['Tables']['acq_seo_pages']['Update']

/** Raw database row type for acq_seo_metrics table */
export type SeoMetricsRow = Database['public']['Tables']['acq_seo_metrics']['Row']
/** Insert type for acq_seo_metrics table */
export type SeoMetricsInsert = Database['public']['Tables']['acq_seo_metrics']['Insert']
/** Update type for acq_seo_metrics table */
export type SeoMetricsUpdate = Database['public']['Tables']['acq_seo_metrics']['Update']

// =============================================================================
// Constants
// =============================================================================

export const SEO_PAGE_TYPES = ['location', 'use-case', 'comparison', 'pillar', 'cluster'] as const
export type SeoPageType = (typeof SEO_PAGE_TYPES)[number]

export const SEO_PAGE_STATUSES = ['draft', 'review', 'published', 'stale'] as const
export type SeoPageStatus = (typeof SEO_PAGE_STATUSES)[number]

export const SEO_VERTICALS = [
  'veterinary-clinics',
  'auto-repair',
  'home-services',
  'hvac-contractors',
  'pest-control',
  'commercial-cleaning',
  'med-spas',
  'hoa-property-management',
  'security-companies'
] as const
export type SeoVertical = (typeof SEO_VERTICALS)[number]

// =============================================================================
// Supporting Types (typed JSONB columns)
// =============================================================================

// =============================================================================
// Chart Types (for content.charts JSONB array)
// =============================================================================

interface SeoChartBase {
  type: 'bar' | 'donut' | 'metrics-row'
  title: string
  subtitle?: string
  source?: string
  sourceLabel?: string
  section: 'problem' | 'solution' | 'results' | 'use_cases' | 'how_it_works'
}

/** Bar chart data for SEO page content sections */
export interface SeoBarChart extends SeoChartBase {
  type: 'bar'
  data: Array<{ label: string; value: number; color?: string }>
  valueFormat?: string
}

/** Donut chart data for SEO page content sections */
export interface SeoDonutChart extends SeoChartBase {
  type: 'donut'
  data: Array<{ id: string; label: string; value: number; color?: string }>
}

/** Metrics row data for SEO page content sections */
export interface SeoMetricsRowChart extends SeoChartBase {
  type: 'metrics-row'
  data: Array<{ label: string; value: number; suffix: string; description?: string }>
}

/** Union of all chart types embeddable in SEO page content */
export type SeoChart = SeoBarChart | SeoDonutChart | SeoMetricsRowChart

/** The section identifier used to place a chart within a page */
export type SeoChartSection = SeoChartBase['section']

/** Structured content sections for an SEO page */
export interface SeoPageContent {
  subtitle: string
  intro?: string
  market_context?: string
  local_challenges?: string
  solution?: string
  problem?: string
  how_it_works?: Array<{ step: number; title: string; description: string }>
  use_cases?: Array<{ title: string; description: string }>
  results?: string
  methodology?: string
  baseline_assumptions?: Record<string, number>
  typical_results?: Record<string, number>
  cta?: string
  charts?: SeoChart[]
}

/** FAQ item for SEO pages */
export interface SeoFaqItem {
  question: string
  answer: string
}

/** Internal link reference */
export interface SeoInternalLink {
  slug: string
  anchor_text: string
}

/** Local market data for location pages */
export interface SeoLocalData {
  business_count?: number
  city_population?: number
  metro?: string
  median_household_income?: number
  pet_owning_households?: number
  avg_annual_pet_spend?: number
  national_avg_pet_spend?: number
  avg_commercial_rent_sqft?: number
  population?: number
  pet_ownership_rate_pct?: number
  source?: string
  [key: string]: string | number | undefined
}

/** AI citation counts per platform */
export interface SeoAiCitations {
  chatgpt?: number
  perplexity?: number
  google_aio?: number
  gemini?: number
  copilot?: number
}

// =============================================================================
// Domain Types (camelCase transformations of database rows)
// =============================================================================

/**
 * SEO page with typed JSONB fields.
 * Transformed from SeoPageRow with camelCase properties.
 */
export interface SeoPage {
  id: string
  organizationId: string
  pageType: SeoPageType
  vertical: SeoVertical
  city: string | null
  state: string | null
  useCase: string | null
  slug: string
  title: string
  metaDescription: string | null
  content: SeoPageContent | null
  faqItems: SeoFaqItem[] | null
  schemaMarkup: Record<string, unknown> | null
  localData: SeoLocalData | null
  internalLinks: SeoInternalLink[] | null
  heroImageUrl: string | null
  status: SeoPageStatus
  publishedAt: Date | null
  refreshedAt: Date | null
  createdAt: Date
  updatedAt: Date
}

/**
 * SEO metrics daily snapshot with typed JSONB fields.
 * Transformed from SeoMetricsRow with camelCase properties.
 */
export interface SeoMetrics {
  id: string
  organizationId: string
  seoPageId: string
  period: string
  impressions: number | null
  clicks: number | null
  ctr: number | null
  avgPosition: number | null
  ctaClicks: number | null
  formSubmissions: number | null
  scroll25: number | null
  scroll50: number | null
  scroll75: number | null
  scroll100: number | null
  aiCitations: SeoAiCitations | null
  qualityScore: number | null
  wordCount: number | null
  faqCount: number | null
  dataPointCount: number | null
  readability: number | null
  createdAt: Date
}
