'use server';


import { isNextRedirectError } from '@/lib/utils/redirect-error';
import { serverFetch } from '@/lib/api/fetch-wrapper';
import type { Rank } from './types';

// ===== API 응답 타입 =====
interface PositionApiData {
  id: number;
  tenant_id: number;
  type: 'rank' | 'title';
  name: string;
  sort_order: number;
  is_active: boolean;
  created_at?: string;
  updated_at?: string;
}

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

// ===== 데이터 변환: API → Frontend =====
function transformApiToFrontend(apiData: PositionApiData): Rank {
  return {
    id: apiData.id,
    name: apiData.name,
    order: apiData.sort_order,
    isActive: apiData.is_active,
    createdAt: apiData.created_at,
    updatedAt: apiData.updated_at,
  };
}

// ===== 직급 목록 조회 =====
export async function getRanks(params?: {
  is_active?: boolean;
  q?: string;
}): Promise<{
  success: boolean;
  data?: Rank[];
  error?: string;
  __authError?: boolean;
}> {
  try {
    const searchParams = new URLSearchParams();
    searchParams.set('type', 'rank');

    if (params?.is_active !== undefined) {
      searchParams.set('is_active', params.is_active.toString());
    }
    if (params?.q) {
      searchParams.set('q', params.q);
    }

    const url = `${process.env.NEXT_PUBLIC_API_URL}/api/v1/positions?${searchParams.toString()}`;

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

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

    if (!response) {
      return { success: false, error: '직급 목록 조회에 실패했습니다.' };
    }

    const result: ApiResponse<PositionApiData[]> = await response.json();

    if (!response.ok || !result.success) {
      return { success: false, error: result.message || '직급 목록 조회에 실패했습니다.' };
    }

    const ranks = result.data.map(transformApiToFrontend);
    return { success: true, data: ranks };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[getRanks] Error:', error);
    return { success: false, error: '서버 오류가 발생했습니다.' };
  }
}

// ===== 직급 생성 =====
export async function createRank(data: {
  name: string;
  sort_order?: number;
  is_active?: boolean;
}): Promise<{
  success: boolean;
  data?: Rank;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/positions`,
      {
        method: 'POST',
        body: JSON.stringify({
          type: 'rank',
          name: data.name,
          sort_order: data.sort_order,
          is_active: data.is_active ?? true,
        }),
      }
    );

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

    if (!response) {
      return { success: false, error: '직급 생성에 실패했습니다.' };
    }

    const result: ApiResponse<PositionApiData> = await response.json();

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

    const rank = transformApiToFrontend(result.data);
    return { success: true, data: rank };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[createRank] Error:', error);
    return { success: false, error: '서버 오류가 발생했습니다.' };
  }
}

// ===== 직급 수정 =====
export async function updateRank(
  id: number,
  data: {
    name?: string;
    sort_order?: number;
    is_active?: boolean;
  }
): Promise<{
  success: boolean;
  data?: Rank;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/positions/${id}`,
      {
        method: 'PUT',
        body: JSON.stringify(data),
      }
    );

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

    if (!response) {
      return { success: false, error: '직급 수정에 실패했습니다.' };
    }

    const result: ApiResponse<PositionApiData> = await response.json();

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

    const rank = transformApiToFrontend(result.data);
    return { success: true, data: rank };
  } catch (error) {
    if (isNextRedirectError(error)) throw error;
    console.error('[updateRank] Error:', error);
    return { success: false, error: '서버 오류가 발생했습니다.' };
  }
}

// ===== 직급 삭제 =====
export async function deleteRank(id: number): Promise<{
  success: boolean;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/positions/${id}`,
      {
        method: 'DELETE',
      }
    );

    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('[deleteRank] Error:', error);
    return { success: false, error: '서버 오류가 발생했습니다.' };
  }
}

// ===== 직급 순서 변경 =====
export async function reorderRanks(
  items: { id: number; sort_order: number }[]
): Promise<{
  success: boolean;
  error?: string;
  __authError?: boolean;
}> {
  try {
    const { response, error } = await serverFetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/v1/positions/reorder`,
      {
        method: 'PUT',
        body: JSON.stringify({ items }),
      }
    );

    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('[reorderRanks] Error:', error);
    return { success: false, error: '서버 오류가 발생했습니다.' };
  }
}