import { Course, Lesson, Chapter, LessonAttachment, Quiz, LessonProgress, EnrollmentStatus } from '@prisma/client';
import { MultiLanguageText } from './index';

// Use MultiLanguageText from index.ts for consistency
export type MultiLangText = MultiLanguageText;

// ============================================
// Enrollment Types
// ============================================

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

export interface EnrollCourseResponse {
    code: number;
    message: string;
    data?: {
        enrollment_id: number;
        course_id: number;
        user_id: number;
        status: EnrollmentStatus;
        enrolled_at: Date;
    };
}

export interface ListEnrolledCoursesInput {
    token: string;
    page?: number;
    limit?: number;
    status?: EnrollmentStatus;
}

export interface EnrolledCourseItem {
    id: number;
    course_id: number;
    course: {
        id: number;
        title: MultiLangText;
        slug: string;
        thumbnail_url: string | null;
        description: MultiLangText;
    };
    status: EnrollmentStatus;
    progress_percentage: number;
    enrolled_at: Date;
    started_at: Date | null;
    completed_at: Date | null;
    last_accessed_at: Date | null;
}

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

// ============================================
// Course Learning Page Types
// ============================================

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

export interface LessonWithLockStatus {
    id: number;
    chapter_id: number;
    title: MultiLangText;
    type: 'VIDEO' | 'QUIZ';
    duration_minutes: number | null;
    sort_order: number;
    is_published: boolean;
    is_sequential: boolean;
    prerequisite_lesson_ids: number[] | null;
    require_pass_quiz: boolean;
    // Lock status
    is_locked: boolean;
    lock_reason?: string;
    // Progress
    is_completed: boolean;
    video_progress_percentage?: number;
}

export interface ChapterWithLessons {
    id: number;
    title: MultiLangText;
    description: MultiLangText | null;
    sort_order: number;
    is_published: boolean;
    lessons: LessonWithLockStatus[];
}

export interface GetCourseLearningResponse {
    code: number;
    message: string;
    data: {
        course: {
            id: number;
            title: MultiLangText;
            slug: string;
            description: MultiLangText;
            thumbnail_url: string | null;
            have_certificate: boolean;
        };
        enrollment: {
            status: EnrollmentStatus;
            progress_percentage: number;
            enrolled_at: Date;
            started_at: Date | null;
            completed_at: Date | null;
        };
        chapters: ChapterWithLessons[];
        total_lessons: number;
        completed_lessons: number;
    };
}

// ============================================
// Lesson Content Types
// ============================================

export interface GetLessonContentInput {
    token: string;
    course_id: number;
    lesson_id: number;
}

export interface LessonContentData {
    id: number;
    chapter_id: number;
    title: MultiLangText;
    content: MultiLangText | null;
    type: 'VIDEO' | 'QUIZ';
    duration_minutes: number | null;
    is_sequential: boolean;
    prerequisite_lesson_ids: number[] | null;
    require_pass_quiz: boolean;
    video_url: string | null; // Presigned URL for video
    attachments: {
        file_name: string;
        file_path: string;
        file_size: number;
        mime_type: string;
        presigned_url: string | null; // Presigned URL for attachment
    }[];
    quiz?: {
        id: number;
        title: MultiLangText;
        description: MultiLangText | null;
        passing_score: number;
        time_limit: number | null;
        shuffle_questions: boolean;
        shuffle_choices: boolean;
        is_skippable: boolean;
        show_answers_after_completion: boolean;
        questions: {
            id: number;
            question: MultiLangText;
            question_type: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
            score: number;
            sort_order: number;
            choices: {
                id: number;
                text: MultiLangText;
                sort_order: number;
            }[];
        }[];
    } | null;
    // Navigation
    prev_lesson_id: number | null;
    next_lesson_id: number | null;
}


export interface GetLessonContentResponse {
    code: number;
    message: string;
    data: LessonContentData;
    progress?: {
        is_completed: boolean;
        video_progress_seconds: number;
        video_duration_seconds: number | null;
        video_progress_percentage: number | null;
        last_watched_at: Date | null;
    };
}

// ============================================
// Lesson Access Check Types
// ============================================

export interface CheckLessonAccessInput {
    token: string;
    course_id: number;
    lesson_id: number;
}

export interface CheckLessonAccessResponse {
    code: number;
    message: string;
    data: {
        is_accessible: boolean;
        is_enrolled: boolean;
        is_locked: boolean;
        lock_reason?: string;
        required_lessons?: {
            id: number;
            title: MultiLangText;
            is_completed: boolean;
        }[];
        required_quiz_pass?: {
            lesson_id: number;
            quiz_id: number;
            title: MultiLangText;
            is_passed: boolean;
        };
    };
}

// ============================================
// Video Progress Types
// ============================================

export interface SaveVideoProgressInput {
    token: string;
    lesson_id: number;
    video_progress_seconds: number;
    video_duration_seconds?: number;
}

export interface SaveVideoProgressResponse {
    code: number;
    message: string;
    data?: {
        lesson_id: number;
        video_progress_seconds: number;
        video_duration_seconds: number | null;
        video_progress_percentage: number | null;
        is_completed: boolean;
        last_watched_at: Date;
        course_progress_percentage?: number;
        is_course_completed?: boolean;
    };
}

export interface GetVideoProgressInput {
    token: string;
    lesson_id: number;
}

export interface GetVideoProgressResponse {
    code: number;
    message: string;
    data: {
        lesson_id: number;
        video_progress_seconds: number;
        video_duration_seconds: number | null;
        video_progress_percentage: number | null;
        is_completed: boolean;
        completed_at: Date | null;
        last_watched_at: Date | null;
    } | null;
}

// ============================================
// Lesson Completion Types
// ============================================

export interface MarkLessonCompleteInput {
    token: string;
    course_id: number;
    lesson_id: number;
}

export interface MarkLessonCompleteResponse {
    code: number;
    message: string;
    data?: {
        lesson_id: number;
        is_completed: boolean;
        completed_at: Date;
        course_progress_percentage: number;
        is_course_completed: boolean;
        next_lesson_id: number | null;
        certificate_issued?: boolean;
    };
}

// ============================================
// Request Body Types (for TSOA)
// ============================================

export interface SaveVideoProgressBody {
    video_progress_seconds: number;
    video_duration_seconds?: number;
}

export interface EnrollCourseBody {
    course_id: number;
}

export interface CompleteLessonInput {
    token: string;
    lesson_id: number;
}

export interface CompleteLessonResponse {
    code: number;
    message: string;
    data?: {
        lesson_id: number;
        is_completed: boolean;
        completed_at: Date;
        course_progress_percentage: number;
        is_course_completed: boolean;
        next_lesson_id: number | null;
        certificate_issued?: boolean;
    };
}

// ============================================
// Quiz Submission Types
// ============================================

export interface QuizAnswerInput {
    question_id: number;
    choice_id: number; // For MULTIPLE_CHOICE and TRUE_FALSE
}

export interface SubmitQuizInput {
    token: string;
    course_id: number;
    lesson_id: number;
    answers: QuizAnswerInput[];
}

export interface SubmitQuizBody {
    answers: QuizAnswerInput[];
}

export interface SubmitQuizResponse {
    code: number;
    message: string;
    data?: {
        attempt_id: number;
        quiz_id: number;
        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;
        answers_review?: {
            question_id: number;
            selected_choice_id: number;
            correct_choice_id: number;
            is_correct: boolean;
            score: number;
        }[];
        course_progress_percentage?: number;
        is_course_completed?: boolean;
    };
}

// ============================================
// Get Quiz Attempts (Student)
// ============================================

export interface GetQuizAttemptsInput {
    token: string;
    course_id: number;
    lesson_id: number;
}

export interface QuizAttemptData {
    id: number;
    quiz_id: number;
    score: number;
    total_score: number;
    total_questions: number;
    correct_answers: number;
    is_passed: boolean;
    attempt_number: number;
    started_at: Date;
    completed_at: Date | null;
}

export interface GetQuizAttemptsResponse {
    code: number;
    message: string;
    data: {
        lesson_id: number;
        lesson_title: MultiLangText;
        quiz_id: number;
        quiz_title: MultiLangText;
        passing_score: number;
        attempts: QuizAttemptData[];
        best_score: number | null;
        latest_attempt: QuizAttemptData | null;
    };
}
