// ---------------------------------------------------------------------------
// Contractors API client  (client-side)
// ---------------------------------------------------------------------------

import { apiFetch } from './client';

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

export interface CreateContractorRequest {
  residence_id?: number;
  name: string;
  company?: string;
  phone?: string;
  email?: string;
  website?: string;
  notes?: string;
  street_address?: string;
  city?: string;
  state_province?: string;
  postal_code?: string;
  specialty_ids?: number[];
  rating?: number;
  is_favorite?: boolean;
}

export interface UpdateContractorRequest {
  name?: string;
  company?: string;
  phone?: string;
  email?: string;
  website?: string;
  notes?: string;
  street_address?: string;
  city?: string;
  state_province?: string;
  postal_code?: string;
  specialty_ids?: number[];
  rating?: number;
  is_favorite?: boolean;
  residence_id?: number;
}

export interface ContractorResponse {
  id: number;
  residence_id?: number;
  residence_name?: string;
  name: string;
  company: string;
  phone: string;
  email: string;
  website: string;
  notes: string;
  street_address: string;
  city: string;
  state_province: string;
  postal_code: string;
  specialties: SpecialtyResponse[];
  rating?: number;
  is_favorite: boolean;
  task_count: number;
  created_by_id: number;
  created_at: string;
  updated_at: string;
}

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

export interface ContractorTaskResponse {
  id: number;
  title: string;
  residence_name: string;
  status: string;
  due_date?: string;
}

export interface ToggleFavoriteResponse {
  id: number;
  is_favorite: boolean;
}

export interface MessageResponse {
  message: string;
}

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

/** List all contractors for the current user. */
export function listContractors(): Promise<ContractorResponse[]> {
  return apiFetch<ContractorResponse[]>('/contractors/');
}

/** List contractors for a specific residence. */
export function listContractorsByResidence(
  residenceId: number,
): Promise<ContractorResponse[]> {
  return apiFetch<ContractorResponse[]>(
    `/contractors/by-residence/${residenceId}/`,
  );
}

/** Get a single contractor by ID. */
export function getContractor(id: number): Promise<ContractorResponse> {
  return apiFetch<ContractorResponse>(`/contractors/${id}/`);
}

/** Create a new contractor. */
export function createContractor(
  data: CreateContractorRequest,
): Promise<ContractorResponse> {
  return apiFetch<ContractorResponse>('/contractors/', {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

/** Update an existing contractor. */
export function updateContractor(
  id: number,
  data: UpdateContractorRequest,
): Promise<ContractorResponse> {
  return apiFetch<ContractorResponse>(`/contractors/${id}/`, {
    method: 'PUT',
    body: JSON.stringify(data),
  });
}

/** Delete a contractor. */
export function deleteContractor(id: number): Promise<MessageResponse> {
  return apiFetch<MessageResponse>(`/contractors/${id}/`, {
    method: 'DELETE',
  });
}

/** Toggle the favorite status of a contractor. */
export function toggleFavorite(
  id: number,
): Promise<ToggleFavoriteResponse> {
  return apiFetch<ToggleFavoriteResponse>(
    `/contractors/${id}/toggle-favorite/`,
    { method: 'POST' },
  );
}

/** Get tasks associated with a contractor. */
export function getContractorTasks(
  id: number,
): Promise<ContractorTaskResponse[]> {
  return apiFetch<ContractorTaskResponse[]>(`/contractors/${id}/tasks/`);
}
