import { apiRequest } from '@/shared/api/httpClient';

export interface CreateOrganizationData {
  readonly name: string;
  readonly logo?: string | null;
}

export interface CreateSchoolData {
  readonly name: string;
  readonly code: string;
  /** Target organization (used only for global creators with no own org). */
  readonly organization?: string;
  readonly logo?: string | null;
  readonly description?: string | null;
  readonly address?: string | null;
  readonly phone?: string | null;
  readonly email?: string | null;
}

export interface FirstCampusData {
  readonly name: string;
  readonly timezone: string;
  readonly logo?: string | null;
  readonly address?: string | null;
  readonly phone?: string | null;
  readonly email?: string | null;
}

export interface CreateCampusData {
  readonly name: string;
  readonly code: string;
  readonly timezone: string;
  readonly schoolId?: string;
  readonly logo?: string | null;
  readonly description?: string | null;
  readonly address?: string | null;
  readonly phone?: string | null;
  readonly email?: string | null;
}

export interface CreateClassData {
  readonly name: string;
  readonly campus?: string;
  readonly logo?: string | null;
}

export function updateOrganization(
  id: string,
  data: Partial<CreateOrganizationData>,
): Promise<unknown> {
  return apiRequest(`/organizations/${id}`, {
    method: 'PUT',
    body: { id, data },
  });
}

export function deleteOrganization(id: string): Promise<unknown> {
  return apiRequest(`/organizations/${id}`, { method: 'DELETE' });
}

/** Atomically creates a school and its first campus (Org → School → Campus). */
export function createSchoolWithFirstCampus(
  data: CreateSchoolData,
  firstCampus: FirstCampusData,
): Promise<unknown> {
  return apiRequest('/schools/with-first-campus', {
    method: 'POST',
    body: { data, firstCampus },
  });
}

export function createCampus(data: CreateCampusData): Promise<unknown> {
  return apiRequest('/campuses', { method: 'POST', body: { data } });
}

export function updateSchool(id: string, data: Partial<CreateSchoolData>): Promise<unknown> {
  return apiRequest(`/schools/${id}`, {
    method: 'PUT',
    body: { id, data },
  });
}

export function deleteSchool(id: string): Promise<unknown> {
  return apiRequest(`/schools/${id}`, { method: 'DELETE' });
}

export function updateCampus(id: string, data: Partial<CreateCampusData>): Promise<unknown> {
  return apiRequest(`/campuses/${id}`, {
    method: 'PUT',
    body: { id, data },
  });
}

export function deleteCampus(id: string): Promise<unknown> {
  return apiRequest(`/campuses/${id}`, { method: 'DELETE' });
}

export function createClass(data: CreateClassData): Promise<unknown> {
  return apiRequest('/classes', { method: 'POST', body: { data } });
}

export function updateClass(id: string, data: Partial<CreateClassData>): Promise<unknown> {
  return apiRequest(`/classes/${id}`, {
    method: 'PUT',
    body: { id, data },
  });
}

export function deleteClass(id: string): Promise<unknown> {
  return apiRequest(`/classes/${id}`, { method: 'DELETE' });
}
