'use client';

import React, { createContext, useContext } from 'react';
import { JiraConfig } from '@/lib/types';

interface JiraConfigContextType {
  config: JiraConfig;
  isConfigured: boolean;
}

const JiraConfigContext = createContext<JiraConfigContextType | undefined>(
  undefined
);

interface JiraConfigProviderProps {
  children: React.ReactNode;
  config: JiraConfig;
}

export function JiraConfigProvider({
  children,
  config,
}: JiraConfigProviderProps) {
  // Une config Jira est considérée comme valide si elle a les champs obligatoires
  const isConfigured = Boolean(
    config.baseUrl && config.email && config.apiToken && config.enabled
  );

  return (
    <JiraConfigContext.Provider value={{ config, isConfigured }}>
      {children}
    </JiraConfigContext.Provider>
  );
}

export function useJiraConfig() {
  const context = useContext(JiraConfigContext);
  if (context === undefined) {
    throw new Error('useJiraConfig must be used within a JiraConfigProvider');
  }
  return context;
}
