import { api } from './api';
import type { Issue, IssueComment, CreateIssuePayload, UpdateIssuePayload, IssueTyp, IssueFilters, AssignableMember, IssueStatusDef, IssuePriorityDef, IssueWidgetSummary, IssueHistorie, IssueDatei } from '../types/issue.types';

export const issuesApi = {
  getIssues: async (filters?: IssueFilters): Promise<Issue[]> => {
    const params = new URLSearchParams();
    if (filters?.typ_id?.length) params.set('typ_id', filters.typ_id.join(','));
    if (filters?.prioritaet?.length) params.set('prioritaet', filters.prioritaet.join(','));
    if (filters?.status?.length) params.set('status', filters.status.join(','));
    if (filters?.erstellt_von) params.set('erstellt_von', filters.erstellt_von);
    if (filters?.zugewiesen_an) params.set('zugewiesen_an', filters.zugewiesen_an);
    const qs = params.toString();
    const r = await api.get(`/api/issues${qs ? `?${qs}` : ''}`);
    return r.data.data;
  },
  getIssue: async (id: number): Promise<Issue> => {
    const r = await api.get(`/api/issues/${id}`);
    return r.data.data;
  },
  createIssue: async (data: CreateIssuePayload): Promise<Issue> => {
    const r = await api.post('/api/issues', data);
    return r.data.data;
  },
  updateIssue: async (id: number, data: UpdateIssuePayload): Promise<Issue> => {
    const r = await api.patch(`/api/issues/${id}`, data);
    return r.data.data;
  },
  deleteIssue: async (id: number): Promise<void> => {
    await api.delete(`/api/issues/${id}`);
  },
  getComments: async (issueId: number): Promise<IssueComment[]> => {
    const r = await api.get(`/api/issues/${issueId}/comments`);
    return r.data.data;
  },
  addComment: async (issueId: number, inhalt: string): Promise<IssueComment> => {
    const r = await api.post(`/api/issues/${issueId}/comments`, { inhalt });
    return r.data.data;
  },
  getHistory: async (issueId: number): Promise<IssueHistorie[]> => {
    const r = await api.get(`/api/issues/${issueId}/history`);
    return r.data.data;
  },
  // Types CRUD
  getTypes: async (): Promise<IssueTyp[]> => {
    const r = await api.get('/api/issues/typen');
    return r.data.data;
  },
  createType: async (data: Partial<IssueTyp>): Promise<IssueTyp> => {
    const r = await api.post('/api/issues/typen', data);
    return r.data.data;
  },
  updateType: async (id: number, data: Partial<IssueTyp>): Promise<IssueTyp> => {
    const r = await api.patch(`/api/issues/typen/${id}`, data);
    return r.data.data;
  },
  deleteType: async (id: number): Promise<void> => {
    await api.delete(`/api/issues/typen/${id}`);
  },
  // Members for assignment
  getMembers: async (): Promise<AssignableMember[]> => {
    const r = await api.get('/api/issues/members');
    return r.data.data;
  },
  // Widget summary
  getWidgetSummary: async (): Promise<IssueWidgetSummary> => {
    const r = await api.get('/api/issues/widget-summary');
    return r.data.data;
  },
  // Statuses CRUD
  getStatuses: async (): Promise<IssueStatusDef[]> => {
    const r = await api.get('/api/issues/statuses');
    return r.data.data;
  },
  createStatus: async (data: Partial<IssueStatusDef>): Promise<IssueStatusDef> => {
    const r = await api.post('/api/issues/statuses', data);
    return r.data.data;
  },
  updateStatus: async (id: number, data: Partial<IssueStatusDef>): Promise<IssueStatusDef> => {
    const r = await api.patch(`/api/issues/statuses/${id}`, data);
    return r.data.data;
  },
  deleteStatus: async (id: number): Promise<void> => {
    await api.delete(`/api/issues/statuses/${id}`);
  },
  // Priorities CRUD
  getPriorities: async (): Promise<IssuePriorityDef[]> => {
    const r = await api.get('/api/issues/priorities');
    return r.data.data;
  },
  createPriority: async (data: Partial<IssuePriorityDef>): Promise<IssuePriorityDef> => {
    const r = await api.post('/api/issues/priorities', data);
    return r.data.data;
  },
  updatePriority: async (id: number, data: Partial<IssuePriorityDef>): Promise<IssuePriorityDef> => {
    const r = await api.patch(`/api/issues/priorities/${id}`, data);
    return r.data.data;
  },
  deletePriority: async (id: number): Promise<void> => {
    await api.delete(`/api/issues/priorities/${id}`);
  },
  // Files
  uploadFile: async (issueId: number, file: File): Promise<IssueDatei> => {
    const formData = new FormData();
    formData.append('file', file);
    const r = await api.post(`/api/issues/${issueId}/files`, formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
    });
    return r.data.data;
  },
  getFiles: async (issueId: number): Promise<IssueDatei[]> => {
    const r = await api.get(`/api/issues/${issueId}/files`);
    return r.data.data;
  },
  deleteFile: async (fileId: string): Promise<void> => {
    await api.delete(`/api/issues/files/${fileId}`);
  },
};
