import request from '@/utils/request';
import type { Page } from '@/types/base';
import type {
    JobCategoryResponse,
    JobCategoryRequest,
    JobCategoryQuery,
    ScheduleJobResponse,
    ScheduleJobRequest,
    ScheduleJobQuery,
    ScheduleJobLogResponse,
    ScheduleJobLogQuery,
    DashboardResponse,
} from './types';

const JOB_CATEGORY_URL = '/api/v1/schedule/job-categories';
const SCHEDULE_JOB_URL = '/api/v1/schedule/jobs';
const JOB_LOG_URL = '/api/v1/schedule/job-logs';

// ==================== 任务分类 ====================

/**
 * 获取任务分类列表
 */
export const getJobCategoryList = (params?: JobCategoryQuery) =>
    request.get<JobCategoryResponse[]>(`${JOB_CATEGORY_URL}/list`, { params });

/**
 * 获取任务分类分页
 */
export const getJobCategories = (params?: JobCategoryQuery) =>
    request.get<Page<JobCategoryResponse>>(`${JOB_CATEGORY_URL}/page`, { params });

/**
 * 创建任务分类
 */
export const createJobCategory = (data: JobCategoryRequest) =>
    request.post<JobCategoryResponse>(JOB_CATEGORY_URL, data);

/**
 * 更新任务分类
 */
export const updateJobCategory = (id: number, data: JobCategoryRequest) =>
    request.put<JobCategoryResponse>(`${JOB_CATEGORY_URL}/${id}`, data);

/**
 * 删除任务分类
 */
export const deleteJobCategory = (id: number) =>
    request.delete(`${JOB_CATEGORY_URL}/${id}`);

/**
 * 批量删除任务分类
 */
export const batchDeleteJobCategories = (ids: number[]) =>
    request.post(`${JOB_CATEGORY_URL}/batch-delete`, { ids });

// ==================== 定时任务 ====================

/**
 * 获取定时任务分页
 */
export const getScheduleJobs = (params?: ScheduleJobQuery) =>
    request.get<Page<ScheduleJobResponse>>(`${SCHEDULE_JOB_URL}/page`, { params });

/**
 * 获取定时任务详情
 */
export const getScheduleJob = (id: number) =>
    request.get<ScheduleJobResponse>(`${SCHEDULE_JOB_URL}/${id}`);

/**
 * 创建定时任务
 */
export const createScheduleJob = (data: ScheduleJobRequest) =>
    request.post<ScheduleJobResponse>(SCHEDULE_JOB_URL, data);

/**
 * 更新定时任务
 */
export const updateScheduleJob = (id: number, data: ScheduleJobRequest) =>
    request.put<ScheduleJobResponse>(`${SCHEDULE_JOB_URL}/${id}`, data);

/**
 * 删除定时任务
 */
export const deleteScheduleJob = (id: number) =>
    request.delete(`${SCHEDULE_JOB_URL}/${id}`);

/**
 * 批量删除定时任务
 */
export const batchDeleteScheduleJobs = (ids: number[]) =>
    request.post(`${SCHEDULE_JOB_URL}/batch-delete`, { ids });

/**
 * 启动任务
 */
export const startJob = (id: number) =>
    request.post<void>(`${SCHEDULE_JOB_URL}/${id}/start`);

/**
 * 暂停任务
 */
export const pauseJob = (id: number) =>
    request.post<void>(`${SCHEDULE_JOB_URL}/${id}/pause`);

/**
 * 恢复任务
 */
export const resumeJob = (id: number) =>
    request.post<void>(`${SCHEDULE_JOB_URL}/${id}/resume`);

/**
 * 停止任务
 */
export const stopJob = (id: number) =>
    request.post<void>(`${SCHEDULE_JOB_URL}/${id}/stop`);

/**
 * 立即触发任务
 */
export const triggerJob = (id: number) =>
    request.post<void>(`${SCHEDULE_JOB_URL}/${id}/trigger`);

/**
 * 禁用任务
 */
export const disableJob = (id: number) =>
    request.post<void>(`${SCHEDULE_JOB_URL}/${id}/disable`);

/**
 * 启用任务（解除禁用）
 */
export const enableJob = (id: number) => request.post<void>(`${SCHEDULE_JOB_URL}/${id}/enable`);

/**
 * 更新Cron表达式
 */
export const updateJobCron = (id: number, cronExpression: string) =>
    request.put<void>(`${SCHEDULE_JOB_URL}/${id}/cron`, null, { 
        params: { cronExpression } 
    });

// ==================== 任务日志 ====================

/**
 * 获取任务执行日志分页
 */
export const getJobLogs = (params?: ScheduleJobLogQuery) =>
    request.get<Page<ScheduleJobLogResponse>>(`${JOB_LOG_URL}/page`, { params });

// ==================== 仪表盘 ====================

/**
 * 获取仪表盘数据
 */
export const getDashboard = () =>
    request.get<DashboardResponse>(`${SCHEDULE_JOB_URL}/dashboard`);

