import request from '@/utils/request';
import type { BaseResponse } from '@/types/api';
import type { RepositoryDTO, RepositoryQuery, TestConnectionDTO, RunningSyncDTO, RepositorySyncStatusDTO } from './types';

export const getRepositories = async (params?: RepositoryQuery) => {
  return await request.get<BaseResponse<RepositoryDTO[]>>('/api/system/repositories', { params });
};

export const createRepository = async (data: Partial<RepositoryDTO>) => {
  return await request.post<BaseResponse<RepositoryDTO>>('/api/system/repositories', data);
};

export const updateRepository = async (id: number, data: Partial<RepositoryDTO>) => {
  return await request.put<BaseResponse<RepositoryDTO>>(`/api/system/repositories/${id}`, data);
};

export const deleteRepository = async (id: number) => {
  return await request.delete<BaseResponse<void>>(`/api/system/repositories/${id}`);
};

export const testConnection = async (data: TestConnectionDTO) => {
  return await request.post<BaseResponse<boolean>>('/api/system/repositories/test-connection', data);
};

export const syncAll = async (id: number) => {
  return await request.post<BaseResponse<number>>(`/api/system/repositories/${id}/sync/all`);
};

export async function getRunningSyncs() {
  return await request.get<BaseResponse<RunningSyncDTO[]>>('/api/system/repositories/sync/running');
}

export async function getSyncStatus(historyId: number) {
  return await request.get<BaseResponse<RepositorySyncStatusDTO>>(`/api/system/repositories/sync/${historyId}/status`);
} 