import { request } from '@/utils/request';

export interface TeacherUserInfo {
    id?: string;
    account?: string;
    nickName?: string;
    name?: string;
    avatar?: string;
    avatarUrl?: string;
    schoolId?: string;
    schoolName?: string;
    subjectId?: string | number;
    [key: string]: any;
}

export interface TeacherClassItem {
    id: string;
    name: string;
    classId?: string;
    className?: string;
    [key: string]: any;
}

export interface TeacherSubjectItem {
    subjectId?: string | number;
    id?: string | number;
    value?: string | number;
    subjectName?: string;
    name?: string;
    label?: string;
    [key: string]: any;
}

export interface TopErrorTitle {
    titleId?: number | string;
    titleContent?: string;
    subjectId?: number;
    subjectName?: string;
    errorCount?: number;
    errorRate?: number;
    [key: string]: any;
}

export interface AiCorrectionOverview {
    classId: number | string;
    className: string;
    aiPaperCount: number;
    aiTitleCount: number;
    teacherSaveTimeMinutes: number;
    excellentCount: number;
    excellentRate: number;
    goodCount: number;
    goodRate: number;
    passCount: number;
    passRate: number;
    unsubmittedCount: number;
    unsubmittedRate: number;
    topErrorTitles?: TopErrorTitle[];
}

export interface ClassHomeworkSubjectStat {
    subjectId: number;
    subjectName: string;
    homeworkCount: number;
    completedCount: number;
    uncompletedCount: number;
}

export interface ClassHomeworkStudentItem {
    userId: number | string;
    userName: string;
    totalCount: number;
    completedCount: number;
    uncompletedCount: number;
    subjectStats: ClassHomeworkSubjectStat[];
}

export interface ClassHomeworkReportData {
    classId: number | string;
    className: string;
    subjects: { subjectId: number; subjectName: string }[];
    studentList: ClassHomeworkStudentItem[];
}

export interface WeakKnowledgeItem {
    knowledgeId: number | string;
    knowledgeName: string;
    subjectId: number | string;
    subjectName: string;
    errorCount: number;
    titleCount: number;
    [key: string]: any;
}

export interface WeakKnowledgeTop10Data {
    classId: number | string;
    className: string;
    topWeakKnowledgeList: WeakKnowledgeItem[];
}

const formEncode = (value: string) => encodeURIComponent(value).replace(/%20/g, '+');

const appendDateRange = (query: string[], startTime?: string, endTime?: string) => {
    if (startTime?.trim()) {
        const value = startTime.trim();
        query.push('startTime=' + formEncode(value.includes(' ') ? value : `${value} 00:00:00`));
    }
    if (endTime?.trim()) {
        const value = endTime.trim();
        query.push('endTime=' + formEncode(value.includes(' ') ? value : `${value} 23:59:59`));
    }
};

const buildReportQuery = (params: {
    classId: string | number;
    subjectIds?: number[];
    startTime?: string;
    endTime?: string;
    schoolId?: string;
}) => {
    const query = ['classId=' + formEncode(String(params.classId))];
    if (params.schoolId) query.push('schoolId=' + formEncode(String(params.schoolId)));
    params.subjectIds?.forEach((id) => query.push('subjectIds=' + formEncode(String(id))));
    appendDateRange(query, params.startTime, params.endTime);
    return query.join('&');
};

export const getLoginUser = () =>
    request<TeacherUserInfo>({
        url: '/getLoginUser',
        method: 'GET',
    });

export const getClassListByTeacher = (params?: { gradeId?: string; gradeName?: string }) =>
    request<TeacherClassItem[]>({
        url: '/org/class/listByTeacher',
        method: 'GET',
        params: params || {},
    });

export const getSubjectListByClassId = (classId: string | number) =>
    request<TeacherSubjectItem[]>({
        url: '/org/class/listSubjectInfoByClassId',
        method: 'GET',
        params: { classId },
    });

export const getAiCorrectionOverview = (params: {
    classId: string | number;
    subjectIds?: number[];
    startTime?: string;
    endTime?: string;
    schoolId?: string;
}) =>
    request<AiCorrectionOverview>({
        url: '/school/report/aiCorrectionEfficiency/overview?' + buildReportQuery(params),
        method: 'GET',
    });

export const getClassHomeworkReport = (params: {
    classId: string | number;
    subjectIds?: number[];
    startTime?: string;
    endTime?: string;
}) =>
    request<ClassHomeworkReportData>({
        url: '/school/report/classHomework/list?' + buildReportQuery(params),
        method: 'GET',
    });

export const getWeakKnowledgeTop10 = (params: {
    classId: string | number;
    subjectIds?: number[];
    startTime?: string;
    endTime?: string;
    schoolId?: string;
}) =>
    request<WeakKnowledgeTop10Data>({
        url: '/school/report/weakKnowledge/top10?' + buildReportQuery(params),
        method: 'GET',
    });
