import { httpClient } from './base/http-client';
import { JiraConfig } from '@/lib/types';

export interface JiraConfigResponse {
  jiraConfig: JiraConfig;
}

export interface SaveJiraConfigRequest {
  baseUrl: string;
  email: string;
  apiToken: string;
  projectKey?: string;
  ignoredProjects?: string[];
}

export interface SaveJiraConfigResponse {
  success: boolean;
  message: string;
  jiraConfig: JiraConfig;
}

class JiraConfigClient {
  private readonly basePath = '/user-preferences/jira-config';

  /**
   * Récupère la configuration Jira actuelle
   */
  async getJiraConfig(): Promise<JiraConfig> {
    const response = await httpClient.get<JiraConfigResponse>(this.basePath);
    return response.jiraConfig;
  }

  /**
   * Sauvegarde la configuration Jira
   */
  async saveJiraConfig(
    config: SaveJiraConfigRequest
  ): Promise<SaveJiraConfigResponse> {
    return httpClient.put<SaveJiraConfigResponse>(this.basePath, config);
  }

  /**
   * Supprime la configuration Jira (remet à zéro)
   */
  async deleteJiraConfig(): Promise<{ success: boolean; message: string }> {
    return httpClient.delete(this.basePath);
  }

  /**
   * Valide l'existence d'un projet Jira
   */
  async validateProject(projectKey: string): Promise<{
    success: boolean;
    exists: boolean;
    projectName?: string;
    error?: string;
    message: string;
  }> {
    return httpClient.post('/jira/validate-project', { projectKey });
  }
}

export const jiraConfigClient = new JiraConfigClient();
