/**
 * Shared type definitions
 *
 * Add your application-specific types here.
 */

// Inertia page props types
export interface User {
  id: number;
  name: string;
  email: string;
  email_verified_at: string | null;
  has_password: boolean;
  is_admin: boolean;
  admin_role?: string | null;
  two_factor_enabled?: boolean;
  days_since_signup: number;
}

export interface Auth {
  user: User | null;
  theme?: 'light' | 'dark' | 'system';
  impersonating?: {
    admin_name: string;
  } | null;
}

export interface Features {
  billing: boolean;
  socialAuth: boolean;
  emailVerification: boolean;
  apiTokens: boolean;
  userSettings: boolean;
  notifications: boolean;
  onboarding: boolean;
  apiDocs: boolean;
  twoFactor: boolean;
  webhooks: boolean;
  admin: boolean;
}

export type SiteRole = 'owner' | 'admin' | 'editor' | 'viewer';

export interface SiteSummary {
  id: number;
  name: string;
  domain: string;
  has_gsc: boolean;
  gsc_synced: boolean;
  has_wp: boolean;
  wp_connected: boolean;
  has_analysis: boolean;
  has_reviewed_recommendation: boolean;
  has_ai_key: boolean;
  ai_available: boolean;
  current_role: SiteRole;
}

/** Minimal site shape used across page Props (id + name + domain only). */
export type SiteBasic = Pick<SiteSummary, 'id' | 'name' | 'domain'>;

export interface AiStatus {
  mode: 'bundled' | 'byok' | null;
  available: boolean;
  has_key: boolean;
  bundled_remaining: number | null;
  bundled_limit: number | null;
}

export interface AiConsent {
  has_bundled_consent: boolean;
}

export interface Limits {
  max_sites_per_user: number;
  sites_used_count: number;
  max_drafts_per_month: number | null;
  drafts_used_this_month: number;
  max_briefs_per_month: number | null;
  pro_sites: number;
  pro_drafts_per_month: number;
  pro_price: number;
}

export interface AiDefaults {
  temperature_default: number;
  temperature_min: number;
  temperature_max: number;
  top_p_default: number;
  top_p_min: number;
  top_p_max: number;
  max_output_tokens_default: number;
  max_output_tokens_min: number;
  max_output_tokens_max: number;
  allowed_models: string[];
}

export interface Branding {
  company_name: string;
  logo_url: string | null;
  primary_color: string;
  secondary_color: string;
  remove_rankwiz_branding: boolean;
  custom_domain: string | null;
}

export interface ActiveJob {
  type:
    | 'ai_draft'
    | 'analysis'
    | 'cannibalization'
    | 'topic_clustering'
    | 'freshness_analysis'
    | 'batch_ai';
  id: number;
  site_name: string;
  site_id: number;
  status: string;
  progress_percent: number;
  completed: number;
  total: number;
  estimated_seconds_remaining: number | null;
}

export interface PageProps {
  [key: string]: unknown;
  auth: Auth;
  app_url: string;
  community_url: string;
  flash: {
    success?: string;
    error?: string;
    info?: string;
    warning?: string;
    upgrade_prompt?: {
      upgrade_required: boolean;
      limit_key: string;
    };
  };
  errors: Record<string, string>;
  features: Features;
  notifications_unread_count: number;
  sites: SiteSummary[];
  limits: Limits | null;
  active_jobs: ActiveJob[];
  ai_max_batch_size: number;
  ai_defaults: AiDefaults;
  has_serp_key: boolean;
  polling_interval_ms: number;
  plan?: string;
  trial?: { active: boolean; daysRemaining: number | null };
  branding?: Branding | null;
  unacknowledged_alerts?: TrafficAlert[];
  changelog?: ChangelogData | null;
  admin_failed_job_count?: number | null;
  nps_prompt?: { type: string; day: number } | false;
  micro_survey_pending?: MicroSurveyPrompt | false;
  ai_status?: AiStatus | null;
  ai_consent?: AiConsent | null;
  grace_period_warning?: { days_remaining: number; expires_at: string } | null;
  billing_alert?: {
    status: 'past_due';
    dunning_started_at: string | null;
    days_in_dunning: number;
    portal_url: string;
  } | null;
  posthog_traits?: Record<string, unknown> | null;
}

export interface MicroSurveyRating {
  value: string;
  label: string;
}

export interface MicroSurveyPrompt {
  trigger: string;
  question: string;
  ratings: MicroSurveyRating[];
}

export interface ChangelogEntry {
  date: string;
  items: string[];
}

export interface ChangelogData {
  entries: ChangelogEntry[];
  has_new: boolean;
}

// Common pagination type
export interface PaginationLink {
  url: string | null;
  label: string;
  active: boolean;
}

export interface PaginatedResponse<T> {
  data: T[];
  current_page: number;
  last_page: number;
  per_page: number;
  total: number;
  from: number | null;
  to: number | null;
  links: PaginationLink[];
  next_page_url?: string | null;
  prev_page_url?: string | null;
}

// Notifications
export interface AppNotification {
  id: string;
  type: string;
  data: {
    title: string;
    message: string;
    action_url?: string;
    icon?: string;
  };
  read_at: string | null;
  created_at: string;
}

// Webhooks
export interface WebhookEndpoint {
  id: number;
  url: string;
  events: string[];
  description: string | null;
  active: boolean;
  secret?: string;
  deliveries_count?: number;
  created_at: string;
}

export interface WebhookDelivery {
  id: number;
  uuid: string;
  event_type: string;
  status: 'pending' | 'success' | 'failed';
  response_code: number | null;
  attempts: number;
  delivered_at: string | null;
  created_at: string;
}

// Form state helpers
export interface FormErrors {
  [key: string]: string;
}

// Onboarding wizard
export type OnboardingStep = 'welcome' | 'connect_gsc' | 'run_analysis' | 'review_results';

export interface OnboardingWizardProgress {
  current_step: OnboardingStep;
  completed_steps: OnboardingStep[];
  is_completed: boolean;
  can_skip: boolean;
}

export interface OnboardingWizardState {
  progress: OnboardingWizardProgress;
  site: {
    id: number;
    name: string;
    domain: string;
    has_gsc_connection: boolean;
    has_wp_connection: boolean;
    latest_analysis_run_id: number | null;
  };
}

// Topic clustering
export interface TopicQuery {
  query: string;
  impressions: number;
  clicks: number;
  ctr: number;
  position: number;
}

export interface Topic {
  id: number;
  name: string;
  query_count: number;
  total_impressions: number;
  total_clicks: number;
  avg_ctr: number;
  avg_position: number;
  queries: TopicQuery[];
}

export interface TopicClusteringResult {
  topics: Topic[];
  total_queries: number;
  total_queries_clustered: number;
  method: string;
  created_at: string;
}

// Traffic Alerts
export interface TrafficAlert {
  id: number;
  site_id: number;
  analysis_run_id?: number | null;
  severity: 'critical' | 'warning' | 'info';
  anomaly_type: 'sudden_drop' | 'sustained_decline' | 'unusual_spike';
  status: 'unacknowledged' | 'acknowledged' | 'dismissed';
  page_url: string;
  metrics: {
    clicks_before: number;
    clicks_after: number;
    delta_percent: number;
    impressions_before?: number;
    impressions_after?: number;
    ctr_before?: number;
    ctr_after?: number;
    position_before?: number;
    position_after?: number;
  };
  acknowledged_at?: string | null;
  dismissed_at?: string | null;
  created_at: string;
}
