import { Course, Prisma, User } from '@prisma/client';
import { MultiLanguageText } from './index';

// Custom type for TSOA - avoiding complex Prisma types
export interface CreateCourseInput {
    category_id?: number;
    title: MultiLanguageText;
    slug: string;
    description: MultiLanguageText;
    thumbnail_url?: string;
    price?: number;
    is_free?: boolean;
    have_certificate?: boolean;
}

export interface createCourses {
    data: CreateCourseInput;
}

export interface createCourseResponse {
    code: number;
    message: string;
    data: Course;
}

export interface ListMyCourseResponse {
    code: number;
    message: string;
    data: Course[];
    total: number;
}

export interface GetMyCourseResponse {
    code: number;
    message: string;
    data: Course;
}

export interface getmyCourse {
    token: string;
    course_id: number;
}

export interface UpdateCourseInput {
    category_id?: number;
    title?: MultiLanguageText;
    slug?: string;
    description?: MultiLanguageText;
    thumbnail_url?: string;
    price?: number;
    is_free?: boolean;
    have_certificate?: boolean;
}

export interface UpdateMyCourse {
    data: UpdateCourseInput;
}

export interface UpdateMyCourseResponse {
    code: number;
    message: string;
    data: Course;
}

export interface DeleteMyCourseResponse {
    code: number;
    message: string;
}

export interface submitCourseResponse {
    code: number;
    message: string;
}

export interface submitCourse {
    courseId: number;
    submitted_by: number;
}

export interface submitCourseResponse {
    code: number;
    message: string;
}

export interface listCourseinstructorResponse {
    code: number;
    message: string;
    data: Course[];
}

export interface addinstructorCourse {
    token: string;
    email_or_username: string;
    course_id: number;
}

export interface SearchInstructorInput {
    token: string;
    query: string;
    course_id: number;
}

export interface SearchInstructorResult {
    id: number;
    username: string;
    email: string;
    first_name: string | null;
    last_name: string | null;
    avatar_url: string | null;
}

export interface SearchInstructorResponse {
    code: number;
    message: string;
    data: SearchInstructorResult[];
}

export interface addinstructorCourseResponse {
    code: number;
    message: string;
}

export interface InstructorInfo {
    id: number;
    username: string;
    email: string;
    first_name: string | null;
    last_name: string | null;
    avatar_url: string | null;
}

export interface listinstructorCourseResponse {
    code: number;
    message: string;
    data: {
        user_id: number;
        is_primary: boolean;
        user: InstructorInfo;
    }[];
}

export interface listinstructorCourse {
    token: string;
    course_id: number;
}

export interface removeinstructorCourse {
    token: string;
    user_id: number;
    course_id: number;
}

export interface removeinstructorCourseResponse {
    code: number;
    message: string;
}

export interface setprimaryCourseInstructor {
    token: string;
    user_id: number;
    course_id: number;
}

export interface setprimaryCourseInstructorResponse {
    code: number;
    message: string;
}

export interface sendCourseForReview {
    token: string;
    course_id: number;
}

export interface CourseApprovalData {
    id: number;
    course_id: number;
    submitted_by: number;
    reviewed_by: number | null;
    action: 'SUBMITTED' | 'APPROVED' | 'REJECTED' | 'REVISION_REQUESTED';
    previous_status: string | null;
    new_status: string | null;
    comment: string | null;
    created_at: Date;
    submitter: {
        id: number;
        username: string;
        email: string;
    };
    reviewer: {
        id: number;
        username: string;
        email: string;
    } | null;
}

export interface GetCourseApprovalsResponse {
    code: number;
    message: string;
    data: CourseApprovalData[];
    total: number;
}

// ============================================
// Enrolled Students (Instructor)
// ============================================

export interface GetEnrolledStudentsInput {
    token: string;
    course_id: number;
    page?: number;
    limit?: number;
    search?: string;
    status?: 'ENROLLED' | 'COMPLETED';
}

export interface EnrolledStudentData {
    user_id: number;
    username: string;
    email: string;
    first_name: string | null;
    last_name: string | null;
    avatar_url: string | null;
    enrolled_at: Date;
    progress_percentage: number;
    status: string;
}

export interface GetEnrolledStudentsResponse {
    code: number;
    message: string;
    data: EnrolledStudentData[];
    total: number;
    page: number;
    limit: number;
}

// ============================================
// Quiz Scores by Lesson (Instructor)
// ============================================

export interface GetQuizScoresInput {
    token: string;
    course_id: number;
    lesson_id: number;
    page?: number;
    limit?: number;
    search?: string;
    is_passed?: boolean;
}

export interface StudentQuizScoreData {
    user_id: number;
    username: string;
    email: string;
    first_name: string | null;
    last_name: string | null;
    avatar_url: string | null;
    latest_attempt: {
        id: number;
        score: number;
        total_score: number;
        is_passed: boolean;
        attempt_number: number;
        completed_at: Date | null;
    } | null;
    best_score: number | null;
    total_attempts: number;
    is_passed: boolean;
}

export interface GetQuizScoresResponse {
    code: number;
    message: string;
    data: {
        lesson_id: number;
        lesson_title: MultiLanguageText;
        quiz_id: number;
        quiz_title: MultiLanguageText;
        passing_score: number;
        total_score: number;
        students: StudentQuizScoreData[];
    };
    total: number;
    page: number;
    limit: number;
}

// ============================================
// Quiz Attempt Detail (Instructor)
// ============================================

export interface GetQuizAttemptDetailInput {
    token: string;
    course_id: number;
    lesson_id: number;
    student_id: number;
}

export interface QuizAttemptDetailData {
    attempt_id: number;
    quiz_id: number;
    student: {
        user_id: number;
        username: string;
        email: string;
        first_name: string | null;
        last_name: string | null;
    };
    score: number;
    total_score: number;
    total_questions: number;
    correct_answers: number;
    is_passed: boolean;
    passing_score: number;
    attempt_number: number;
    started_at: Date;
    completed_at: Date | null;
    answers_review: {
        question_id: number;
        sort_order: number;
        question_text: MultiLanguageText;
        selected_choice_id: number | null;
        selected_choice_text: MultiLanguageText | null;
        is_correct: boolean;
        score: number;
        question_score: number;
    }[];
}

export interface GetQuizAttemptDetailResponse {
    code: number;
    message: string;
    data: QuizAttemptDetailData;
}

// ============================================
// Enrolled Student Detail (Instructor)
// ============================================

export interface GetEnrolledStudentDetailInput {
    token: string;
    course_id: number;
    student_id: number;
}

export interface LessonProgressDetail {
    lesson_id: number;
    lesson_title: MultiLanguageText;
    lesson_type: string;
    sort_order: number;
    is_completed: boolean;
    completed_at: Date | null;
    video_progress_seconds: number | null;
    video_duration_seconds: number | null;
    video_progress_percentage: number | null;
    last_watched_at: Date | null;
}

export interface ChapterProgressDetail {
    chapter_id: number;
    chapter_title: MultiLanguageText;
    sort_order: number;
    lessons: LessonProgressDetail[];
    completed_lessons: number;
    total_lessons: number;
}

export interface EnrolledStudentDetailData {
    student: {
        user_id: number;
        username: string;
        email: string;
        first_name: string | null;
        last_name: string | null;
        avatar_url: string | null;
    };
    enrollment: {
        enrolled_at: Date;
        progress_percentage: number;
        status: string;
    };
    chapters: ChapterProgressDetail[];
    total_completed_lessons: number;
    total_lessons: number;
}

export interface GetEnrolledStudentDetailResponse {
    code: number;
    message: string;
    data: EnrolledStudentDetailData;
}

