
// ============================================
// Multi-language Types
// ============================================

import { MultiLanguageText } from './index';

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

// ============================================
// Lesson Types (for nested inclusion)
// ============================================

export interface LessonAttachmentData {
    id: number;
    lesson_id: number;
    file_name: string;
    file_path: string;
    file_size: number;
    mime_type: string;
    description?: MultiLanguageText | null;
    sort_order: number;
    created_at: Date;
    presigned_url?: string | null;
}

export interface QuizData {
    id: number;
    lesson_id: number;
    title: MultiLanguageText;
    description: MultiLanguageText | null;
    passing_score: number;
    time_limit: number | null;
    shuffle_questions: boolean;
    shuffle_choices: boolean;
    show_answers_after_completion: boolean;
    is_skippable: boolean;
    created_at: Date;
    created_by: number;
    updated_at: Date | null;
    updated_by: number | null;
}

export interface LessonProgressData {
    id: number;
    user_id: number;
    lesson_id: number;
    is_completed: boolean;
    completed_at: Date | null;
    video_progress_seconds: number;
    video_duration_seconds: number | null;
    video_progress_percentage: number | null;
    last_watched_at: Date | null;
    created_at: Date;
    updated_at: Date | null;
}

export interface LessonData {
    id: number;
    chapter_id: number;
    title: MultiLanguageText;
    content: MultiLanguageText | null;
    type: 'VIDEO' | 'QUIZ';
    duration_minutes: number | null;
    sort_order: number;
    is_sequential: boolean;
    prerequisite_lesson_ids: number[] | null;
    require_pass_quiz: boolean;
    is_published: boolean;
    created_at: Date;
    updated_at: Date | null;
    video_url?: string | null;
    attachments?: LessonAttachmentData[];
    quiz?: QuizData | null;
    progress?: LessonProgressData[];
}

// ============================================
// Chapter Types
// ============================================

export interface ChapterData {
    id: number;
    course_id: number;
    title: MultiLanguageText;
    description: MultiLanguageText | null;
    sort_order: number;
    is_published: boolean;
    created_at: Date;
    updated_at: Date | null;
    lessons?: LessonData[];
}

// ============================================
// Request Types
// ============================================

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

export interface GetChapterRequest {
    token: string;
    course_id: number;
    chapter_id: number;
}

export interface CreateChapterInput {
    token: string;
    course_id: number;
    title: MultiLanguageText;
    description?: MultiLanguageText;
    sort_order?: number;
    is_published?: boolean;
}

export interface CreateChapterRequest {
    token: string;
    course_id: number;
    data: CreateChapterInput;
}

export interface UpdateChapterInput {
    token: string;
    course_id: number;
    chapter_id: number;
    title?: MultiLanguageText;
    description?: MultiLanguageText;
    sort_order?: number;
    is_published?: boolean;
}

export interface UpdateChapterRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    data: UpdateChapterInput;
}

export interface DeleteChapterRequest {
    token: string;
    course_id: number;
    chapter_id: number;
}

export interface ReorderChapterRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    sort_order: number;
}

// ============================================
// Response Types
// ============================================

export interface ListChaptersResponse {
    code: number;
    message: string;
    data: ChapterData[];
    total: number;
}

export interface GetChapterResponse {
    code: number;
    message: string;
    data: ChapterData;
}

export interface CreateChapterResponse {
    code: number;
    message: string;
    data: ChapterData;
}

export interface UpdateChapterResponse {
    code: number;
    message: string;
    data: ChapterData;
}

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

export interface ReorderChapterResponse {
    code: number;
    message: string;
    data: ChapterData[];
}

// ============================================
// ============================================
// Lesson Request Types
// ============================================

export interface GetLessonRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    lesson_id: number;
}

/**
 * Uploaded file information from multer
 */
export interface UploadedFileInfo {
    originalname: string;
    mimetype: string;
    size: number;
    buffer: Buffer;
}

export interface CreateLessonInput {
    token: string;
    course_id: number;
    chapter_id: number;
    title: MultiLanguageText;
    content?: MultiLanguageText;
    type: 'VIDEO' | 'QUIZ';
    duration_minutes?: number;
    sort_order?: number;
    is_sequential?: boolean;
    prerequisite_lesson_ids?: number[];
    require_pass_quiz?: boolean;
    is_published?: boolean;
    // File upload fields
    video?: UploadedFileInfo;
    attachments?: UploadedFileInfo[];
    // Quiz data for QUIZ type lessons (one quiz per lesson)
    quiz_data?: {
        title: MultiLanguageText;
        description?: MultiLanguageText;
        passing_score?: number;
        time_limit?: number;
        shuffle_questions?: boolean;
        shuffle_choices?: boolean;
        show_answers_after_completion?: boolean;
        questions: CreateQuizQuestionInput[];
    };
}

/**
 * Input for creating quiz questions (matches Prisma QuestionType enum)
 */
export interface CreateQuizQuestionInput {
    question: MultiLanguageText;
    explanation?: MultiLanguageText;
    question_type: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
    score?: number;
    sort_order?: number;
    choices?: CreateQuizChoiceInput[];
}

/**
 * Input for creating quiz choices (matches Prisma Choice model)
 */
export interface CreateQuizChoiceInput {
    text: MultiLanguageText; // "text" matches Prisma field name
    is_correct: boolean;
    sort_order?: number;
}

/**
 * Response data for quiz questions (with IDs after creation)
 */
export interface QuizQuestionData {
    id: number;
    quiz_id: number;
    question: MultiLanguageText;
    explanation: MultiLanguageText | null;
    question_type: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
    score: number;
    sort_order: number;
    created_at: Date;
    updated_at: Date | null;
    choices?: QuizChoiceData[];
}

/**
 * Response data for quiz choices (matches Prisma Choice model - no timestamps)
 */
export interface QuizChoiceData {
    id: number;
    question_id: number;
    text: MultiLanguageText; // "text" matches Prisma field name
    is_correct: boolean;
    sort_order: number;
}

export interface CreateLessonRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    data: CreateLessonInput;
}

export interface UpdateLessonInput {
    title?: MultiLanguageText;
    content?: MultiLanguageText;
    duration_minutes?: number;
    sort_order?: number;
    is_sequential?: boolean;
    prerequisite_lesson_ids?: number[];
    require_pass_quiz?: boolean;
    is_published?: boolean;
}

export interface UpdateLessonRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    lesson_id: number;
    data: UpdateLessonInput;
}

export interface DeleteLessonRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    lesson_id: number;
}

export interface ReorderLessonsRequest {
    token: string;
    course_id: number;
    chapter_id: number;
    lesson_id: number;
    sort_order: number;
}

// ============================================
// Lesson Response Types
// ============================================

export interface GetLessonResponse {
    code: number;
    message: string;
    data: LessonData;
}

export interface CreateLessonResponse {
    code: number;
    message: string;
    data: LessonData;
}

export interface UpdateLessonResponse {
    code: number;
    message: string;
    data: LessonData;
}



// ============================================
// Separate Video/Attachment API Types
// ============================================

/**
 * Input for uploading video to a lesson
 */
export interface UploadVideoInput {
    token: string;
    course_id: number;
    lesson_id: number;
    video: UploadedFileInfo;
}

/**
 * Input for updating (replacing) video in a lesson
 */
export interface UpdateVideoInput {
    token: string;
    course_id: number;
    lesson_id: number;
    video: UploadedFileInfo;
}

/**
 * Input for setting YouTube video to a lesson
 */
export interface SetYouTubeVideoInput {
    token: string;
    course_id: number;
    lesson_id: number;
    youtube_video_id: string;
    video_title: string;
}

/**
 * Response for YouTube video operation
 */
export interface YouTubeVideoResponse {
    code: number;
    message: string;
    data: {
        lesson_id: number;
        video_url: string;
        video_id: string;
        video_title: string;
        mime_type: string;
    };
}

/**
 * Input for uploading a single attachment to a lesson
 */
export interface UploadAttachmentInput {
    token: string;
    course_id: number;
    lesson_id: number;
    attachment: UploadedFileInfo;
}

/**
 * Input for deleting an attachment from a lesson
 */
export interface DeleteAttachmentInput {
    token: string;
    course_id: number;
    lesson_id: number;
    attachment_id: number;
}

/**
 * Response for video operations
 */
export interface VideoOperationResponse {
    code: number;
    message: string;
    data: {
        lesson_id: number;
        video_url: string | null;
        file_name: string;
        file_size: number;
        mime_type: string;
    };
}

/**
 * Response for attachment operations
 */
export interface AttachmentOperationResponse {
    code: number;
    message: string;
    data: LessonAttachmentData;
}

/**
 * Response for delete attachment operation
 */
export interface DeleteAttachmentResponse {
    code: number;
    message: string;
}


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

export interface ReorderLessonsResponse {
    code: number;
    message: string;
    data: LessonData[];
}

// ============================================
// Lesson with Full Details (attachments, quiz, progress)
// ============================================

export interface LessonWithDetailsResponse {
    code: number;
    message: string;
    data: LessonData & {
        attachments: LessonAttachmentData[];
        quiz: QuizData | null;
        progress: LessonProgressData[];
    };
}


/**
 * Input for adding quiz to an existing QUIZ lesson
 */
export interface AddQuizToLessonInput {
    token: string;
    course_id: number;
    lesson_id: number;
    quiz_data: {
        title: MultiLanguageText;
        description?: MultiLanguageText;
        passing_score?: number;
        time_limit?: number;
        shuffle_questions?: boolean;
        shuffle_choices?: boolean;
        show_answers_after_completion?: boolean;
        questions: CreateQuizQuestionInput[];
    };
}

/**
 * Input for adding a single question to a quiz lesson
 */
export interface AddQuestionInput {
    token: string;
    course_id: number;
    lesson_id: number;
    question: MultiLanguageText;
    explanation?: MultiLanguageText;
    question_type: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
    sort_order?: number;
    choices?: CreateQuizChoiceInput[];
}

/**
 * Response for adding a question
 */
export interface AddQuestionResponse {
    code: number;
    message: string;
    data: QuizQuestionData;
}

/**
 * Input for updating a question
 */
export interface UpdateQuestionInput {
    token: string;
    course_id: number;
    lesson_id: number;
    question_id: number;
    question?: MultiLanguageText;
    explanation?: MultiLanguageText;
    question_type?: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
    sort_order?: number;
    choices?: CreateQuizChoiceInput[]; // Replace all choices if provided
}

/**
 * Response for updating a question
 */
export interface UpdateQuestionResponse {
    code: number;
    message: string;
    data: QuizQuestionData;
}

/**
 * Input for deleting a question
 */
export interface DeleteQuestionInput {
    token: string;
    course_id: number;
    lesson_id: number;
    question_id: number;
}

export interface ReorderQuestionInput {
    token: string;
    course_id: number;
    lesson_id: number;
    question_id: number;
    sort_order: number;
}

export interface ReorderQuestionResponse {
    code: number;
    message: string;
    data: QuizQuestionData[];
}

/**
 * Response for deleting a question
 */
export interface DeleteQuestionResponse {
    code: number;
    message: string;
}

/**
 * Input for updating quiz settings
 */
export interface UpdateQuizInput {
    token: string;
    course_id: number;
    lesson_id: number;
    title?: MultiLanguageText;
    description?: MultiLanguageText;
    passing_score?: number;
    time_limit?: number;
    shuffle_questions?: boolean;
    shuffle_choices?: boolean;
    show_answers_after_completion?: boolean;
    is_skippable?: boolean;
}

/**
 * Response for updating quiz
 */
export interface UpdateQuizResponse {
    code: number;
    message: string;
    data: QuizData;
}

// ============================================
// Controller Body Request Types
// ============================================

export interface CreateChapterBody {
    title: MultiLanguageText;
    description?: MultiLanguageText;
    sort_order?: number;
}

export interface UpdateChapterBody {
    title?: MultiLanguageText;
    description?: MultiLanguageText;
    sort_order?: number;
    is_published?: boolean;
}

export interface ReorderChapterBody {
    sort_order: number;
}

export interface CreateLessonBody {
    title: MultiLanguageText;
    content?: MultiLanguageText;
    type: 'VIDEO' | 'QUIZ';
    sort_order?: number;
}

export interface UpdateLessonBody {
    title?: MultiLanguageText;
    content?: MultiLanguageText;
    duration_minutes?: number;
    sort_order?: number;
    prerequisite_lesson_ids?: number[];
    is_published?: boolean;
}

export interface ReorderLessonsBody {
    lesson_id: number;
    sort_order: number;
}

export interface SetYouTubeVideoBody {
    youtube_video_id: string;
    video_title: string;
}

export interface ReorderQuestionBody {
    sort_order: number;
}

export interface AddQuestionBody {
    question: MultiLanguageText;
    explanation?: MultiLanguageText;
    question_type: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
    sort_order?: number;
    choices?: CreateQuizChoiceInput[];
}

export interface UpdateQuestionBody {
    question?: MultiLanguageText;
    explanation?: MultiLanguageText;
    question_type?: 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'SHORT_ANSWER';
    sort_order?: number;
    choices?: CreateQuizChoiceInput[];
}

export interface UpdateQuizBody {
    title?: MultiLanguageText;
    description?: MultiLanguageText;
    passing_score?: number;
    time_limit?: number;
    shuffle_questions?: boolean;
    shuffle_choices?: boolean;
    show_answers_after_completion?: boolean;
    is_skippable?: boolean;
}