import { apiClient } from '../api/api-client';
import type { Tenant } from '@/domain/entities/tenant';

export interface TenantRepository {
  list(params?: Record<string, string>): Promise<Tenant[]>;
  getById(id: string): Promise<Tenant>;
  create(tenant: Omit<Tenant, 'id' | 'createdAt' | 'updatedAt' | 'usage'>): Promise<Tenant>;
  update(id: string, data: Partial<Tenant>): Promise<Tenant>;
  remove(id: string): Promise<void>;
}

export const apiTenantRepository: TenantRepository = {
  async list(params) {
    const query = params ? '?' + new URLSearchParams(params).toString() : '';
    return apiClient<Tenant[]>(`/api/v1/admin/tenants${query}`);
  },

  async getById(id) {
    return apiClient<Tenant>(`/api/v1/admin/tenants/${id}`);
  },

  async create(tenant) {
    return apiClient<Tenant>('/api/v1/admin/tenants', {
      method: 'POST',
      body: tenant,
    });
  },

  async update(id, data) {
    return apiClient<Tenant>(`/api/v1/admin/tenants/${id}`, {
      method: 'PUT',
      body: data,
    });
  },

  async remove(id) {
    await apiClient(`/api/v1/admin/tenants/${id}`, {
      method: 'DELETE',
    });
  },
};
