import axios from 'axios';
import { API_BASE_URL } from './apiConfig';

export interface SchedulerStatus {
  isEnabled: boolean;
  isRunning: boolean;
  cronExpression: string;
  nextRunTime: string;
  lastRunTime: string;
  lastRunResult: string;
  downloadedFilesCount: number;
  failedFilesCount: number;
  downloadDir: string;
  maxWorkers: number;
  currentTask: string;
  progressPercent: number;
  successCount?: number;
  failCount?: number;
  totalRunCount?: number;
  recentLogs?: string[];
}

export interface SchedulerConfig {
  [key: string]: string;
}

export const schedulerApi = {
  getStatus: async (): Promise<SchedulerStatus> => {
    const response = await axios.get(`${API_BASE_URL}/scheduler/status`);
    return response.data;
  },

  startDownload: async (): Promise<SchedulerStatus> => {
    const response = await axios.post(`${API_BASE_URL}/scheduler/start`);
    return response.data;
  },

  stopDownload: async (): Promise<SchedulerStatus> => {
    const response = await axios.post(`${API_BASE_URL}/scheduler/stop`);
    return response.data;
  },

  updateConfig: async (config: SchedulerConfig): Promise<SchedulerStatus> => {
    const response = await axios.put(`${API_BASE_URL}/scheduler/config`, config);
    return response.data;
  },
};

