import axios from 'axios';
import dotenv from 'dotenv';

dotenv.config();

export class JiraClient {
  private client;

  constructor(token: string) {
    this.client = axios.create({
      baseURL: process.env.JIRA_URL || '',
      auth: {
        username: process.env.JIRA_USERNAME || '',
        password: token,
      },
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
    });
  }

  async createTicket(summary: string, description: string, projectKey: string) {
    const response = await this.client.post('/rest/api/3/issue', {
      fields: {
        project: { key: projectKey },
        summary,
        description: {
          type: "doc",
          version: 1,
          content: [
            {
              type: "paragraph",
              content: [
                {
                  type: "text",
                  text: description
                }
              ]
            }
          ]
        },
        issuetype: { name: 'Task' },
      },
    });

    return response.data;
  }
} 