/**
 * Hook for managing rating submission.
 */

import { useState, useCallback } from 'react';
import type { RatingState, DimensionKey } from '../types';
import * as api from '../services/api';

interface UseRatingsOptions {
  raterId: string | null;
  queryId: string | null;
  ideaId: string | null;
  onSuccess?: () => void;
}

export function useRatings({ raterId, queryId, ideaId, onSuccess }: UseRatingsOptions) {
  const [ratings, setRatings] = useState<RatingState>({
    originality: null,
    elaboration: null,
    coherence: null,
    usefulness: null,
  });
  const [submitting, setSubmitting] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // Set a single rating
  const setRating = useCallback((dimension: DimensionKey, value: number | null) => {
    setRatings((prev) => ({ ...prev, [dimension]: value }));
  }, []);

  // Reset all ratings
  const resetRatings = useCallback(() => {
    setRatings({
      originality: null,
      elaboration: null,
      coherence: null,
      usefulness: null,
    });
    setError(null);
  }, []);

  // Check if all ratings are set
  const isComplete = useCallback(() => {
    return (
      ratings.originality !== null &&
      ratings.elaboration !== null &&
      ratings.coherence !== null &&
      ratings.usefulness !== null
    );
  }, [ratings]);

  // Submit rating
  const submit = useCallback(async () => {
    if (!raterId || !queryId || !ideaId) {
      setError('Missing required information');
      return false;
    }

    if (!isComplete()) {
      setError('Please rate all dimensions');
      return false;
    }

    setSubmitting(true);
    setError(null);

    try {
      await api.submitRating({
        rater_id: raterId,
        idea_id: ideaId,
        query_id: queryId,
        originality: ratings.originality,
        elaboration: ratings.elaboration,
        coherence: ratings.coherence,
        usefulness: ratings.usefulness,
        skipped: false,
      });

      resetRatings();
      onSuccess?.();
      return true;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to submit rating');
      return false;
    } finally {
      setSubmitting(false);
    }
  }, [raterId, queryId, ideaId, ratings, isComplete, resetRatings, onSuccess]);

  // Skip idea
  const skip = useCallback(async () => {
    if (!raterId || !queryId || !ideaId) {
      setError('Missing required information');
      return false;
    }

    setSubmitting(true);
    setError(null);

    try {
      await api.submitRating({
        rater_id: raterId,
        idea_id: ideaId,
        query_id: queryId,
        originality: null,
        elaboration: null,
        coherence: null,
        usefulness: null,
        skipped: true,
      });

      resetRatings();
      onSuccess?.();
      return true;
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to skip idea');
      return false;
    } finally {
      setSubmitting(false);
    }
  }, [raterId, queryId, ideaId, resetRatings, onSuccess]);

  return {
    ratings,
    setRating,
    resetRatings,
    isComplete,
    submit,
    skip,
    submitting,
    error,
  };
}
