import { useQuery } from '@tanstack/react-query';
import { projectService } from '../services/projectService';
import { BuildFilter } from '../types';

export const useProjects = () => {
  return useQuery({
    queryKey: ['projects'],
    queryFn: () => projectService.getProjects(),
  });
};

export const useProject = (id: number) => {
  return useQuery({
    queryKey: ['project', id],
    queryFn: () => projectService.getProject(id),
    enabled: !!id,
  });
};

export const useProjectBuilds = (
  projectId: number,
  page: number = 1,
  pageSize: number = 10,
  filter?: BuildFilter
) => {
  return useQuery({
    queryKey: ['project-builds', projectId, page, pageSize, filter],
    queryFn: () => projectService.getProjectBuilds(projectId, page, pageSize, filter),
    enabled: !!projectId,
    keepPreviousData: true,
  });
};

export const useBuild = (buildId: number) => {
  return useQuery({
    queryKey: ['build', buildId],
    queryFn: () => projectService.getBuild(buildId),
    enabled: !!buildId,
  });
};

export const useBuildCommits = (buildId: number) => {
  return useQuery({
    queryKey: ['build-commits', buildId],
    queryFn: () => projectService.getBuildCommits(buildId),
    enabled: !!buildId,
  });
};

export const useProjectStatus = () => {
  return useQuery({
    queryKey: ['project-status'],
    queryFn: () => projectService.getProjectStatus(),
    refetchInterval: 30000, // Refresh every 30 seconds
  });
};