import apiClient from './apiClient';

export interface InvoiceItem {
  id: number;
  description: string;
  quantity: number;
  unit_price: number;
  tax_rate: number;
  discount_amount: number;
  line_total: number;
  room_id?: number;
  service_id?: number;
}

export interface Invoice {
  id: number;
  invoice_number: string;
  booking_id: number;
  user_id: number;
  issue_date: string;
  due_date: string;
  paid_date?: string;
  subtotal: number;
  tax_rate: number;
  tax_amount: number;
  discount_amount: number;
  total_amount: number;
  amount_paid: number;
  balance_due: number;
  status: 'draft' | 'sent' | 'paid' | 'overdue' | 'cancelled';
  company_name?: string;
  company_address?: string;
  company_phone?: string;
  company_email?: string;
  company_tax_id?: string;
  company_logo_url?: string;
  customer_name: string;
  customer_email: string;
  customer_address?: string;
  customer_phone?: string;
  customer_tax_id?: string;
  notes?: string;
  terms_and_conditions?: string;
  payment_instructions?: string;
  is_proforma?: boolean;
  promotion_code?: string;
  items: InvoiceItem[];
  created_at: string;
  updated_at: string;
}

export interface InvoiceResponse {
  status: string;
  message?: string;
  data: {
    invoice?: Invoice;
    invoices?: Invoice[];
    total?: number;
    page?: number;
    limit?: number;
    total_pages?: number;
  };
}

export interface CreateInvoiceData {
  booking_id: number;
  tax_rate?: number;
  discount_amount?: number;
  due_days?: number;
  company_name?: string;
  company_address?: string;
  company_phone?: string;
  company_email?: string;
  company_tax_id?: string;
  company_logo_url?: string;
  customer_tax_id?: string;
  notes?: string;
  terms_and_conditions?: string;
  payment_instructions?: string;
}

export interface UpdateInvoiceData {
  company_name?: string;
  company_address?: string;
  company_phone?: string;
  company_email?: string;
  company_tax_id?: string;
  company_logo_url?: string;
  notes?: string;
  terms_and_conditions?: string;
  payment_instructions?: string;
  status?: string;
  due_date?: string;
  tax_rate?: number;
  discount_amount?: number;
}

export const getInvoices = async (params?: {
  booking_id?: number;
  status?: string;
  page?: number;
  limit?: number;
}): Promise<InvoiceResponse> => {
  const response = await apiClient.get<InvoiceResponse>('/invoices', { params });
  return response.data;
};

export const getInvoiceById = async (id: number): Promise<InvoiceResponse> => {
  const response = await apiClient.get<InvoiceResponse>(`/invoices/${id}`);
  return response.data;
};

export const getInvoicesByBooking = async (bookingId: number): Promise<InvoiceResponse> => {
  const response = await apiClient.get<InvoiceResponse>(`/invoices/booking/${bookingId}`);
  return response.data;
};

export const createInvoice = async (data: CreateInvoiceData): Promise<InvoiceResponse> => {
  const response = await apiClient.post<InvoiceResponse>('/invoices', data);
  return response.data;
};

export const updateInvoice = async (id: number, data: UpdateInvoiceData): Promise<InvoiceResponse> => {
  const response = await apiClient.put<InvoiceResponse>(`/invoices/${id}`, data);
  return response.data;
};

export const markInvoiceAsPaid = async (id: number, amount?: number): Promise<InvoiceResponse> => {
  const response = await apiClient.post<InvoiceResponse>(`/invoices/${id}/mark-paid`, { amount });
  return response.data;
};

export const deleteInvoice = async (id: number): Promise<{ status: string; message: string }> => {
  const response = await apiClient.delete<{ status: string; message: string }>(`/invoices/${id}`);
  return response.data;
};

const invoiceService = {
  getInvoices,
  getInvoiceById,
  getInvoicesByBooking,
  createInvoice,
  updateInvoice,
  markInvoiceAsPaid,
  deleteInvoice,
};

export default invoiceService;

