/**
 * 기안함 서버 액션
 *
 * API Endpoints:
 * - GET /api/v1/approvals/drafts - 기안함 목록 조회
 * - GET /api/v1/approvals/drafts/summary - 기안함 현황 카드
 * - GET /api/v1/approvals/{id} - 결재 문서 상세
 * - DELETE /api/v1/approvals/{id} - 결재 문서 삭제 (임시저장만)
 * - POST /api/v1/approvals/{id}/submit - 결재 상신
 * - POST /api/v1/approvals/{id}/cancel - 결재 회수
 */

'use server';


import { executeServerAction, type ActionResult } from '@/lib/api/execute-server-action';
import { buildApiUrl } from '@/lib/api/query-params';
import type { PaginatedApiResponse } from '@/lib/api/types';
import type { DraftRecord, DocumentStatus, Approver } from './types';
import { formatDate } from '@/lib/utils/date';

// ============================================
// API 응답 타입 정의
// ============================================

interface DraftsSummary {
  total: number;
  draft: number;
  pending: number;
  approved: number;
  rejected: number;
}

// API 응답의 결재 문서 타입
interface ApprovalApiData {
  id: number;
  document_number: string;
  title: string;
  status: string;
  form?: {
    id: number;
    name: string;
    code: string;
    category: string;
  };
  drafter?: {
    id: number;
    name: string;
  };
  steps?: ApprovalStepApiData[];
  content?: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

interface ApprovalStepApiData {
  id: number;
  step_order: number;
  step_type: string;
  approver_id: number;
  approver?: {
    id: number;
    name: string;
    tenant_profile?: {
      position_key?: string;
      department?: { id: number; name: string };
    };
  };
  status: string;
  processed_at?: string;
  comment?: string;
}

// ============================================
// 헬퍼 함수
// ============================================

/**
 * API 상태 → 프론트엔드 상태 변환
 */
function mapApiStatus(apiStatus: string): DocumentStatus {
  const statusMap: Record<string, DocumentStatus> = {
    'draft': 'draft',
    'pending': 'pending',
    'in_progress': 'inProgress',
    'approved': 'approved',
    'rejected': 'rejected',
    'cancelled': 'cancelled',
    'on_hold': 'on_hold',
  };
  return statusMap[apiStatus] || 'draft';
}

/**
 * 결재자 상태 변환
 */
function mapApproverStatus(stepStatus: string): Approver['status'] {
  const statusMap: Record<string, Approver['status']> = {
    'pending': 'pending',
    'approved': 'approved',
    'rejected': 'rejected',
    'skipped': 'skipped',
    'on_hold': 'on_hold',
  };
  return statusMap[stepStatus] || 'none';
}

/**
 * 직책 코드 → 한글 변환
 */
function getPositionLabel(positionKey: string | null | undefined): string {
  if (!positionKey) return '';
  const labels: Record<string, string> = {
    'EXECUTIVE': '임원',
    'DIRECTOR': '부장',
    'MANAGER': '과장',
    'SENIOR': '대리',
    'STAFF': '사원',
    'INTERN': '인턴',
  };
  return labels[positionKey] ?? positionKey;
}

/**
 * API 데이터 → 프론트엔드 데이터 변환
 */
function transformApiToFrontend(data: ApprovalApiData): DraftRecord {
  // approval 타입 결재자만 필터링 (reference 제외)
  const approvers: Approver[] = (data.steps || [])
    .filter((step) => step.step_type === 'approval')
    .map((step) => ({
      id: String(step.approver_id),
      name: step.approver?.name || '',
      position: getPositionLabel(step.approver?.tenant_profile?.position_key),
      department: step.approver?.tenant_profile?.department?.name || '',
      status: mapApproverStatus(step.status),
      approvedAt: step.processed_at,
    }));

  // drafter의 tenant_profile에서 직책/부서 추출
  const drafterProfile = (data.drafter as { tenant_profile?: { position_key?: string; department?: { name: string } } })?.tenant_profile;

  return {
    id: String(data.id),
    documentNo: data.document_number,
    documentType: data.form?.name || '',
    documentTypeCode: data.form?.code || 'proposal',
    title: data.title,
    draftDate: formatDate(data.created_at),
    drafter: data.drafter?.name || '',
    drafterPosition: getPositionLabel(drafterProfile?.position_key),
    drafterDepartment: drafterProfile?.department?.name || '',
    approvers,
    status: mapApiStatus(data.status),
    content: data.content,
    createdAt: data.created_at,
    updatedAt: data.updated_at,
  };
}

const DRAFT_STATUS_MAP: Record<string, string> = {
  'draft': 'draft', 'pending': 'pending', 'inProgress': 'in_progress',
  'approved': 'approved', 'rejected': 'rejected', 'cancelled': 'cancelled', 'on_hold': 'on_hold',
};

// ============================================
// API 함수
// ============================================

export async function getDrafts(params?: {
  page?: number; per_page?: number; search?: string; status?: string;
  sort_by?: string; sort_dir?: 'asc' | 'desc';
}): Promise<{ data: DraftRecord[]; total: number; lastPage: number; __authError?: boolean }> {
  const result = await executeServerAction<PaginatedApiResponse<ApprovalApiData>>({
    url: buildApiUrl('/api/v1/approvals/drafts', {
      page: params?.page,
      per_page: params?.per_page,
      search: params?.search,
      status: params?.status && params.status !== 'all' ? (DRAFT_STATUS_MAP[params.status] || params.status) : undefined,
      sort_by: params?.sort_by,
      sort_dir: params?.sort_dir,
    }),
    errorMessage: '기안함 목록 조회에 실패했습니다.',
  });

  if (result.__authError) return { data: [], total: 0, lastPage: 1, __authError: true };
  if (!result.success || !result.data?.data) return { data: [], total: 0, lastPage: 1 };

  return {
    data: result.data.data.map(transformApiToFrontend),
    total: result.data.total,
    lastPage: result.data.last_page,
  };
}

export async function getDraftsSummary(): Promise<DraftsSummary | null> {
  const result = await executeServerAction<DraftsSummary>({
    url: buildApiUrl('/api/v1/approvals/drafts/summary'),
    errorMessage: '기안함 현황 조회에 실패했습니다.',
  });
  return result.success ? result.data || null : null;
}

export async function getDraftById(id: string): Promise<DraftRecord | null> {
  const result = await executeServerAction({
    url: buildApiUrl(`/api/v1/approvals/${id}`),
    transform: (data: ApprovalApiData) => transformApiToFrontend(data),
    errorMessage: '결재 문서 조회에 실패했습니다.',
  });
  return result.success ? result.data || null : null;
}

export async function deleteDraft(id: string): Promise<ActionResult> {
  return executeServerAction({
    url: buildApiUrl(`/api/v1/approvals/${id}`),
    method: 'DELETE',
    errorMessage: '결재 문서 삭제에 실패했습니다.',
  });
}

export async function deleteDrafts(ids: string[]): Promise<{ success: boolean; failedIds?: string[]; error?: string }> {
  const failedIds: string[] = [];
  for (const id of ids) {
    const result = await deleteDraft(id);
    if (!result.success) failedIds.push(id);
  }
  if (failedIds.length > 0) {
    return { success: false, failedIds, error: `${failedIds.length}건의 삭제에 실패했습니다.` };
  }
  return { success: true };
}

export async function submitDraft(id: string): Promise<ActionResult> {
  return executeServerAction({
    url: buildApiUrl(`/api/v1/approvals/${id}/submit`),
    method: 'POST',
    body: {},
    errorMessage: '결재 상신에 실패했습니다.',
  });
}

export async function submitDrafts(ids: string[]): Promise<{ success: boolean; failedIds?: string[]; error?: string }> {
  const failedIds: string[] = [];
  for (const id of ids) {
    const result = await submitDraft(id);
    if (!result.success) failedIds.push(id);
  }
  if (failedIds.length > 0) {
    return { success: false, failedIds, error: `${failedIds.length}건의 상신에 실패했습니다.` };
  }
  return { success: true };
}

export async function cancelDraft(id: string): Promise<ActionResult> {
  return executeServerAction({
    url: buildApiUrl(`/api/v1/approvals/${id}/cancel`),
    method: 'POST',
    body: {},
    errorMessage: '결재 회수에 실패했습니다.',
  });
}

export async function copyApproval(id: string): Promise<ActionResult<{ id: number }>> {
  return executeServerAction<{ id: number }>({
    url: buildApiUrl(`/api/v1/approvals/${id}/copy`),
    method: 'POST',
    body: {},
    errorMessage: '문서 복사에 실패했습니다.',
  });
}
