import ApiService from "./ApiService";

// Vendor interfaces
export interface Vendor {
  id: string;
  vendor_name: string;
  type_id: string;
  phone_number: string;
  email: string;
  project_id: string;
  notes: string;
  created_at?: string;
  updated_at?: string;
  partyType?: {
    id: string;
    type_name: string;
  };
  project?: {
    id: string;
    name: string;
  };
}

export interface VendorType {
  id: string;
  type_name: string;
  type_category: string;
}

export interface Project {
  id: string;
  name: string;
}

export interface VendorFilters {
  search?: string;
  type_id?: string;
  column_name?: string;
  order?: 'ASC' | 'DESC';
  page?: number;
  limit?: number;
}

export interface CreateVendorData {
  vendor_name: string;
  type_id: string;
  phone_number: string;
  email: string;
  project_id: string;
  notes: string;
}

export interface UpdateVendorData {
  id: string;
  vendor_name: string;
  type_id: string;
  phone_number: string;
  email: string;
  project_id: string;
  notes: string;
}

export interface CreateVendorTypeData {
  type_name: string;
  type_category: "VENDOR";
}

export interface VendorListResponse {
  success: boolean;
  message?: string;
  data: {
    data: Vendor[];
    count: number;
  };
}

export interface VendorResponse {
  success: boolean;
  message?: string;
  data: Vendor;
}

export interface VendorTypesResponse {
  success: boolean;
  message?: string;
  data: VendorType[];
}

export interface ProjectsResponse {
  success: boolean;
  message?: string;
  data: {
    data: Project[];
    count: number;
  };
}

export interface VendorTypeResponse {
  success: boolean;
  message?: string;
  data: VendorType;
}

// Get Vendor Types
export async function getVendorTypes() {
  return ApiService.request<VendorTypesResponse>({
    url: "/vendors/types",
    method: "get",
  });
}

// Create Vendor Type (Inline)
export async function createVendorType(data: CreateVendorTypeData) {
  return ApiService.request<VendorTypeResponse>({
    url: "/party-types",
    method: "post",
    data,
  });
}

// Get Projects for dropdown
export async function getProjectsForDropdown() {
  return ApiService.request<ProjectsResponse>({
    url: "/projects",
    method: "get",
    params: {
      limit: 9999,
    },
  });
}

// Create Vendor
export async function createVendor(data: CreateVendorData) {
  return ApiService.request<VendorResponse>({
    url: "/vendors",
    method: "post",
    data,
  });
}

// Get Vendors with filters
export async function getVendors(filters?: VendorFilters) {
  const params: any = {
    search: filters?.search || "",
    page: filters?.page || 1,
    limit: filters?.limit || 10,
  };

  // Only add type_id to params if it's defined and not "all"
  if (filters?.type_id && filters.type_id !== "all") {
    params.type_id = filters.type_id;
  }

  if (filters?.column_name) {
    params.column_name = filters.column_name;
  }

  if (filters?.order) {
    params.order = filters.order;
  }

  return ApiService.request<VendorListResponse>({
    url: "/vendors",
    method: "get",
    params,
  });
}

// Get Vendor by ID
export async function getVendorById(id: string) {
  return ApiService.request<VendorResponse>({
    url: `/vendors/${id}`,
    method: "get",
  });
}

// Update Vendor
export async function updateVendor(data: UpdateVendorData) {
  const { id, ...updateData } = data;
  return ApiService.request<VendorResponse>({
    url: `/vendors/${id}`,
    method: "put",
    data: updateData,
  });
}

export interface DeleteVendorResponse {
  success: boolean;
  message?: string;
}

// Delete Vendor
export async function deleteVendor(id: string) {
  return ApiService.request<DeleteVendorResponse>({
    url: `/vendors/${id}`,
    method: "delete",
  });
}
