import api, { ApiResponse } from './api';

export interface ContactMessage {
  id: string;
  name: string;
  email: string;
  phone: string | null;
  message: string;
  isRead: boolean;
  createdAt: string;
}

export interface ContactCounts {
  total: number;
  unread: number;
}

class ContactService {
  async getAllMessages(isRead?: boolean): Promise<ContactMessage[]> {
    const params: Record<string, string> = {};
    if (isRead !== undefined) params.isRead = String(isRead);
    const response = await api.get<ApiResponse<ContactMessage[]>>('/contact/admin/all', { params });
    return response.data.data;
  }

  async getCounts(): Promise<ContactCounts> {
    const response = await api.get<ApiResponse<ContactCounts>>('/contact/admin/counts');
    return response.data.data;
  }

  async markAsRead(id: string): Promise<ContactMessage> {
    const response = await api.patch<ApiResponse<ContactMessage>>(`/contact/admin/${id}/read`);
    return response.data.data;
  }

  async deleteMessage(id: string): Promise<void> {
    await api.delete(`/contact/admin/${id}`);
  }
}

export const contactService = new ContactService();
