// src/lib/api/customers.ts
import { api } from "@/src/lib/api/client"

export type CustomerStatus = "active" | "suspended"

export type Customer = {
  id: number
  name: string
  code?: string | null
  email?: string | null
  phone?: string | null
  country?: string | null
  city?: string | null
  address?: string | null
  status: CustomerStatus
  metadata?: any | null

  portal_enabled?: boolean
  created_by?: number | null
  created_at: string
}

export type CustomerCreate = {
  name: string
  code?: string | null
  email?: string | null
  phone?: string | null
  country?: string | null
  city?: string | null
  address?: string | null
  status?: CustomerStatus
  metadata?: any | null
}

export type CustomerUpdate = Partial<CustomerCreate>

export function listCustomers(): Promise<Customer[]> {
  return api.request("/customers/", { method: "GET" })
}

export function createCustomer(payload: CustomerCreate): Promise<Customer> {
  return api.request("/customers/", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  })
}

export function updateCustomer(id: number, payload: CustomerUpdate): Promise<Customer> {
  return api.request(`/customers/${id}/`, {
    method: "PATCH",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  })
}

export function deleteCustomer(id: number): Promise<void> {
  return api.request(`/customers/${id}/`, { method: "DELETE" })
}

export function forceDeleteCustomer(id: number): Promise<{ detail: string; cases_deleted: number }> {
  return api.request(`/customers/${id}/force-delete/`, { method: "DELETE" })
}

// ---- Customer Portal primary user (déjà chez toi) ----
export type CustomerPortalPrimaryUserPayload = {
  email: string
  username?: string
  first_name?: string
  last_name?: string
  telephone?: string
  departement?: string
}

export async function createCustomerPortalPrimaryUser(
  customerId: number,
  payload: CustomerPortalPrimaryUserPayload
): Promise<{ portal_enabled: boolean; user_created: boolean; user_email: string; temporary_password?: string }> {
  return api.request(`/customers/${customerId}/portal/primary-user/`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  })
}

// ---- Memberships (utilisateurs du client) ----
export type CustomerMembershipRole = "owner" | "manager" | "viewer"
export type CustomerMembershipStatus = "invited" | "active" | "suspended"

export type CustomerMembership = {
  id: number
  customer: number
  user: number

  user_label?: string
  user_email?: string
  user_username?: string
  user_first_name?: string
  user_last_name?: string
  user_is_active?: boolean
  user_telephone?: string
  user_departement?: string

  role: CustomerMembershipRole
  status: CustomerMembershipStatus
  is_primary_contact: boolean
  created_at: string
}

export type CustomerMembershipUpdatePayload = {
  // membership
  role?: CustomerMembershipRole
  status?: CustomerMembershipStatus
  is_primary_contact?: boolean

  // user
  email?: string
  username?: string
  first_name?: string
  last_name?: string
  telephone?: string
  departement?: string
  is_active?: boolean
}

export function listCustomerMemberships(customerId: number): Promise<CustomerMembership[]> {
  return api.request(`/customers/${customerId}/memberships/`, { method: "GET" })
}

export function updateCustomerMembership(
  customerId: number,
  membershipId: number,
  payload: CustomerMembershipUpdatePayload
): Promise<CustomerMembership> {
  return api.request(`/customers/${customerId}/memberships/${membershipId}/`, {
    method: "PATCH",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  })
}

export function deleteCustomerMembership(customerId: number, membershipId: number): Promise<void> {
  return api.request(`/customers/${customerId}/memberships/${membershipId}/`, { method: "DELETE" })
}

export function resetCustomerMembershipPassword(
  customerId: number,
  membershipId: number
): Promise<{ detail: string; temporary_password?: string }> {
  return api.request(`/customers/${customerId}/memberships/${membershipId}/reset-password/`, {
    method: "POST",
  })
}
