import { api } from './api';
import type {
  VeranstaltungKategorie,
  VeranstaltungListItem,
  Veranstaltung,
  GroupInfo,
  CreateVeranstaltungInput,
  ConflictEvent,
} from '../types/events.types';

// ---------------------------------------------------------------------------
// Response shapes from the backend
// ---------------------------------------------------------------------------
interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
}

// ---------------------------------------------------------------------------
// Events API service
// ---------------------------------------------------------------------------
export const eventsApi = {
  // -------------------------------------------------------------------------
  // Kategorien (Categories)
  // -------------------------------------------------------------------------

  /** List all event categories */
  getKategorien(): Promise<VeranstaltungKategorie[]> {
    return api
      .get<ApiResponse<VeranstaltungKategorie[]>>('/api/events/kategorien')
      .then((r) => r.data.data);
  },

  /** Create a new event category */
  createKategorie(data: {
    name: string;
    beschreibung?: string;
    farbe?: string;
    icon?: string;
    zielgruppen?: string[];
  }): Promise<VeranstaltungKategorie> {
    return api
      .post<ApiResponse<VeranstaltungKategorie>>('/api/events/kategorien', data)
      .then((r) => r.data.data);
  },

  /** Update an existing event category */
  updateKategorie(
    id: string,
    data: Partial<{ name: string; beschreibung?: string; farbe?: string; icon?: string; zielgruppen?: string[] }>
  ): Promise<VeranstaltungKategorie> {
    return api
      .patch<ApiResponse<VeranstaltungKategorie>>(`/api/events/kategorien/${id}`, data)
      .then((r) => r.data.data);
  },

  /** Delete an event category */
  deleteKategorie(id: string): Promise<void> {
    return api
      .delete(`/api/events/kategorien/${id}`)
      .then(() => undefined);
  },

  // -------------------------------------------------------------------------
  // Groups
  // -------------------------------------------------------------------------

  /** List all available target groups */
  getGroups(): Promise<GroupInfo[]> {
    return api
      .get<ApiResponse<GroupInfo[]>>('/api/events/groups')
      .then((r) => r.data.data);
  },

  // -------------------------------------------------------------------------
  // Event listing
  // -------------------------------------------------------------------------

  /** Events in a date range for the month calendar view */
  getCalendarRange(from: Date, to: Date): Promise<VeranstaltungListItem[]> {
    return api
      .get<ApiResponse<VeranstaltungListItem[]>>('/api/events/calendar', {
        params: {
          from: from.toISOString(),
          to: to.toISOString(),
        },
      })
      .then((r) => r.data.data);
  },

  /** Upcoming events (dashboard widget, list view) */
  getUpcoming(limit = 10): Promise<VeranstaltungListItem[]> {
    return api
      .get<ApiResponse<VeranstaltungListItem[]>>('/api/events/upcoming', {
        params: { limit },
      })
      .then((r) => r.data.data);
  },

  /** Full event detail */
  getById(id: string): Promise<Veranstaltung> {
    return api
      .get<ApiResponse<Veranstaltung>>(`/api/events/${id}`)
      .then((r) => r.data.data);
  },

  // -------------------------------------------------------------------------
  // CRUD
  // -------------------------------------------------------------------------

  /** Create a new event */
  createEvent(data: CreateVeranstaltungInput): Promise<Veranstaltung> {
    return api
      .post<ApiResponse<Veranstaltung>>('/api/events', data)
      .then((r) => r.data.data);
  },

  /** Update an existing event */
  updateEvent(id: string, data: Partial<CreateVeranstaltungInput>): Promise<Veranstaltung> {
    return api
      .patch<ApiResponse<Veranstaltung>>(`/api/events/${id}`, data)
      .then((r) => r.data.data);
  },

  /** Cancel (soft-delete) an event with a reason */
  cancelEvent(id: string, abgesagt_grund: string): Promise<void> {
    return api
      .delete(`/api/events/${id}`, { data: { abgesagt_grund } })
      .then(() => undefined);
  },

  /** Hard-delete an event permanently */
  deleteEvent(id: string, mode: 'all' | 'single' | 'future' = 'all'): Promise<void> {
    return api.post(`/api/events/${id}/delete`, { mode }).then(() => undefined);
  },

  // -------------------------------------------------------------------------
  // iCal
  // -------------------------------------------------------------------------

  /** Get the user's personal calendar subscribe URL */
  getCalendarToken(): Promise<{ token: string; subscribeUrl: string }> {
    return api
      .get<ApiResponse<{ token: string; subscribeUrl: string }>>(
        '/api/events/calendar-token'
      )
      .then((r) => r.data.data);
  },

  /** Bulk-import events from CSV-parsed data */
  importEvents(events: CreateVeranstaltungInput[]): Promise<{ created: number; errors: string[] }> {
    return api
      .post<ApiResponse<{ created: number; errors: string[] }>>('/api/events/import', { events })
      .then((r) => r.data.data);
  },

  /** Check for overlapping events in a time range */
  checkConflicts(from: string, to: string, excludeId?: string): Promise<ConflictEvent[]> {
    return api
      .get<ApiResponse<ConflictEvent[]>>('/api/events/conflicts', {
        params: { from, to, ...(excludeId ? { excludeId } : {}) },
      })
      .then((r) => r.data.data);
  },
};
