import { apiRequest } from './api';

export interface TimeEntryRecord {
  id: string;
  projectId: string;
  milestoneId: string | null;
  taskId: string | null;
  userId: string;
  date: string;
  hours: number;
  isBillable: boolean;
  note: string | null;
  createdAt: string;
  updatedAt: string;
  user?: {
    id: string;
    name: string;
    email: string;
  };
  project?: {
    id: string;
    projectNumber: string;
    name: string;
    accountId?: string;
    account?: {
      id: string;
      name: string;
    };
  };
  milestone?: {
    id: string;
    title: string;
  } | null;
  task?: {
    id: string;
    title: string;
  } | null;
}

export interface TimesheetsStats {
  myHoursThisWeek: number;
  teamHoursThisWeek: number;
  billableHoursThisWeek: number;
  billablePercentThisWeek: number;
  activeProjectsCount: number;
}

export interface TimesheetsListResponse {
  data: TimeEntryRecord[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
    stats: TimesheetsStats;
  };
}

export interface LogTimeInput {
  projectId: string;
  userId?: string;
  milestoneId?: string;
  taskId?: string;
  date: string;
  hours: number;
  isBillable?: boolean;
  note?: string;
}

export interface UpdateTimeInput {
  projectId?: string;
  milestoneId?: string;
  taskId?: string;
  date?: string;
  hours?: number;
  isBillable?: boolean;
  note?: string;
}

export const timesheetsApi = {
  async list(
    params: {
      view?: 'this_week' | 'mine' | 'billable' | 'all';
      projectId?: string;
      userId?: string;
      startDate?: string;
      endDate?: string;
      q?: string;
      page?: number;
      limit?: number;
    } = {},
  ): Promise<TimesheetsListResponse> {
    const qs = new URLSearchParams();
    if (params.view) qs.set('view', params.view);
    if (params.projectId) qs.set('projectId', params.projectId);
    if (params.userId) qs.set('userId', params.userId);
    if (params.startDate) qs.set('startDate', params.startDate);
    if (params.endDate) qs.set('endDate', params.endDate);
    if (params.q) qs.set('q', params.q);
    if (params.page) qs.set('page', String(params.page));
    if (params.limit) qs.set('limit', String(params.limit));

    const query = qs.toString();
    return apiRequest<TimesheetsListResponse>(`/timesheets${query ? `?${query}` : ''}`, { method: 'GET' });
  },

  async get(id: string): Promise<TimeEntryRecord> {
    return apiRequest<TimeEntryRecord>(`/timesheets/${id}`, {
      method: 'GET',
    });
  },

  async create(input: LogTimeInput): Promise<TimeEntryRecord> {
    return apiRequest<TimeEntryRecord>('/timesheets', {
      method: 'POST',
      body: JSON.stringify(input),
    });
  },

  async update(id: string, input: UpdateTimeInput): Promise<TimeEntryRecord> {
    return apiRequest<TimeEntryRecord>(`/timesheets/${id}`, {
      method: 'PATCH',
      body: JSON.stringify(input),
    });
  },

  async delete(id: string): Promise<{ success: boolean }> {
    return apiRequest<{ success: boolean }>(`/timesheets/${id}`, {
      method: 'DELETE',
    });
  },
};
