import request from '@/utils/request';
import type { CreateApplicationRequest, UpdateApplicationRequest, Application, ApplicationQuery, RepositoryGroup, RepositoryProject } from './types';
import type { Page } from '@/types/base';
import {DevelopmentLanguageType} from "./types";

const BASE_URL = '/api/v1/applications';
const REPOSITORY_GROUP_URL = '/api/v1/repository-group';
const REPOSITORY_PROJECT_URL = '/api/v1/repository-project';

// 创建应用
export const createApplication = (data: CreateApplicationRequest) => 
    request.post<void>(BASE_URL, data);

// 更新应用
export const updateApplication = (data: UpdateApplicationRequest) => 
    request.put<void>(`${BASE_URL}/${data.id}`, data);

// 删除应用
export const deleteApplication = (id: number) => 
    request.delete<void>(`${BASE_URL}/${id}`);

// 获取应用详情
export const getApplication = (id: number) => 
    request.get<Application>(`${BASE_URL}/${id}`);

// 分页查询应用列表
export const getApplicationPage = (params?: ApplicationQuery) =>
    request.get<Page<Application>>(`${BASE_URL}/page`, { params });

// 获取所有应用列表
export const getApplicationList = () => 
    request.get<Application[]>(BASE_URL);

// 条件查询应用列表
export const getApplicationListByCondition = (params?: ApplicationQuery) =>
    request.get<Application[]>(`${BASE_URL}/list`, { params });

// 获取开发语言列表
export const getDevelopmentLanguages = () =>
    request.get<DevelopmentLanguageType[]>(`${BASE_URL}/development-languages`);

// 获取仓库组列表
export const getRepositoryGroupList = (externalSystemId: number) =>
    request.get<RepositoryGroup[]>(`${REPOSITORY_GROUP_URL}/list`, {
        params: { externalSystemId },
    });

// 获取项目列表
export const getRepositoryProjects = (repoGroupId: number) =>
    request.get<RepositoryProject[]>(`${REPOSITORY_PROJECT_URL}/list`, {
        params: { repoGroupId: repoGroupId },
    });
