/**
 * Client pour l'API Jira
 */

import { HttpClient } from './base/http-client';
import { JiraSyncResult } from '@/services/integrations/jira/jira';

export interface JiraConnectionStatus {
  connected: boolean;
  message: string;
  details?: string;
  scheduler?: JiraSchedulerStatus;
}

export interface JiraSchedulerStatus {
  isRunning: boolean;
  isEnabled: boolean;
  interval: 'hourly' | 'daily' | 'weekly';
  nextSync: string | null;
  jiraConfigured: boolean;
}

export class JiraClient extends HttpClient {
  constructor() {
    super('/api/jira');
  }

  /**
   * Teste la connexion à Jira
   */
  async testConnection(): Promise<JiraConnectionStatus> {
    return this.get<JiraConnectionStatus>('/sync');
  }

  /**
   * Lance la synchronisation manuelle des tickets Jira
   */
  async syncTasks(): Promise<JiraSyncResult> {
    const response = await this.post<{ data: JiraSyncResult }>('/sync');
    return response.data;
  }

  /**
   * Active/désactive le scheduler automatique
   */
  async toggleScheduler(enabled: boolean): Promise<JiraSchedulerStatus> {
    const response = await this.post<{ data: JiraSchedulerStatus }>('/sync', {
      action: 'scheduler',
      enabled,
    });
    return response.data;
  }

  /**
   * Met à jour la configuration du scheduler
   */
  async updateSchedulerConfig(
    jiraAutoSync: boolean,
    jiraSyncInterval: 'hourly' | 'daily' | 'weekly'
  ): Promise<JiraSchedulerStatus> {
    const response = await this.post<{ data: JiraSchedulerStatus }>('/sync', {
      action: 'config',
      jiraAutoSync,
      jiraSyncInterval,
    });
    return response.data;
  }
}

// Instance singleton
export const jiraClient = new JiraClient();
