import { apiClient } from './client';

export interface TenantSubscription {
  id: string;
  amount: number;
  currentPeriodEnd: string | null;
  status: string;
}

export interface Tenant {
  id: string;
  nombre: string;
  rfc: string;
  plan: string;
  databaseName: string;
  createdAt: string;
  verticalProfile?: 'CONTABLE' | 'JURIDICO' | 'ARQUITECTURA' | null;
  codigoPostal?: string | null;
  _count?: {
    /** Memberships activos (matches el `_count.memberships` que retorna `getAllTenants`). */
    memberships: number;
  };
  subscriptions?: TenantSubscription[];
}

export type TenantPlan = 'trial' | 'custom' | 'mi_empresa' | 'mi_empresa_plus' | 'business_control' | 'business_cloud';

export interface CreateTenantData {
  nombre: string;
  rfc: string;
  plan?: TenantPlan;
  adminEmail: string;
  adminNombre: string;
  amount?: number;
  /** Solo plan custom: deadline para primer pago (formato ISO YYYY-MM-DD). */
  firstPaymentDueAt?: string | null;
  /** Tipo de despacho (CONTABLE, JURIDICO, ARQUITECTURA). Default: CONTABLE */
  verticalProfile?: 'CONTABLE' | 'JURIDICO' | 'ARQUITECTURA';
  /** Código postal del domicilio fiscal (5 dígitos) */
  codigoPostal?: string;
}

export async function getTenants(): Promise<Tenant[]> {
  const response = await apiClient.get<Tenant[]>('/tenants');
  return response.data;
}

export async function getTenant(id: string): Promise<Tenant> {
  const response = await apiClient.get<Tenant>(`/tenants/${id}`);
  return response.data;
}

export async function createTenant(data: CreateTenantData): Promise<Tenant> {
  const response = await apiClient.post<Tenant>('/tenants', data);
  return response.data;
}

export interface UpdateTenantData {
  nombre?: string;
  rfc?: string;
  plan?: TenantPlan;
  active?: boolean;
  amount?: number;
  firstPaymentDueAt?: string | null;
}

export async function updateTenant(id: string, data: UpdateTenantData): Promise<Tenant> {
  const response = await apiClient.put<Tenant>(`/tenants/${id}`, data);
  return response.data;
}

export async function deleteTenant(id: string): Promise<void> {
  await apiClient.delete(`/tenants/${id}`);
}

// ============================================================================
// Self-serve multi-tenant (memberships del caller)
// ============================================================================

export interface MyTenantDetailed {
  tenantId: string;
  nombre: string;
  rfc: string;
  plan: string;
  role: string;
  isOwner: boolean;
  trialEndsAt: string | null;
  subscription: {
    status: string;
    plan: string;
    amount: number;
    frequency: string;
    currentPeriodEnd: string | null;
    pendingPlan: string | null;
    pendingEffectiveAt: string | null;
  } | null;
}

export async function getMyTenants(): Promise<MyTenantDetailed[]> {
  const response = await apiClient.get<MyTenantDetailed[]>('/tenants/mine');
  return response.data;
}

export interface AddMyTenantData {
  nombre: string;
  rfc: string;
  plan?: 'mi_empresa' | 'mi_empresa_plus' | 'business_control' | 'business_cloud';
}

export async function addMyTenant(data: AddMyTenantData): Promise<{ tenant: Tenant }> {
  const response = await apiClient.post<{ tenant: Tenant }>('/tenants/mine', data);
  return response.data;
}
