/**
 * WhatsApp admin tools (legacy CI `whatsapp/*`).
 * Expected Laravel routes under `/api/v1/whatsapp`.
 */
import { apiFetch } from './http'

const BASE = '/api/v1/whatsapp'

export type WhatsappSectionRow = {
  class_section_id?: number
  id?: number
  class_section_name?: string
  name?: string
  school_year?: string
  semester?: string
}

export type WhatsappLinkRow = {
  invite_link?: string | null
  active?: number | boolean
}

export type WhatsappManageLinksResponse = {
  schoolYear?: string | null
  semester?: string | null
  sections?: WhatsappSectionRow[]
  linksBySection?: Record<string, WhatsappLinkRow>
  parents?: Array<{
    id: number
    firstname?: string | null
    lastname?: string | null
    email?: string | null
    source?: string | null
  }>
}

export type ParentContactRow = {
  firstname?: string | null
  lastname?: string | null
  type?: string | null
  phone?: string | null
  email?: string | null
}

export type WhatsappParentContactsResponse = {
  schoolYear?: string | null
  semester?: string | null
  contacts?: ParentContactRow[]
}

export type ParentContactsByClassParentRow = {
  class_section_id?: number
  primary_id?: number
  primary_name?: string
  primary_phone?: string | null
  primary_member?: string | null
  second_id?: number
  second_name?: string
  second_phone?: string | null
  second_member?: string | null
}

export type ParentContactsByClassSection = {
  class_section_id?: number
  class_section_name?: string
  school_year?: string
  semester?: string
  parents?: ParentContactsByClassParentRow[]
}

export type WhatsappParentContactsByClassResponse = {
  classSections?: ParentContactsByClassSection[]
}

export async function fetchWhatsappManageLinks(params?: {
  school_year?: string
  semester?: string
}): Promise<WhatsappManageLinksResponse> {
  const qs = new URLSearchParams()
  if (params?.school_year) qs.set('school_year', params.school_year)
  if (params?.semester) qs.set('semester', params.semester)
  const suffix = qs.toString() ? `?${qs}` : ''
  return apiFetch(`${BASE}/links${suffix}`)
}

export async function saveWhatsappLink(payload: {
  class_section_id: number
  class_section_name?: string
  invite_link: string
  active?: boolean
}): Promise<{ ok?: boolean; message?: string }> {
  return apiFetch(`${BASE}/links`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload),
  })
}

export async function sendWhatsappInvites(payload: {
  mode: 'all' | 'class' | 'parents'
  class_section_id?: number | null
  parent_ids?: number[]
}): Promise<{ ok?: boolean; message?: string }> {
  return apiFetch(`${BASE}/invites/send`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload),
  })
}

export async function fetchWhatsappParentContacts(params?: {
  school_year?: string
  semester?: string
}): Promise<WhatsappParentContactsResponse> {
  const qs = new URLSearchParams()
  if (params?.school_year) qs.set('school_year', params.school_year)
  if (params?.semester) qs.set('semester', params.semester)
  const suffix = qs.toString() ? `?${qs}` : ''
  return apiFetch(`${BASE}/parent-contacts${suffix}`)
}

export async function fetchWhatsappParentContactsByClass(): Promise<WhatsappParentContactsByClassResponse> {
  return apiFetch(`${BASE}/parent-contacts-by-class`)
}

export async function updateWhatsappMembership(payload: {
  class_section_id: number
  school_year?: string
  semester?: string
  primary_id: number
  second_id?: number
  primary_member?: string
  second_member?: string
}): Promise<{ status?: string; message?: string }> {
  return apiFetch(`${BASE}/membership`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload),
  })
}
