import { useState, useEffect, useCallback } from 'react';
import maintenanceTeamService, { 
  type MaintenanceTeam, 
  type CreateMaintenanceTeamData, 
  type MaintenanceTeamListParams 
} from '../services/maintenanceTeamService';

// Hook for fetching maintenance team list
export const useMaintenanceTeamList = (params: MaintenanceTeamListParams = {}) => {
  const [teams, setTeams] = useState<MaintenanceTeam[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [totalCount, setTotalCount] = useState(0);

  const fetchTeams = useCallback(async () => {
    try {
      setLoading(true);
      setError(null);
      const response = await maintenanceTeamService.getMaintenanceTeams(params);
      setTeams(response.data);
      
      // Get total count for pagination
      const count = await maintenanceTeamService.getMaintenanceTeamCount(params.filters);
      setTotalCount(count);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to fetch maintenance teams');
    } finally {
      setLoading(false);
    }
  }, [JSON.stringify(params)]);

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

  return {
    teams,
    loading,
    error,
    totalCount,
    refetch: fetchTeams,
  };
};

// Hook for fetching single maintenance team details
export const useMaintenanceTeamDetails = (teamName: string | null) => {
  const [team, setTeam] = useState<MaintenanceTeam | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const fetchTeam = useCallback(async () => {
    if (!teamName) {
      setTeam(null);
      return;
    }

    try {
      setLoading(true);
      setError(null);
      const data = await maintenanceTeamService.getMaintenanceTeam(teamName);
      setTeam(data);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to fetch maintenance team details');
    } finally {
      setLoading(false);
    }
  }, [teamName]);

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

  return {
    team,
    loading,
    error,
    refetch: fetchTeam,
  };
};

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

  const createTeam = async (data: CreateMaintenanceTeamData): Promise<MaintenanceTeam> => {
    try {
      setLoading(true);
      setError(null);
      const result = await maintenanceTeamService.createMaintenanceTeam(data);
      return result;
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : 'Failed to create maintenance team';
      setError(errorMessage);
      throw err;
    } finally {
      setLoading(false);
    }
  };

  const updateTeam = async (name: string, data: Partial<CreateMaintenanceTeamData>): Promise<MaintenanceTeam> => {
    try {
      setLoading(true);
      setError(null);
      const result = await maintenanceTeamService.updateMaintenanceTeam(name, data);
      return result;
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : 'Failed to update maintenance team';
      setError(errorMessage);
      throw err;
    } finally {
      setLoading(false);
    }
  };

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

  const getUserFullName = async (email: string): Promise<string> => {
    return await maintenanceTeamService.getUserFullName(email);
  };

  return {
    createTeam,
    updateTeam,
    deleteTeam,
    getUserFullName,
    loading,
    error,
  };
};