import { useState } from 'react';
import { apiFetch } from '../lib/api';

type SuggestResult =
  | { ok: true }
  | { ok: false; alreadyExists: true; status: 'suggested' | 'approved' | 'rejected' }
  | { ok: false; alreadyExists?: false; errorCode?: 'INVALID_DOMAIN' | 'INVALID_COUNTRY' | string };

export type SuggestState = 'idle' | 'loading' | 'success' | 'already_suggested' | 'already_approved' | 'already_rejected' | 'invalid_domain' | 'error';

export function useCuratedSuggest() {
  const [state, setState] = useState<SuggestState>('idle');

  async function suggest(domain: string, country: string): Promise<SuggestState> {
    setState('loading');
    try {
      const res = await apiFetch<{ ok: boolean; alreadyExists?: boolean; status?: string }>(
        '/api/curated-domains/suggest',
        { method: 'POST', body: { domain, country } },
      );
      if (res.ok) {
        setState('success');
        return 'success';
      }
      if (res.alreadyExists) {
        const next: SuggestState =
          res.status === 'approved'
            ? 'already_approved'
            : res.status === 'rejected'
              ? 'already_rejected'
              : 'already_suggested';
        setState(next);
        return next;
      }
      setState('error');
      return 'error';
    } catch (e: any) {
      const code: string = (e as any)?.code ?? '';
      if (code === 'INVALID_DOMAIN') {
        setState('invalid_domain');
        return 'invalid_domain';
      }
      setState('error');
      return 'error';
    }
  }

  function reset() {
    setState('idle');
  }

  return { state, suggest, reset };
}
