import { useState, useEffect, useCallback } from 'react';
import issueService, { type Issue, type CreateIssueData, type IssueListParams } from '../services/issueService';

// Hook for fetching issue list
export const useIssueList = (params: IssueListParams = {}) => {
  const [issues, setIssues] = useState<Issue[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [totalCount, setTotalCount] = useState(0);

  const fetchIssues = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const response = await issueService.getIssues(params);
      setIssues(response.data);
      
      // Get total count for pagination
      const count = await issueService.getIssueCount(params.filters);
      setTotalCount(count);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to fetch issues');
    } finally {
      setLoading(false);
    }
  }, [JSON.stringify(params)]);

  useEffect(() => {
    fetchIssues();
  }, [fetchIssues]);

  return {
    issues,
    loading,
    error,
    totalCount,
    refetch: fetchIssues,
  };
};

// Hook for fetching single issue details
export const useIssueDetails = (issueName: string | null) => {
  const [issue, setIssue] = useState<Issue | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const fetchIssue = useCallback(async () => {
    if (!issueName) {
      setIssue(null);
      return;
    }

    try {
      setLoading(true);
      setError(null);
      const data = await issueService.getIssue(issueName);
      setIssue(data);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to fetch issue details');
    } finally {
      setLoading(false);
    }
  }, [issueName]);

  useEffect(() => {
    fetchIssue();
  }, [fetchIssue]);

  return {
    issue,
    loading,
    error,
    refetch: fetchIssue,
  };
};

// Hook for issue mutations (create, update, delete)
export const useIssueMutations = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const createIssue = async (data: CreateIssueData): Promise<Issue> => {
    try {
      setLoading(true);
      setError(null);
      const result = await issueService.createIssue(data);
      return result;
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : 'Failed to create issue';
      setError(errorMessage);
      throw err;
    } finally {
      setLoading(false);
    }
  };

  const updateIssue = async (name: string, data: Partial<CreateIssueData>): Promise<Issue> => {
    try {
      setLoading(true);
      setError(null);
      const result = await issueService.updateIssue(name, data);
      return result;
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : 'Failed to update issue';
      setError(errorMessage);
      throw err;
    } finally {
      setLoading(false);
    }
  };

  const deleteIssue = async (name: string): Promise<void> => {
    try {
      setLoading(true);
      setError(null);
      await issueService.deleteIssue(name);
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : 'Failed to delete issue';
      setError(errorMessage);
      throw err;
    } finally {
      setLoading(false);
    }
  };

  return {
    createIssue,
    updateIssue,
    deleteIssue,
    loading,
    error,
  };
};