/**
 * API для подписок и оплаты
 */

import apiClient from '@/lib/api-client';

export interface Subscription {
  id: number;
  plan: { id: number; name: string };
  start_date: string;
  end_date: string;
  student_count?: number;
}

export async function getActiveSubscription(): Promise<Subscription | null> {
  try {
    const response = await apiClient.get<any>('/subscriptions/subscriptions/active/');
    return response.data;
  } catch {
    return null;
  }
}

export interface ActivateFreeParams {
  plan_id: number;
  duration_days?: number;
  student_count?: number;
}

/** Активировать бесплатный тариф (цена 0) без создания платежа */
export async function activateFreeSubscription(params: ActivateFreeParams): Promise<{ success: boolean; subscription: Subscription }> {
  const url = '/subscriptions/subscriptions/activate_free/';
  if (typeof window !== 'undefined') {
    console.log('[API] POST', url, params);
  }
  const response = await apiClient.post<{ success: boolean; subscription: Subscription }>(url, params);
  return response.data;
}
