'use server';

/**
 * 거래처(영업) 서버 액션
 *
 * API Endpoints:
 * - GET /api/v1/clients/{id} - 상세 조회
 * - POST /api/v1/clients - 등록
 * - PUT /api/v1/clients/{id} - 수정
 * - DELETE /api/v1/clients/{id} - 삭제
 *
 * 🚨 401 에러 시 __authError: true 반환 → 클라이언트에서 로그인 페이지로 리다이렉트
 */

import { isNextRedirectError } from '@/lib/utils/redirect-error';
import { serverFetch } from '@/lib/api/fetch-wrapper';
import type { Client, ClientFormData, ClientApiResponse } from '@/hooks/useClientList';
import {
  transformClientFromApi,
  transformClientToApiCreate,
  transformClientToApiUpdate,
} from '@/hooks/useClientList';

// ===== 응답 타입 =====
interface ActionResponse<T = unknown> {
  success: boolean;
  data?: T;
  error?: string;
  __authError?: boolean;
}

interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
}

// ===== 거래처 단건 조회 =====
export async function getClientById(id: string): Promise<ActionResponse<Client>> {
  try {
    const url = `${process.env.NEXT_PUBLIC_API_URL}/api/v1/clients/${id}`;
    console.log('[ClientActions] GET client URL:', url);

    const { response, error } = await serverFetch(url, { method: 'GET' });

    // 🚨 401 인증 에러
    if (error?.__authError) {
      console.error('[ClientActions] Auth error:', error);
      return { success: false, __authError: true };
    }

    if (!response) {
      console.error('[ClientActions] No response, error:', error);
      return {
        success: false,
        error: error?.message || '서버 응답이 없습니다.',
      };
    }

    // 응답 텍스트 먼저 읽기
    const responseText = await response.text();
    console.log('[ClientActions] Response status:', response.status);
    console.log('[ClientActions] Response text:', responseText);

    if (!response.ok) {
      console.error('[ClientActions] GET client error:', response.status);
      return {
        success: false,
        error: `API 오류: ${response.status}`,
      };
    }

    // JSON 파싱
    let result: ApiResponse<ClientApiResponse>;
    try {
      result = JSON.parse(responseText);
    } catch {
      console.error('[ClientActions] JSON parse error');
      return {
        success: false,
        error: 'JSON 파싱 오류',
      };
    }

    if (!result.success || !result.data) {
      console.error('[ClientActions] API returned error:', result);
      return {
        success: false,
        error: result.message || '거래처를 찾을 수 없습니다.',
      };
    }

    return {
      success: true,
      data: transformClientFromApi(result.data),
    };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[ClientActions] getClientById error:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : '거래처 조회 중 오류가 발생했습니다.',
    };
  }
}

// ===== 거래처 생성 =====
export async function createClient(
  formData: Partial<ClientFormData>
): Promise<ActionResponse<Client>> {
  try {
    const apiData = transformClientToApiCreate(formData as ClientFormData);
    const url = `${process.env.NEXT_PUBLIC_API_URL}/api/v1/clients`;

    console.log('[ClientActions] POST client request:', apiData);

    const { response, error } = await serverFetch(url, {
      method: 'POST',
      body: JSON.stringify(apiData),
    });

    // 🚨 401 인증 에러
    if (error?.__authError) {
      return { success: false, __authError: true };
    }

    if (!response) {
      return {
        success: false,
        error: error?.message || '서버 오류가 발생했습니다.',
      };
    }

    const result: ApiResponse<ClientApiResponse> = await response.json();
    console.log('[ClientActions] POST client response:', result);

    if (!response.ok || !result.success) {
      return {
        success: false,
        error: result.message || '거래처 생성에 실패했습니다.',
      };
    }

    return {
      success: true,
      data: transformClientFromApi(result.data),
    };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[ClientActions] createClient error:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : '거래처 생성 중 오류가 발생했습니다.',
    };
  }
}

// ===== 거래처 수정 =====
export async function updateClient(
  id: string,
  formData: Partial<ClientFormData>
): Promise<ActionResponse<Client>> {
  try {
    const apiData = transformClientToApiUpdate(formData as ClientFormData);
    const url = `${process.env.NEXT_PUBLIC_API_URL}/api/v1/clients/${id}`;

    console.log('[ClientActions] PUT client request:', apiData);

    const { response, error } = await serverFetch(url, {
      method: 'PUT',
      body: JSON.stringify(apiData),
    });

    // 🚨 401 인증 에러
    if (error?.__authError) {
      return { success: false, __authError: true };
    }

    if (!response) {
      return {
        success: false,
        error: error?.message || '서버 오류가 발생했습니다.',
      };
    }

    const result: ApiResponse<ClientApiResponse> = await response.json();
    console.log('[ClientActions] PUT client response:', result);

    if (!response.ok || !result.success) {
      return {
        success: false,
        error: result.message || '거래처 수정에 실패했습니다.',
      };
    }

    return {
      success: true,
      data: transformClientFromApi(result.data),
    };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[ClientActions] updateClient error:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : '거래처 수정 중 오류가 발생했습니다.',
    };
  }
}

// ===== 거래처 삭제 =====
export async function deleteClient(id: string): Promise<ActionResponse> {
  try {
    const url = `${process.env.NEXT_PUBLIC_API_URL}/api/v1/clients/${id}`;
    const { response, error } = await serverFetch(url, { method: 'DELETE' });

    // 🚨 401 인증 에러
    if (error?.__authError) {
      return { success: false, __authError: true };
    }

    if (!response) {
      return {
        success: false,
        error: error?.message || '서버 오류가 발생했습니다.',
      };
    }

    const result = await response.json();
    console.log('[ClientActions] DELETE client response:', result);

    if (!response.ok || !result.success) {
      return {
        success: false,
        error: result.message || '거래처 삭제에 실패했습니다.',
      };
    }

    return { success: true };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[ClientActions] deleteClient error:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : '거래처 삭제 중 오류가 발생했습니다.',
    };
  }
}

// ===== 거래처 코드 생성 (8자리 영문+숫자) =====
export async function generateClientCode(): Promise<string> {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  let result = '';
  for (let i = 0; i < 8; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return result;
}
