'use client';

import { useQuery, useMutation, UseQueryOptions } from '@tanstack/react-query';
import { colorAPI } from './client';
import {
  ColorInfoRequest,
  ColorInfoData,
  ConvertFormatRequest,
  ConvertFormatData,
  ColorManipulationRequest,
  ColorManipulationData,
  RandomColorsRequest,
  RandomColorsData,
  GradientRequest,
  GradientData,
  PaletteGenerateRequest,
  PaletteGenerateData,
  HealthData,
} from './types';

// Color Information
export const useColorInfo = (
  request: ColorInfoRequest,
  options?: Omit<UseQueryOptions<ColorInfoData>, 'queryKey' | 'queryFn'>
) => {
  return useQuery({
    queryKey: ['colorInfo', request.colors],
    queryFn: async () => {
      const response = await colorAPI.getColorInfo(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
    enabled: request.colors.length > 0 && request.colors.every((c) => c.length > 0),
    ...options,
  });
};

// Format Conversion
export const useConvertFormat = () => {
  return useMutation({
    mutationFn: async (request: ConvertFormatRequest) => {
      const response = await colorAPI.convertFormat(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

// Color Manipulation
export const useLighten = () => {
  return useMutation({
    mutationFn: async (request: ColorManipulationRequest) => {
      const response = await colorAPI.lighten(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

export const useDarken = () => {
  return useMutation({
    mutationFn: async (request: ColorManipulationRequest) => {
      const response = await colorAPI.darken(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

export const useSaturate = () => {
  return useMutation({
    mutationFn: async (request: ColorManipulationRequest) => {
      const response = await colorAPI.saturate(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

export const useDesaturate = () => {
  return useMutation({
    mutationFn: async (request: ColorManipulationRequest) => {
      const response = await colorAPI.desaturate(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

export const useRotate = () => {
  return useMutation({
    mutationFn: async (request: ColorManipulationRequest) => {
      const response = await colorAPI.rotate(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

export const useComplement = () => {
  return useMutation({
    mutationFn: async (colors: string[]) => {
      const response = await colorAPI.complement(colors);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

// Color Generation
export const useGenerateRandom = () => {
  return useMutation({
    mutationFn: async (request: RandomColorsRequest) => {
      const response = await colorAPI.generateRandom(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

export const useGenerateGradient = () => {
  return useMutation({
    mutationFn: async (request: GradientRequest) => {
      const response = await colorAPI.generateGradient(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};

// Health Check
export const useHealth = () => {
  return useQuery({
    queryKey: ['health'],
    queryFn: async () => {
      const response = await colorAPI.getHealth();
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
    refetchInterval: 60000, // Check every minute
  });
};

// Palette Generation
export const useGeneratePalette = () => {
  return useMutation({
    mutationFn: async (request: PaletteGenerateRequest) => {
      const response = await colorAPI.generatePalette(request);
      if (!response.success) {
        throw new Error(response.error.message);
      }
      return response.data;
    },
  });
};
