'use server';

import { isNextRedirectError } from '@/lib/utils/redirect-error';
import { executeServerAction, type ActionResult } from '@/lib/api/execute-server-action';
import { buildApiUrl } from '@/lib/api/query-params';
import type { SubscriptionApiData, UsageApiData, SubscriptionInfo } from './types';
import { transformApiToFrontend } from './utils';

// ===== 현재 활성 구독 조회 =====
export async function getCurrentSubscription(): Promise<ActionResult<SubscriptionApiData>> {
  return executeServerAction({
    url: buildApiUrl('/api/v1/subscriptions/current'),
    errorMessage: '구독 정보를 불러오는데 실패했습니다.',
  });
}

// ===== 사용량 조회 =====
export async function getUsage(): Promise<ActionResult<UsageApiData>> {
  return executeServerAction({
    url: buildApiUrl('/api/v1/subscriptions/usage'),
    errorMessage: '사용량 정보를 불러오는데 실패했습니다.',
  });
}

// ===== 구독 취소 =====
export async function cancelSubscription(
  id: number,
  reason?: string
): Promise<ActionResult> {
  return executeServerAction({
    url: buildApiUrl(`/api/v1/subscriptions/${id}/cancel`),
    method: 'POST',
    body: { reason },
    errorMessage: '구독 취소에 실패했습니다.',
  });
}

// ===== 데이터 내보내기 요청 =====
export async function requestDataExport(
  exportType: string = 'all'
): Promise<ActionResult<{ id: number; status: string }>> {
  return executeServerAction({
    url: buildApiUrl('/api/v1/subscriptions/export'),
    method: 'POST',
    body: { export_type: exportType },
    transform: (data: { id: number; status: string }) => ({ id: data.id, status: data.status }),
    errorMessage: '내보내기 요청에 실패했습니다.',
  });
}

// ===== 통합 데이터 조회 (현재 구독 + 사용량) =====
export async function getSubscriptionData(): Promise<{
  success: boolean;
  data: SubscriptionInfo | null;
  error?: string;
}> {
  try {
    const [subscriptionResult, usageResult] = await Promise.all([
      getCurrentSubscription(),
      getUsage(),
    ]);

    if (!subscriptionResult.success && !usageResult.success) {
      return {
        success: false,
        data: null,
        error: subscriptionResult.error || usageResult.error,
      };
    }

    const data = transformApiToFrontend(
      subscriptionResult.data ?? null,
      usageResult.data ?? null
    );

    return { success: true, data };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[SubscriptionActions] getSubscriptionData error:', error);
    return { success: false, data: null, error: '서버 오류가 발생했습니다.' };
  }
}
