/**
 * 授权管理 Hooks
 * 使用 React Query 进行数据获取和缓存管理
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { authorizationService } from '@/services/authorizationService';
import type {
  QueryAuthorizationsParams,
  RevokeAuthorizationRequest,
  GrantCommunityRequest,
  GrantProvinceCompanyRequest,
  GrantCityCompanyRequest,
  GrantAuthProvinceCompanyRequest,
  GrantAuthCityCompanyRequest,
} from '@/types/authorization.types';

/** Query Keys */
export const authorizationKeys = {
  all: ['authorizations'] as const,
  list: (params: QueryAuthorizationsParams) =>
    [...authorizationKeys.all, 'list', params] as const,
};

/**
 * 获取授权列表
 */
export function useAuthorizations(params: QueryAuthorizationsParams = {}) {
  return useQuery({
    queryKey: authorizationKeys.list(params),
    queryFn: () => authorizationService.getList(params),
    staleTime: 30 * 1000, // 30秒后标记为过期
    gcTime: 5 * 60 * 1000, // 5分钟后垃圾回收
  });
}

/**
 * 撤销授权
 */
export function useRevokeAuthorization() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ authorizationId, data }: { authorizationId: string; data: RevokeAuthorizationRequest }) =>
      authorizationService.revoke(authorizationId, data),
    onSuccess: () => {
      // 撤销成功后刷新所有相关列表
      queryClient.invalidateQueries({ queryKey: authorizationKeys.all });
    },
  });
}

/**
 * 授权社区
 */
export function useGrantCommunity() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: GrantCommunityRequest) => authorizationService.grantCommunity(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: authorizationKeys.all });
    },
  });
}

/**
 * 授权正式省公司
 */
export function useGrantProvinceCompany() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: GrantProvinceCompanyRequest) => authorizationService.grantProvinceCompany(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: authorizationKeys.all });
    },
  });
}

/**
 * 授权正式市公司
 */
export function useGrantCityCompany() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: GrantCityCompanyRequest) => authorizationService.grantCityCompany(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: authorizationKeys.all });
    },
  });
}

/**
 * 授权省团队
 */
export function useGrantAuthProvinceCompany() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: GrantAuthProvinceCompanyRequest) => authorizationService.grantAuthProvinceCompany(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: authorizationKeys.all });
    },
  });
}

/**
 * 授权市团队
 */
export function useGrantAuthCityCompany() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: GrantAuthCityCompanyRequest) => authorizationService.grantAuthCityCompany(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: authorizationKeys.all });
    },
  });
}
