import api from './api';

export interface Sponsor {
  id: number;
  name: string;
  logo_url?: string;
  website_url?: string;
  is_active: boolean;
  created_at?: string;
  tier?: string; // 'general' for main partners, 'standard' for regular sponsors
  display_order?: number; // For custom ordering
  // Optional banner-specific metadata
  placement?: string; // e.g., homepage_top, homepage_sidebar
  width?: number;
  height?: number;
}

export async function getSponsors(): Promise<Sponsor[]> {
  const res = await api.get<any>('/sponsors');
  const body = res.data;
  const list = Array.isArray(body) ? body : (Array.isArray(body?.data) ? body.data : []);
  return (list || []).map((s: any) => ({
    ...s,
    id: s.id ?? s.ID ?? s.Id ?? s.iD,
  }));
}

export async function createSponsor(payload: { name: string; logo_url?: string; website_url?: string; is_active?: boolean; tier?: string; display_order?: number; placement?: string; width?: number; height?: number }) {
  const res = await api.post<Sponsor>('/sponsors', payload);
  return res.data;
}

export async function updateSponsor(id: number | string, payload: Partial<{ name: string; logo_url?: string; website_url?: string; is_active?: boolean; tier?: string; display_order?: number; placement?: string; width?: number; height?: number }>) {
  const res = await api.put<Sponsor>(`/sponsors/${id}`, payload);
  return res.data;
}

export async function deleteSponsor(id: number | string) {
  const res = await api.delete<{ zprava: string }>(`/sponsors/${id}`);
  return res.data;
}
