/**
 * 고객센터 공통 타입 정의
 * 시스템 게시판 게시글 API 응답 타입
 */

// API 게시글 응답 타입
export interface PostApiData {
  id: number;
  tenant_id: number | null;
  board_id: number;
  user_id: number;
  title: string;
  content: string;
  editor_type: string;
  ip_address: string | null;
  is_notice: boolean;
  is_secret: boolean;
  views: number;
  status: string;
  created_at: string;
  updated_at: string;
  created_by: number | null;
  updated_by: number | null;
  deleted_at: string | null;
  deleted_by: number | null;
  // 관계
  author?: {
    id: number;
    name: string;
  };
  files?: FileApiData[];
  custom_field_values?: CustomFieldValue[];
}

// API 파일 타입
export interface FileApiData {
  id: number;
  original_name: string;
  file_path: string;
  file_size: number;
  mime_type: string;
}

// 커스텀 필드 값
export interface CustomFieldValue {
  field_key: string;
  field_value: string | null;
  field_name?: string;
}

// API 페이지네이션 응답
export interface PostPaginationResponse {
  current_page: number;
  data: PostApiData[];
  first_page_url: string;
  from: number | null;
  last_page: number;
  last_page_url: string;
  next_page_url: string | null;
  path: string;
  per_page: number;
  prev_page_url: string | null;
  to: number | null;
  total: number;
}

// API 응답 래퍼
export interface ApiResponse<T> {
  success: boolean;
  data: T;
  message: string;
}

// 시스템 게시판 코드 (시스템 게시판 + 동적 테넌트 게시판)
export type SystemBoardCode = 'notice' | 'faq' | 'qna' | 'event' | 'popup' | string;

// 게시글 필터
export interface PostFilters {
  search?: string;
  is_notice?: boolean;
  status?: string;
  per_page?: number;
  page?: number;
}

// ===== 댓글 API 타입 =====

// API 댓글 응답 타입
export interface CommentApiData {
  id: number;
  post_id: number;
  tenant_id: number;
  user_id: number;
  parent_id: number | null;
  content: string;
  ip_address: string | null;
  status: string;
  created_at: string;
  updated_at: string;
  // 관계
  user?: {
    id: number;
    name: string;
    profile_image?: string;
  };
  replies?: CommentApiData[];
}

// 댓글 목록 응답 (배열)
export type CommentsApiResponse = CommentApiData[];

// 댓글 → 프론트엔드 타입 변환
export function transformApiToComment(apiComment: CommentApiData): import('../InquiryManagement/types').Comment {
  return {
    id: String(apiComment.id),
    inquiryId: String(apiComment.post_id),
    authorId: String(apiComment.user_id),
    authorName: apiComment.user?.name || '회원',
    authorProfileImage: apiComment.user?.profile_image,
    content: apiComment.content,
    createdAt: apiComment.created_at,
    updatedAt: apiComment.updated_at,
  };
}
