'use server';


import { isNextRedirectError } from '@/lib/utils/redirect-error';
import { serverFetch } from '@/lib/api/fetch-wrapper';
import type { SubscriptionApiData, UsageApiData, SubscriptionInfo } from './types';
import { transformApiToFrontend } from './utils';

// ===== 현재 활성 구독 조회 =====
export async function getCurrentSubscription(): Promise<{
  success: boolean;
  data: SubscriptionApiData | null;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/current`,
      {
        method: 'GET',
        cache: 'no-store',
      }
    );

    if (error) {
      return {
        success: false,
        data: null,
        error: error.message,
        __authError: error.code === 'UNAUTHORIZED',
      };
    }

    if (!response) {
      return {
        success: false,
        data: null,
        error: '구독 정보를 불러오는데 실패했습니다.',
      };
    }

    const result = await response.json();

    if (!response.ok || !result.success) {
      return {
        success: false,
        data: null,
        error: result.message || '구독 정보를 불러오는데 실패했습니다.',
      };
    }

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

// ===== 사용량 조회 =====
export async function getUsage(): Promise<{
  success: boolean;
  data: UsageApiData | null;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/usage`,
      {
        method: 'GET',
        cache: 'no-store',
      }
    );

    if (error) {
      return {
        success: false,
        data: null,
        error: error.message,
        __authError: error.code === 'UNAUTHORIZED',
      };
    }

    if (!response) {
      return {
        success: false,
        data: null,
        error: '사용량 정보를 불러오는데 실패했습니다.',
      };
    }

    const result = await response.json();

    if (!response.ok || !result.success) {
      return {
        success: false,
        data: null,
        error: result.message || '사용량 정보를 불러오는데 실패했습니다.',
      };
    }

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

// ===== 구독 취소 =====
export async function cancelSubscription(
  id: number,
  reason?: string
): Promise<{
  success: boolean;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/${id}/cancel`,
      {
        method: 'POST',
        body: JSON.stringify({ reason }),
      }
    );

    if (error) {
      return {
        success: false,
        error: error.message,
        __authError: error.code === 'UNAUTHORIZED',
      };
    }

    if (!response) {
      return {
        success: false,
        error: '구독 취소에 실패했습니다.',
      };
    }

    const result = await response.json();

    if (!response.ok || !result.success) {
      return {
        success: false,
        error: result.message || '구독 취소에 실패했습니다.',
      };
    }

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

// ===== 데이터 내보내기 요청 =====
export async function requestDataExport(
  exportType: string = 'all'
): Promise<{
  success: boolean;
  data?: { id: number; status: string };
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/export`,
      {
        method: 'POST',
        body: JSON.stringify({ export_type: exportType }),
      }
    );

    if (error) {
      return {
        success: false,
        error: error.message,
        __authError: error.code === 'UNAUTHORIZED',
      };
    }

    if (!response) {
      return {
        success: false,
        error: '내보내기 요청에 실패했습니다.',
      };
    }

    const result = await response.json();

    if (!response.ok || !result.success) {
      return {
        success: false,
        error: result.message || '내보내기 요청에 실패했습니다.',
      };
    }

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

// ===== 통합 데이터 조회 (현재 구독 + 사용량) =====
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,
      usageResult.data
    );

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