/**
 * Event Service
 * Handles event-related API calls (calendar events)
 */

import { apiClient } from '../client'
import type { Author } from '../types'

export interface Event {
  id: string
  title: string
  description: string
  deadline: number // Unix timestamp
  tags: string[]
  author: Author
  calendar: 'Personal' | 'Business' | 'Family' | 'Holiday' | 'ETC'
  start: string // ISO 8601
  end: string // ISO 8601
  allDay: boolean
  extendedProps?: Record<string, any>
}

export interface ListEventsParams {
  start?: string // ISO 8601
  end?: string // ISO 8601
  calendar?: 'Personal' | 'Business' | 'Family' | 'Holiday' | 'ETC'
}

export interface CreateEventRequest {
  title: string
  description?: string
  deadline?: number
  tags?: string[]
  calendar: 'Personal' | 'Business' | 'Family' | 'Holiday' | 'ETC'
  start: string // ISO 8601
  end: string // ISO 8601
  allDay?: boolean
  extendedProps?: Record<string, any>
}

export interface CreateEventResponse {
  event: Event
}

export interface GetEventResponse {
  event: Event
}

export interface ListEventsResponse {
  events: Event[]
}

export interface UpdateEventRequest {
  id: string
  title?: string
  description?: string
  deadline?: number
  tags?: string[]
  calendar?: 'Personal' | 'Business' | 'Family' | 'Holiday' | 'ETC'
  start?: string // ISO 8601
  end?: string // ISO 8601
  allDay?: boolean
  extendedProps?: Record<string, any>
}

export interface UpdateEventResponse {
  event: Event
}

export interface DeleteEventResponse {
  success: boolean
}

export interface NonRoutineTask {
  id: string
  title: string
  description: string
  deadline: number
  routine: 0
  tags: string[]
  author: Author
  source: 'calendar' | 'kanban' | 'todo'
  status: string
  priority: 'high' | 'medium' | 'low'
}

export interface GetNonRoutineTasksResponse {
  tasks: NonRoutineTask[]
}

export const eventService = {
  /**
   * Create a new event
   */
  async createEvent(data: CreateEventRequest): Promise<Event> {
    const response = await apiClient.post<CreateEventResponse>('/api/events', data)
    return response.event
  },

  /**
   * Get an event by ID
   */
  async getEvent(id: string): Promise<Event> {
    const response = await apiClient.get<GetEventResponse>(`/api/events/${id}`)
    return response.event
  },

  /**
   * List all events with optional filters
   */
  async listEvents(params?: ListEventsParams): Promise<Event[]> {
    const queryParams = new URLSearchParams()
    if (params?.start) queryParams.append('start', params.start)
    if (params?.end) queryParams.append('end', params.end)
    if (params?.calendar) queryParams.append('calendar', params.calendar)

    const queryString = queryParams.toString()
    const endpoint = `/api/events${queryString ? `?${queryString}` : ''}`

    const response = await apiClient.get<ListEventsResponse>(endpoint)
    return response.events || []
  },

  /**
   * Update an event
   */
  async updateEvent(data: UpdateEventRequest): Promise<Event> {
    const { id, ...updateData } = data
    const response = await apiClient.put<UpdateEventResponse>(`/api/events/${id}`, updateData)
    return response.event
  },

  /**
   * Delete an event
   */
  async deleteEvent(id: string): Promise<boolean> {
    const response = await apiClient.delete<DeleteEventResponse>(`/api/events/${id}`)
    return response.success
  },

  /**
   * دریافت تسک‌های غیرروتین (مشترک با Kanban و Todo)
   */
  async getNonRoutineTasks(): Promise<NonRoutineTask[]> {
    const response = await apiClient.get<GetNonRoutineTasksResponse>('/api/tasks/non-routine')
    return response.tasks || []
  }
}

