import {
    extractAnswerImages,
    extractTitleImages,
    formatQuestionHtml,
    getQuestionAnswerSource,
    getQuestionStemSource,
    isHtmlContent,
} from '@/utils/questionHtml';
import type { ReportQuestion, ReportQuestionSummary } from './data';

export type QuestionStatus = 'correct' | 'wrong' | 'unanswered';

export const normalizeAnswer = (value: unknown) => {
    if (Array.isArray(value)) return value.join('、');
    if (value == null) return '';
    return String(value)
        .replace(/^\[|\]$/g, '')
        .replace(/"/g, '')
        .replace(/,/g, '、');
};

export const formatRateText = (value: unknown) => {
    if (value == null || value === '') return '--';
    const num = Number(value);
    if (!Number.isNaN(num)) {
        const rate = num <= 1 ? num * 100 : num;
        return `${Math.round(rate * 10) / 10}%`;
    }
    const text = String(value);
    return text.includes('%') ? text : `${text}%`;
};

export const getQuestionStatus = (item: Record<string, unknown>): QuestionStatus => {
    const correct =
        item.correctResult === 1 ||
        item.correctResult === true ||
        item.markFlag === 1 ||
        item.markFlag === true;
    if (correct) return 'correct';
    if (!item.submitAnswer && !item.submitAnswerPic) return 'unanswered';
    return 'wrong';
};

const stripImageUrlsFromText = (text: string) => {
    let result = text;
    const links = text.match(/https?:\/\/[^\s,]+/gi) || [];
    links.forEach((link) => {
        result = result.replace(link, '');
    });
    return result.replace(/[,，、\s]+$/g, '').replace(/^[,，、\s]+/g, '').trim();
};

const buildStudentAnswer = (item: Record<string, unknown>) => {
    const studentAnswerImages = extractAnswerImages(item);
    let textAnswer = normalizeAnswer(item.submitAnswer);

    if (textAnswer) {
        const onlyUrls = stripImageUrlsFromText(textAnswer) === '' && studentAnswerImages.length > 0;
        if (onlyUrls) {
            textAnswer = '';
        } else {
            textAnswer = stripImageUrlsFromText(textAnswer);
        }
    }

    if (!textAnswer && !studentAnswerImages.length) {
        return { studentAnswer: '未作答', studentAnswerHtml: '', studentAnswerImages: [] };
    }

    if (textAnswer && isHtmlContent(textAnswer)) {
        return {
            studentAnswer: '',
            studentAnswerHtml: formatQuestionHtml(textAnswer),
            studentAnswerImages,
        };
    }

    return {
        studentAnswer: textAnswer,
        studentAnswerHtml: '',
        studentAnswerImages,
    };
};

const getQuestionFullScore = (item: Record<string, unknown>) => Number(item.score ?? item.titleScore ?? 0);

const getQuestionActualScore = (item: Record<string, unknown>) =>
    Number(item.actualScore ?? item.markScore ?? item.answerScore ?? item.gotScore ?? 0);

const formatQuestionScoreText = (item: Record<string, unknown>) => {
    const fullScore = getQuestionFullScore(item);
    const actualScore = getQuestionActualScore(item);
    return `${actualScore}/${fullScore || actualScore || 0}`;
};

export const mapPaperQuestionSummary = (item: Record<string, unknown>, index: number): ReportQuestionSummary => {
    return {
        no: Number(item.titleSort ?? item.sort ?? index + 1),
        type: String(item.titleChannelTypeName || item.chanelTypeName || '综合题'),
        score: formatQuestionScoreText(item),
        status: getQuestionStatus(item),
        classRate: formatRateText(
            item.classScoreRate ?? item.classRate ?? item.classAvgScoreRate ?? item.classGotScoreRate,
        ),
        schoolRate: formatRateText(
            item.schoolScoreRate ?? item.schoolRate ?? item.schoolAvgScoreRate ?? item.schoolGotScoreRate,
        ),
        knowledge: String(item.knowledgeName || item.knowledge || item.knowledgePointName || '待归类'),
    };
};

export const mapPaperQuestionSummaries = (list: unknown[]) =>
    (Array.isArray(list) ? list : []).map((item, index) =>
        mapPaperQuestionSummary(item as Record<string, unknown>, index),
    );

export const mapPaperQuestion = (item: Record<string, unknown>, index: number): ReportQuestion => {
    const status = getQuestionStatus(item);
    const stemSource = getQuestionStemSource(item);
    const answerSource = getQuestionAnswerSource(item);
    const { studentAnswer, studentAnswerHtml, studentAnswerImages } = buildStudentAnswer(item);
    const stemImages = extractTitleImages(stemSource);

    return {
        no: Number(item.titleSort ?? item.sort ?? index + 1),
        type: String(item.titleChannelTypeName || item.chanelTypeName || '综合题'),
        score: formatQuestionScoreText(item),
        status,
        stemHtml: formatQuestionHtml(stemSource, { stripImages: true }),
        stemImages,
        correctAnswerHtml: formatQuestionHtml(answerSource),
        studentAnswer,
        studentAnswerHtml,
        studentAnswerImages,
        classRate: formatRateText(
            item.classScoreRate ?? item.classRate ?? item.classAvgScoreRate ?? item.classGotScoreRate,
        ),
        schoolRate: formatRateText(
            item.schoolScoreRate ?? item.schoolRate ?? item.schoolAvgScoreRate ?? item.schoolGotScoreRate,
        ),
        knowledge: String(item.knowledgeName || item.knowledge || item.knowledgePointName || '待归类'),
    };
};

export const mapPaperQuestions = (list: unknown[]) =>
    (Array.isArray(list) ? list : []).map((item, index) =>
        mapPaperQuestion(item as Record<string, unknown>, index),
    );

/** 兼容旧版 mock 数据结构 */
export const normalizeLegacyQuestion = (item: Record<string, unknown>): ReportQuestion => {
    const legacy = item as ReportQuestion & { stem?: string; correctAnswer?: string };
    if (legacy.stemHtml) return legacy as ReportQuestion;
    return {
        no: Number(legacy.no),
        type: String(legacy.type),
        score: String(legacy.score),
        status: legacy.status,
        stemHtml: String(legacy.stem || ''),
        stemImages: [],
        correctAnswerHtml: String(legacy.correctAnswer || ''),
        studentAnswer: String(legacy.studentAnswer || ''),
        studentAnswerHtml: '',
        studentAnswerImages: [],
        classRate: String(legacy.classRate),
        schoolRate: String(legacy.schoolRate),
        knowledge: String(legacy.knowledge),
    };
};
