// ---------------------------------------------------------------------------
// Lookups / Static Data API client  (client-side)
// ---------------------------------------------------------------------------

import { apiFetch } from './client';

// ---------------------------------------------------------------------------
// Response shapes
// TODO: import from @/lib/types once the shared types package is finalised
// ---------------------------------------------------------------------------

export interface ResidenceTypeResponse {
  id: number;
  name: string;
  icon: string;
}

export interface TaskCategoryResponse {
  id: number;
  name: string;
  icon: string;
  color: string;
}

export interface TaskPriorityResponse {
  id: number;
  name: string;
  icon: string;
  color: string;
  sort_order: number;
}

export interface TaskFrequencyResponse {
  id: number;
  name: string;
  days: number;
  description: string;
}

export interface ContractorSpecialtyResponse {
  id: number;
  name: string;
  icon: string;
}

export interface TaskTemplateResponse {
  id: number;
  title: string;
  description: string;
  category_id: number;
  priority_id?: number;
  frequency_id?: number;
  estimated_cost?: number;
}

export interface TaskTemplateGroupResponse {
  category: TaskCategoryResponse;
  templates: TaskTemplateResponse[];
}

export interface TaskTemplatesGroupedResponse {
  groups: TaskTemplateGroupResponse[];
  total_count: number;
}

export interface StaticDataResponse {
  residence_types: ResidenceTypeResponse[];
  task_categories: TaskCategoryResponse[];
  task_priorities: TaskPriorityResponse[];
  task_frequencies: TaskFrequencyResponse[];
  contractor_specialties: ContractorSpecialtyResponse[];
  task_templates: TaskTemplatesGroupedResponse;
}

export interface MessageResponse {
  message: string;
  status: string;
}

// ---------------------------------------------------------------------------
// API functions
// ---------------------------------------------------------------------------

/**
 * Get all static/lookup data in a single request.
 * Supports ETag-based caching (304 Not Modified).
 */
export function getStaticData(): Promise<StaticDataResponse> {
  return apiFetch<StaticDataResponse>('/static_data/');
}

/** Trigger a refresh of static data cache (mostly a no-op). */
export function refreshStaticData(): Promise<MessageResponse> {
  return apiFetch<MessageResponse>('/static_data/refresh/', {
    method: 'POST',
  });
}

// --- Individual lookup endpoints (public, no auth required) ---

/** Get all residence types. */
export function getResidenceTypes(): Promise<ResidenceTypeResponse[]> {
  return apiFetch<ResidenceTypeResponse[]>('/residences/types/');
}

/** Get all task categories. */
export function getTaskCategories(): Promise<TaskCategoryResponse[]> {
  return apiFetch<TaskCategoryResponse[]>('/tasks/categories/');
}

/** Get all task priorities. */
export function getTaskPriorities(): Promise<TaskPriorityResponse[]> {
  return apiFetch<TaskPriorityResponse[]>('/tasks/priorities/');
}

/** Get all task frequencies. */
export function getTaskFrequencies(): Promise<TaskFrequencyResponse[]> {
  return apiFetch<TaskFrequencyResponse[]>('/tasks/frequencies/');
}

/** Get all contractor specialties. */
export function getContractorSpecialties(): Promise<
  ContractorSpecialtyResponse[]
> {
  return apiFetch<ContractorSpecialtyResponse[]>('/contractors/specialties/');
}

// --- Task Templates ---

/** Get all task templates (flat list). */
export function getTaskTemplates(): Promise<TaskTemplateResponse[]> {
  return apiFetch<TaskTemplateResponse[]>('/tasks/templates/');
}

/** Get task templates grouped by category. */
export function getTaskTemplatesGrouped(): Promise<TaskTemplatesGroupedResponse> {
  return apiFetch<TaskTemplatesGroupedResponse>('/tasks/templates/grouped/');
}

/** Search task templates by query string (min 2 chars). */
export function searchTaskTemplates(
  query: string,
): Promise<TaskTemplateResponse[]> {
  return apiFetch<TaskTemplateResponse[]>(
    `/tasks/templates/search/?q=${encodeURIComponent(query)}`,
  );
}

/** Get task templates for a specific category. */
export function getTaskTemplatesByCategory(
  categoryId: number,
): Promise<TaskTemplateResponse[]> {
  return apiFetch<TaskTemplateResponse[]>(
    `/tasks/templates/by-category/${categoryId}/`,
  );
}

/** Get a single task template by ID. */
export function getTaskTemplate(id: number): Promise<TaskTemplateResponse> {
  return apiFetch<TaskTemplateResponse>(`/tasks/templates/${id}/`);
}
