import axiosInstance from '@/utills/axiosInstance';
import type {
  AvailableNow, CreateLeadRequestPayload, FilterOptions, LaunchMember, LeadFilters, LeadRequest, OptionItem,
} from './interface';

const BASE = '/api/v1/lead-requests';

export async function getAllLeadRequest(filter: FilterOptions): Promise<any> {
  const response = await axiosInstance.get(BASE, { params: filter });
  return response.data;
}

export async function getLeadRequest(id: number): Promise<LeadRequest> {
  const response = await axiosInstance.get(`${BASE}/${id}`);
  return response.data.data;
}

/** What a pending request would copy right now (the requester sees this too). */
export async function getAvailableNow(id: number): Promise<AvailableNow> {
  const response = await axiosInstance.get(`${BASE}/${id}/available`);
  return response.data.data;
}

export async function approveLeadRequest(id: number): Promise<any> {
  const response = await axiosInstance.patch(`${BASE}/${id}/approve`);
  return response.data;
}

export async function rejectLeadRequest(id: number, reason: string): Promise<any> {
  const response = await axiosInstance.patch(`${BASE}/${id}/reject`, { reason });
  return response.data;
}

export async function stopLeadRequest(id: number): Promise<any> {
  const response = await axiosInstance.patch(`${BASE}/${id}/stop`);
  return response.data;
}

export async function addLeadRequests(data: CreateLeadRequestPayload): Promise<any> {
  const response = await axiosInstance.post(BASE, data);
  return response.data;
}

export async function getLocalityOptions(cityId: number): Promise<OptionItem[]> {
  const response = await axiosInstance.get(`${BASE}/options/localities`, { params: { city_id: cityId } });
  return response.data.data;
}

export async function getProjectOptions(cityId: number, localityIds: number[]): Promise<OptionItem[]> {
  const response = await axiosInstance.get(`${BASE}/options/projects`, {
    params: { city_id: cityId, localities: localityIds.join(',') },
  });
  return response.data.data;
}

export async function getSourceOptions(): Promise<OptionItem[]> {
  const response = await axiosInstance.get(`${BASE}/options/sources`);
  return response.data.data;
}

export async function getLaunchOptions(cityId: number): Promise<OptionItem[]> {
  const response = await axiosInstance.get(`${BASE}/options/launches`, { params: { city_id: cityId } });
  return response.data.data;
}

export async function getLaunchMembers(launchId: number): Promise<LaunchMember[]> {
  const response = await axiosInstance.get(`${BASE}/options/launch-members/${launchId}`);
  return response.data.data;
}

/** The destination launch's projects, for the receiving-project choice. */
export async function getLaunchProjectOptions(launchId: number): Promise<OptionItem[]> {
  const response = await axiosInstance.get(`${BASE}/launch-projects`, { params: { launch_id: launchId } });
  return response.data.data;
}

/** Live available-lead count. Pass an AbortSignal so a stale answer never overwrites a newer one. */
export async function countAvailableLeads(
  filters: LeadFilters & { destination_launch_id?: number | null },
  signal?: AbortSignal
): Promise<number> {
  const response = await axiosInstance.post(`${BASE}/count`, filters, { signal });
  return response.data.data.available;
}

export async function getCityOptions(): Promise<OptionItem[]> {
  const response = await axiosInstance.get('/api/v1/masters/cities', {
    params: { pageSize: 500, sortColumn: 'name', sortOrder: 'asc' },
  });
  return response.data.data;
}
