import request from '@/utils/request';
import type {DeploymentConfig, CreateDeploymentConfigRequest, UpdateDeploymentConfigRequest, DeploymentConfigQueryParams, DeployConfigTemplate} from './types';
import type {Page} from '@/types/base';
import type {ExternalSystem} from '@/pages/Deploy/External/types';

interface JenkinsView {
    id: number;
    name: string;
}

interface JenkinsJob {
    id: number;
    name: string;
}

const BASE_URL = '/api/v1/deploy-app-config';

// 获取部署配置分页列表
export const getDeploymentConfigPage = (params: DeploymentConfigQueryParams) =>
    request.get<Page<DeploymentConfig>>(`${BASE_URL}/page`, {params});

// 创建部署配置
export const createDeploymentConfig = (data: CreateDeploymentConfigRequest) =>
    request.post<DeploymentConfig>(BASE_URL, data);

// 更新部署配置
export const updateDeploymentConfig = (data: UpdateDeploymentConfigRequest) =>
    request.put<DeploymentConfig>(`${BASE_URL}/${data.id}`, data);

// 删除部署配置
export const deleteDeploymentConfig = (id: number) =>
    request.delete(`${BASE_URL}/${id}`);

// 获取部署配置详情
export const getDeploymentConfig = (id: number) =>
    request.get<DeploymentConfig>(`${BASE_URL}/${id}`);

// 获取环境下的所有部署配置
export const getDeploymentConfigsByEnv = (environmentId: number) =>
    request.get<DeploymentConfig[]>(`${BASE_URL}/env/${environmentId}`);

// 获取部署配置模板列表
export const getDeployConfigTemplates = () =>
    request.get<DeployConfigTemplate[]>(`${BASE_URL}/defined`);

// 获取外部系统列表
export const getExternalSystemList = (type: string) =>
    request.get<ExternalSystem[]>('/api/v1/external-system/list', {params: {type}});

// 获取 Jenkins View 列表
export const getJenkinsViewList = (externalSystemId: number) =>
    request.get<JenkinsView[]>('/api/v1/jenkins-view/list', {params: {externalSystemId}});

// 获取 Jenkins Job 列表
export const getJenkinsJobList = (externalSystemId: number, viewId: number) =>
    request.get<JenkinsJob[]>('/api/v1/jenkins-job/list', {params: {externalSystemId, viewId}});