// Backend API hosted on emily.sammiehosty.com
const API_BASE_URL = 'https://emily.sammiehosty.com';

export const api = {
  dbStatus: () => fetch(`${API_BASE_URL}/api/db-status.php`),
  
  auth: (password: string) =>
    fetch(`${API_BASE_URL}/api/auth.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ password }),
    }),

  // Dashboard
  getDashboard: () => fetch(`${API_BASE_URL}/api/dashboard.php`),
  
  getWaStatus: () => fetch(`${API_BASE_URL}/api/wa-status.php`),

  // Members
  getMembers: () => fetch(`${API_BASE_URL}/api/members.php`),
  
  addMember: (name: string, birthMonth: number, birthDay: number) =>
    fetch(`${API_BASE_URL}/api/members.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name, birthMonth, birthDay }),
    }),

  updateMember: (id: number, name: string, birthMonth: number, birthDay: number) =>
    fetch(`${API_BASE_URL}/api/members.php`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ id, name, birthMonth, birthDay }),
    }),

  deleteMember: (id: number) =>
    fetch(`${API_BASE_URL}/api/members.php`, {
      method: 'DELETE',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ id }),
    }),

  // Settings
  getSettings: () => fetch(`${API_BASE_URL}/api/settings.php`),
  
  updateSettings: (updates: Record<string, string>) =>
    fetch(`${API_BASE_URL}/api/settings.php`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(updates),
    }),

  // Logs
  getLogs: () => fetch(`${API_BASE_URL}/api/logs.php`),

  // Messages
  sendMessage: (memberName: string, messageType: string, message: string) =>
    fetch(`${API_BASE_URL}/api/send-message.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ memberName, messageType, message }),
    }),

  testSend: (memberId: number, messageType: string) =>
    fetch(`${API_BASE_URL}/api/test-send.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ memberId, messageType }),
    }),

  // Scheduled Messages
  getScheduledMessages: (filter: 'all' | 'pending' | 'sent' = 'all') =>
    fetch(`${API_BASE_URL}/api/scheduled-messages.php?filter=${filter}`),

  addScheduledMessage: (message: string, scheduledAt: string) =>
    fetch(`${API_BASE_URL}/api/scheduled-messages.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ message, scheduledAt }),
    }),

  updateScheduledMessage: (id: number, message: string, scheduledAt: string) =>
    fetch(`${API_BASE_URL}/api/scheduled-messages.php`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ id, message, scheduledAt }),
    }),

  deleteScheduledMessage: (id: number) =>
    fetch(`${API_BASE_URL}/api/scheduled-messages.php`, {
      method: 'DELETE',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ id }),
    }),

  // Contributions
  getContributions: (month: string) =>
    fetch(`${API_BASE_URL}/api/contributions.php?month=${month}`),

  updateContributions: (yearMonth: string, updates: Array<{
    memberId: number;
    week1: boolean;
    week2: boolean;
    week3: boolean;
    week4: boolean;
    week5: boolean;
  }>) =>
    fetch(`${API_BASE_URL}/api/contributions.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ yearMonth, updates }),
    }),

  sendContributionReport: (yearMonth: string) =>
    fetch(`${API_BASE_URL}/api/send-contribution-report.php`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ yearMonth }),
    }),
};

export const getApiBaseUrl = () => API_BASE_URL;
