/**
 * 1:1 문의 관리 타입 정의
 */

// API 타입 re-export
export type { PostApiData, PostPaginationResponse } from '../shared/types';

/**
 * API 데이터 → 프론트엔드 타입 변환
 */
export function transformPostToInquiry(post: import('../shared/types').PostApiData): Inquiry {
  // 카테고리와 상태는 custom_field_values에서 가져옴
  const categoryField = post.custom_field_values?.find(f => f.field_key === 'category');
  // 답변 여부에 따라 상태 결정 (status 필드가 있으면 사용, 없으면 기본값)
  const statusField = post.custom_field_values?.find(f => f.field_key === 'status');

  return {
    id: String(post.id),
    category: (categoryField?.field_value as InquiryCategory) || 'inquiry',
    title: post.title,
    content: post.content,
    authorId: String(post.user_id),
    authorName: post.author?.name || '회원',
    status: (statusField?.field_value as InquiryStatus) || 'waiting',
    attachments: post.files?.map(file => ({
      id: String(file.id),
      fileName: file.original_name,
      fileSize: file.file_size,
      fileUrl: file.file_path,
    })) || [],
    createdAt: post.created_at,
    updatedAt: post.updated_at,
  };
}

// ===== 상담분류 타입 =====
export type InquiryCategory = 'inquiry' | 'report' | 'suggestion' | 'error';

export const INQUIRY_CATEGORIES: { value: InquiryCategory; label: string }[] = [
  { value: 'inquiry', label: '문의하기' },
  { value: 'report', label: '신고하기' },
  { value: 'suggestion', label: '건의사항' },
  { value: 'error', label: '서비스오류' },
];

export const INQUIRY_CATEGORY_LABELS: Record<InquiryCategory, string> = {
  inquiry: '문의하기',
  report: '신고하기',
  suggestion: '건의사항',
  error: '서비스오류',
};

// ===== 상태 타입 =====
export type InquiryStatus = 'waiting' | 'completed';

export const INQUIRY_STATUSES: { value: InquiryStatus | 'all'; label: string }[] = [
  { value: 'all', label: '전체' },
  { value: 'waiting', label: '답변대기' },
  { value: 'completed', label: '답변완료' },
];

export const INQUIRY_STATUS_LABELS: Record<InquiryStatus, string> = {
  waiting: '답변대기',
  completed: '답변완료',
};

// ===== 정렬 옵션 =====
export type SortOption = 'latest' | 'oldest';

export const SORT_OPTIONS: { value: SortOption; label: string }[] = [
  { value: 'latest', label: '최신순' },
  { value: 'oldest', label: '오래된순' },
];

// ===== 첨부파일 타입 =====
export interface Attachment {
  id: string;
  fileName: string;
  fileSize: number;
  fileUrl: string;
}

// ===== 문의 타입 =====
export interface Inquiry {
  id: string;
  category: InquiryCategory;
  title: string;
  content: string;
  authorId: string;
  authorName: string;
  status: InquiryStatus;
  attachments: Attachment[];
  createdAt: string;
  updatedAt: string;
}

// ===== 답변 타입 =====
export interface Reply {
  id: string;
  inquiryId: string;
  authorId: string;
  authorName: string;
  content: string;
  attachments: Attachment[];
  createdAt: string;
}

// ===== 댓글 타입 =====
export interface Comment {
  id: string;
  inquiryId: string;
  authorId: string;
  authorName: string;
  authorProfileImage?: string;
  content: string;
  createdAt: string;
  updatedAt: string;
}

// ===== Mock 데이터 =====
export const MOCK_INQUIRIES: Inquiry[] = [
  {
    id: '1',
    category: 'inquiry',
    title: '서비스 이용 관련 문의드립니다',
    content: `<p>안녕하세요.</p>
<p>서비스 이용 중 궁금한 점이 있어 문의드립니다.</p>
<p>결제 내역은 어디서 확인할 수 있나요?</p>`,
    authorId: 'user1',
    authorName: '홍길동',
    status: 'waiting',
    attachments: [],
    createdAt: '2025-12-19T12:23:00Z',
    updatedAt: '2025-12-19T12:23:00Z',
  },
  {
    id: '2',
    category: 'inquiry',
    title: '회원정보 변경 요청',
    content: `<p>회원정보 변경을 요청합니다.</p>`,
    authorId: 'user1',
    authorName: '홍길동',
    status: 'completed',
    attachments: [
      { id: 'att1', fileName: 'abc.pdf', fileSize: 1024000, fileUrl: '/files/abc.pdf' },
    ],
    createdAt: '2025-12-18T12:23:00Z',
    updatedAt: '2025-12-18T15:30:00Z',
  },
  {
    id: '3',
    category: 'report',
    title: '불법 광고 신고',
    content: `<p>불법 광고를 신고합니다.</p>`,
    authorId: 'user2',
    authorName: '김철수',
    status: 'waiting',
    attachments: [],
    createdAt: '2025-12-17T12:23:00Z',
    updatedAt: '2025-12-17T12:23:00Z',
  },
  {
    id: '4',
    category: 'suggestion',
    title: '기능 개선 건의',
    content: `<p>다음과 같은 기능 개선을 건의합니다.</p>`,
    authorId: 'user3',
    authorName: '이영희',
    status: 'completed',
    attachments: [],
    createdAt: '2025-12-16T12:23:00Z',
    updatedAt: '2025-12-16T10:00:00Z',
  },
  {
    id: '5',
    category: 'error',
    title: '로그인 오류 발생',
    content: `<p>로그인 시 오류가 발생합니다.</p>`,
    authorId: 'user4',
    authorName: '박민수',
    status: 'waiting',
    attachments: [],
    createdAt: '2025-12-15T12:23:00Z',
    updatedAt: '2025-12-15T12:23:00Z',
  },
  {
    id: '6',
    category: 'inquiry',
    title: '결제 관련 문의',
    content: `<p>결제 관련 문의드립니다.</p>`,
    authorId: 'user1',
    authorName: '홍길동',
    status: 'waiting',
    attachments: [],
    createdAt: '2025-12-14T12:23:00Z',
    updatedAt: '2025-12-14T12:23:00Z',
  },
  {
    id: '7',
    category: 'inquiry',
    title: '환불 요청',
    content: `<p>환불을 요청합니다.</p>`,
    authorId: 'user5',
    authorName: '최지은',
    status: 'completed',
    attachments: [],
    createdAt: '2025-12-13T12:23:00Z',
    updatedAt: '2025-12-13T09:00:00Z',
  },
];

export const MOCK_REPLY: Reply = {
  id: 'reply1',
  inquiryId: '2',
  authorId: 'admin1',
  authorName: '관리자',
  content: `<p>안녕하세요, 고객님.</p>
<p>문의 주신 회원정보 변경 요청이 완료되었습니다.</p>
<p>감사합니다.</p>`,
  attachments: [
    { id: 'att2', fileName: 'abc.pdf', fileSize: 512000, fileUrl: '/files/reply.pdf' },
  ],
  createdAt: '2025-12-18T15:30:00Z',
};

export const MOCK_COMMENTS: Comment[] = [
  {
    id: 'comment1',
    inquiryId: '2',
    authorId: 'user1',
    authorName: '이름',
    content: '댓글 내용',
    createdAt: '2025-12-18T15:33:00Z',
    updatedAt: '2025-12-18T15:33:00Z',
  },
];

// ===== 필터 옵션 =====
export const CATEGORY_FILTER_OPTIONS: { value: InquiryCategory | 'all'; label: string }[] = [
  { value: 'all', label: '전체' },
  ...INQUIRY_CATEGORIES,
];