import apiClient from './client'
import type { OrganizationalUnit, CreateOrganizationRequest } from '@/types'

export const organizationsApi = {
  async getAll(): Promise<OrganizationalUnit[]> {
    const response = await apiClient.get<OrganizationalUnit[]>('/organizations')
    return response.data
  },

  async getById(id: number): Promise<OrganizationalUnit> {
    const response = await apiClient.get<OrganizationalUnit>(`/organizations/${id}`)
    return response.data
  },

  async getSubordinates(id: number): Promise<OrganizationalUnit[]> {
    const response = await apiClient.get<OrganizationalUnit[]>(`/organizations/${id}/subordinates`)
    return response.data
  },

  async getAllocationUnits(id: number): Promise<OrganizationalUnit[]> {
    const response = await apiClient.get<OrganizationalUnit[]>(`/organizations/${id}/allocation-units`)
    return response.data
  },

  async create(data: CreateOrganizationRequest): Promise<OrganizationalUnit> {
    const response = await apiClient.post<OrganizationalUnit>('/organizations', data)
    return response.data
  },

  async update(id: number, data: Partial<CreateOrganizationRequest>): Promise<OrganizationalUnit> {
    const response = await apiClient.put<OrganizationalUnit>(`/organizations/${id}`, data)
    return response.data
  },

  async delete(id: number): Promise<void> {
    await apiClient.delete(`/organizations/${id}`)
  },

  async createAccount(organizationId: number, accountData: { username: string; password: string }): Promise<void> {
    await apiClient.post(`/organizations/${organizationId}/accounts`, accountData)
  },

  async getAccounts(organizationId: number): Promise<any[]> {
    const response = await apiClient.get<any[]>(`/organizations/${organizationId}/accounts`)
    return response.data
  },

  async deleteAccount(organizationId: number, accountId: number): Promise<void> {
    await apiClient.delete(`/organizations/${organizationId}/accounts/${accountId}`)
  }
}
