/**
 * Surveys module — React context + hook for in-app surveys in React Native apps.
 */

import React, { createContext, useContext, useState, useCallback, useEffect } from 'react';
import type { PlatformSDK } from '../core.js';

export interface Question {
  id: string;
  text: string;
  type: 'rating' | 'text' | 'choice';
  options?: string[];
  required: boolean;
}

export interface ActiveSurvey {
  id: string;
  title: string;
  description?: string;
  questions: Question[];
  expiresAt?: string;
}

interface SurveyContextType {
  activeSurvey: ActiveSurvey | null;
  submit: (surveyId: string, answers: Record<string, unknown>) => Promise<void>;
  dismiss: (surveyId: string) => void;
  refresh: () => Promise<void>;
}

const SurveyContext = createContext<SurveyContextType | null>(null);

export function useSurveys(): SurveyContextType {
  const ctx = useContext(SurveyContext);
  if (!ctx) throw new Error('useSurveys must be used within a SurveyProvider');
  return ctx;
}

interface SurveyProviderProps {
  sdk: PlatformSDK;
  /** Poll interval in ms (default: 600000 = 10 min) */
  pollInterval?: number;
  children: React.ReactNode;
}

export function SurveyProvider({
  sdk,
  pollInterval = 600_000,
  children,
}: SurveyProviderProps): React.JSX.Element {
  const [activeSurvey, setActiveSurvey] = useState<ActiveSurvey | null>(null);

  const refresh = useCallback(async () => {
    try {
      const res = await sdk.fetch('/surveys/active');
      if (!res.ok) return;
      const data = (await res.json()) as { survey?: ActiveSurvey | null };
      setActiveSurvey(data.survey ?? null);
    } catch {
      /* silent */
    }
  }, [sdk]);

  const submit = useCallback(
    async (surveyId: string, answers: Record<string, unknown>) => {
      await sdk.fetch(`/surveys/${surveyId}/start`, { method: 'POST' });
      for (const [questionId, answer] of Object.entries(answers)) {
        await sdk.fetch(`/surveys/${surveyId}/response`, {
          method: 'POST',
          body: JSON.stringify({ questionId, answer }),
        });
      }
      await sdk.fetch(`/surveys/${surveyId}/complete`, { method: 'POST', body: '{}' });
      setActiveSurvey(null);
    },
    [sdk]
  );

  const dismiss = useCallback(
    (surveyId: string) => {
      setActiveSurvey(null);
      sdk.fetch(`/surveys/${surveyId}/dismiss`, { method: 'POST' }).catch(() => {});
    },
    [sdk]
  );

  useEffect(() => {
    refresh();
    const id = setInterval(refresh, pollInterval);
    return () => clearInterval(id);
  }, [refresh, pollInterval]);

  const value: SurveyContextType = { activeSurvey, submit, dismiss, refresh };
  return React.createElement(SurveyContext.Provider, { value }, children);
}

// MARK: - UI Components

interface SurveyModalProps {
  survey: ActiveSurvey | null;
  onSubmit: (answers: Record<string, unknown>) => void;
  onDismiss: () => void;
}

/**
 * Placeholder survey modal — product apps should implement their own
 * styled version. Returns null.
 */
export function SurveyModal(_props: SurveyModalProps): React.JSX.Element | null {
  return null;
}
