import { fetchApi } from './client';
import type {
  DashboardSummary,
  UserProfile,
  UserTicket,
  UserPayment,
  UserInvoice,
  UserSession,
  NextEventInfo,
} from './types';

export const dashboardApi = {
  // Summary
  getSummary: () =>
    fetchApi<{ summary: DashboardSummary }>('/api/dashboard/summary'),

  // Profile
  getProfile: () =>
    fetchApi<{ profile: UserProfile }>('/api/dashboard/profile'),

  updateProfile: (data: { name?: string; phone?: string; languagePreference?: string; rucNumber?: string }) =>
    fetchApi<{ profile: UserProfile; message: string }>('/api/dashboard/profile', {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  // Tickets
  getTickets: () =>
    fetchApi<{ tickets: UserTicket[] }>('/api/dashboard/tickets'),

  getTicket: (id: string) =>
    fetchApi<{ ticket: UserTicket }>(`/api/dashboard/tickets/${id}`),

  // Next event
  getNextEvent: () =>
    fetchApi<{ nextEvent: NextEventInfo | null }>('/api/dashboard/next-event'),

  // Payments
  getPayments: () =>
    fetchApi<{ payments: UserPayment[] }>('/api/dashboard/payments'),

  // Invoices
  getInvoices: () =>
    fetchApi<{ invoices: UserInvoice[] }>('/api/dashboard/invoices'),

  // Sessions
  getSessions: () =>
    fetchApi<{ sessions: UserSession[] }>('/api/dashboard/sessions'),

  revokeSession: (id: string) =>
    fetchApi<{ message: string }>(`/api/dashboard/sessions/${id}`, { method: 'DELETE' }),

  revokeAllSessions: () =>
    fetchApi<{ message: string }>('/api/dashboard/sessions/revoke-all', { method: 'POST' }),

  // Security
  setPassword: (password: string) =>
    fetchApi<{ message: string }>('/api/dashboard/set-password', {
      method: 'POST',
      body: JSON.stringify({ password }),
    }),

  unlinkGoogle: () =>
    fetchApi<{ message: string }>('/api/dashboard/unlink-google', { method: 'POST' }),
};
