'use server';

import type { StructureReview, StructureReviewStats, StructureReviewStatus } from './types';
import { apiClient } from '@/lib/api';

/**
 * 구조검토관리 Server Actions
 * 표준화된 apiClient 사용 버전
 */

// ========================================
// API 응답 타입
// ========================================

interface ApiStructureReview {
  id: number;
  review_number: string | null;
  partner_id: number | null;
  partner_name: string | null;
  site_id: number | null;
  site_name: string | null;
  request_date: string | null;
  review_company: string | null;
  reviewer_name: string | null;
  review_date: string | null;
  completion_date: string | null;
  status: StructureReviewStatus;
  file_url: string | null;
  created_at: string;
  updated_at: string;
}

interface ApiStructureReviewStats {
  total: number;
  pending: number;
  completed: number;
}

// ========================================
// 타입 변환 함수
// ========================================

/**
 * API 응답 → StructureReview 타입 변환
 */
function transformStructureReview(apiData: ApiStructureReview): StructureReview {
  return {
    id: String(apiData.id),
    reviewNumber: apiData.review_number || '',
    partnerId: apiData.partner_id ? String(apiData.partner_id) : '',
    partnerName: apiData.partner_name || '',
    siteId: apiData.site_id ? String(apiData.site_id) : '',
    siteName: apiData.site_name || '',
    requestDate: apiData.request_date || '',
    reviewCompany: apiData.review_company || '',
    reviewerName: apiData.reviewer_name || '',
    reviewDate: apiData.review_date || null,
    completionDate: apiData.completion_date || null,
    status: apiData.status || 'pending',
    fileUrl: apiData.file_url || undefined,
    createdAt: apiData.created_at || '',
    updatedAt: apiData.updated_at || '',
  };
}

/**
 * StructureReview → API 요청 데이터 변환
 */
function transformToApiData(data: Partial<StructureReview>): Record<string, unknown> {
  return {
    review_number: data.reviewNumber || null,
    partner_id: data.partnerId ? Number(data.partnerId) : null,
    partner_name: data.partnerName || null,
    site_id: data.siteId ? Number(data.siteId) : null,
    site_name: data.siteName || null,
    request_date: data.requestDate || null,
    review_company: data.reviewCompany || null,
    reviewer_name: data.reviewerName || null,
    review_date: data.reviewDate || null,
    completion_date: data.completionDate || null,
    status: data.status || 'pending',
    file_url: data.fileUrl || null,
  };
}

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

interface GetStructureReviewListParams {
  size?: number;
  page?: number;
  startDate?: string;
  endDate?: string;
  search?: string;
  status?: string;
  partnerId?: string;
  siteId?: string;
  sortBy?: string;
}

/**
 * 구조검토 목록 조회
 * GET /api/v1/construction/structure-reviews
 */
export async function getStructureReviewList(params: GetStructureReviewListParams = {}): Promise<{
  success: boolean;
  data?: {
    items: StructureReview[];
    total: number;
    page: number;
    size: number;
    totalPages: number;
  };
  error?: string;
}> {
  try {
    const queryParams: Record<string, string> = {};

    // 검색
    if (params.search) queryParams.search = params.search;

    // 필터
    if (params.status && params.status !== 'all') queryParams.status = params.status;
    if (params.partnerId && params.partnerId !== 'all') queryParams.partner_id = params.partnerId;
    if (params.siteId && params.siteId !== 'all') queryParams.site_id = params.siteId;

    // 날짜 범위
    if (params.startDate) queryParams.start_date = params.startDate;
    if (params.endDate) queryParams.end_date = params.endDate;

    // 페이지네이션
    if (params.page) queryParams.page = String(params.page);
    if (params.size) queryParams.per_page = String(params.size);

    // 정렬
    if (params.sortBy) {
      const sortMap: Record<string, { field: string; dir: string }> = {
        latest: { field: 'created_at', dir: 'desc' },
        oldest: { field: 'created_at', dir: 'asc' },
        partnerNameAsc: { field: 'partner_name', dir: 'asc' },
        partnerNameDesc: { field: 'partner_name', dir: 'desc' },
        siteNameAsc: { field: 'site_name', dir: 'asc' },
        siteNameDesc: { field: 'site_name', dir: 'desc' },
      };
      const sort = sortMap[params.sortBy];
      if (sort) {
        queryParams.sort_by = sort.field;
        queryParams.sort_dir = sort.dir;
      }
    }

    const response = await apiClient.get<{
      success: boolean;
      data: {
        data: ApiStructureReview[];
        current_page: number;
        per_page: number;
        total: number;
        last_page: number;
      };
    }>('/construction/structure-reviews', { params: queryParams });

    const paginatedData = response.data;
    const items = (paginatedData.data || []).map(transformStructureReview);

    return {
      success: true,
      data: {
        items,
        total: paginatedData.total || 0,
        page: paginatedData.current_page || 1,
        size: paginatedData.per_page || 20,
        totalPages: paginatedData.last_page || 1,
      },
    };
  } catch (error) {
    console.error('구조검토 목록 조회 오류:', error);
    return { success: false, error: '구조검토 목록을 불러오는데 실패했습니다.' };
  }
}

/**
 * 구조검토 통계 조회
 * GET /api/v1/construction/structure-reviews/stats
 */
export async function getStructureReviewStats(): Promise<{
  success: boolean;
  data?: StructureReviewStats;
  error?: string;
}> {
  try {
    const response = await apiClient.get<{
      success: boolean;
      data: ApiStructureReviewStats;
    }>('/construction/structure-reviews/stats');

    const statsData = response.data;
    return {
      success: true,
      data: {
        total: statsData.total || 0,
        pending: statsData.pending || 0,
        completed: statsData.completed || 0,
      },
    };
  } catch (error) {
    console.error('구조검토 통계 조회 오류:', error);
    return { success: false, error: '구조검토 통계를 불러오는데 실패했습니다.' };
  }
}

/**
 * 구조검토 상세 조회
 * GET /api/v1/construction/structure-reviews/{id}
 */
export async function getStructureReview(id: string): Promise<{
  success: boolean;
  data?: StructureReview;
  error?: string;
}> {
  try {
    const response = await apiClient.get<{
      success: boolean;
      data: ApiStructureReview;
    }>(`/construction/structure-reviews/${id}`);

    return {
      success: true,
      data: transformStructureReview(response.data),
    };
  } catch (error) {
    console.error('구조검토 상세 조회 오류:', error);
    return { success: false, error: '구조검토 정보를 찾을 수 없습니다.' };
  }
}

/**
 * 구조검토 생성
 * POST /api/v1/construction/structure-reviews
 */
export async function createStructureReview(data: Partial<StructureReview>): Promise<{
  success: boolean;
  data?: StructureReview;
  error?: string;
}> {
  try {
    const apiData = transformToApiData(data);
    const response = await apiClient.post<{
      success: boolean;
      data: ApiStructureReview;
    }>('/construction/structure-reviews', apiData);

    return {
      success: true,
      data: transformStructureReview(response.data),
    };
  } catch (error) {
    console.error('구조검토 생성 오류:', error);
    return { success: false, error: '구조검토 등록에 실패했습니다.' };
  }
}

/**
 * 구조검토 수정
 * PUT /api/v1/construction/structure-reviews/{id}
 */
export async function updateStructureReview(
  id: string,
  data: Partial<StructureReview>
): Promise<{
  success: boolean;
  data?: StructureReview;
  error?: string;
}> {
  try {
    const apiData = transformToApiData(data);
    const response = await apiClient.put<{
      success: boolean;
      data: ApiStructureReview;
    }>(`/construction/structure-reviews/${id}`, apiData);

    return {
      success: true,
      data: transformStructureReview(response.data),
    };
  } catch (error) {
    console.error('구조검토 수정 오류:', error);
    return { success: false, error: '구조검토 수정에 실패했습니다.' };
  }
}

/**
 * 구조검토 삭제
 * DELETE /api/v1/construction/structure-reviews/{id}
 */
export async function deleteStructureReview(id: string): Promise<{
  success: boolean;
  error?: string;
}> {
  try {
    await apiClient.delete(`/construction/structure-reviews/${id}`);
    return { success: true };
  } catch (error) {
    console.error('구조검토 삭제 오류:', error);
    return { success: false, error: '구조검토 삭제에 실패했습니다.' };
  }
}

/**
 * 구조검토 일괄 삭제
 * DELETE /api/v1/construction/structure-reviews/bulk
 */
export async function deleteStructureReviews(ids: string[]): Promise<{
  success: boolean;
  deletedCount?: number;
  error?: string;
}> {
  try {
    await apiClient.delete('/construction/structure-reviews/bulk', {
      data: { ids: ids.map((id) => Number(id)) },
    });
    return { success: true, deletedCount: ids.length };
  } catch (error) {
    console.error('구조검토 일괄 삭제 오류:', error);
    return { success: false, error: '구조검토 일괄 삭제에 실패했습니다.' };
  }
}