export class ImportFile {
    constructor({
        id,
        originalName,
        fileSize,
        extension,
        status = 'pending',
        createdAt,
        metadata = null,
        mangaMatches = [],
        selectedMangaSlug = null,
        selectedVolume = null,
        selectedChapter = null,
        errorMessage = null,
        processedAt = null,
        // New properties for simplified workflow
        file = null, // Browser File object
        analysis = null, // Analysis result from API
        selectedManga = null, // Selected manga object
        selectedChapterId = null // Selected chapter ID
    }) {
        this.id = id;
        this.originalName = originalName;
        this.fileSize = fileSize;
        this.extension = extension;
        this.status = status;
        this.createdAt = createdAt;
        this.metadata = metadata;
        this.mangaMatches = mangaMatches;
        this.selectedMangaSlug = selectedMangaSlug;
        this.selectedVolume = selectedVolume;
        this.selectedChapter = selectedChapter;
        this.errorMessage = errorMessage;
        this.processedAt = processedAt;

        // New properties
        this.file = file;
        this.analysis = analysis;
        this.selectedManga = selectedManga;
        this.selectedChapterId = selectedChapterId;
        this.mangaMatches = mangaMatches; // Store found manga matches
    }

    static create(data) {
        return new ImportFile({
            ...data,
            createdAt: data.createdAt || new Date().toISOString()
        });
    }

    // Create from browser File object
    static createFromFile(file) {
        return new ImportFile({
            id: `file_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
            originalName: file.name,
            fileSize: file.size,
            extension: file.name.split('.').pop().toLowerCase(),
            file: file,
            createdAt: new Date().toISOString()
        });
    }

    isProcessed() {
        return this.status === 'processed';
    }

    hasError() {
        return this.status === 'error';
    }

    isPending() {
        return this.status === 'pending';
    }

    needsConversion() {
        return this.extension === 'cbr';
    }

    isReadyForImport() {
        return this.isProcessed() && this.selectedMangaSlug && (this.selectedVolume || this.selectedChapter);
    }

    getFormattedSize() {
        const bytes = parseInt(this.fileSize);
        const units = ['B', 'KB', 'MB', 'GB'];
        let size = bytes;
        let unitIndex = 0;

        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }

        return `${size.toFixed(2)} ${units[unitIndex]}`;
    }

    getContentType() {
        if (this.metadata?.chapter) {
            return `Chapter ${this.metadata.chapter}`;
        }
        if (this.metadata?.volume) {
            return `Volume ${this.metadata.volume}`;
        }
        return 'Unknown';
    }

    // === NEW METHODS FOR SIMPLIFIED WORKFLOW ===

    // Status helpers for new workflow
    isAnalyzed() {
        return this.status === 'analyzed';
    }

    isImporting() {
        return this.status === 'importing';
    }

    isImported() {
        return this.status === 'imported';
    }

    // Analysis helpers
    hasAnalysis() {
        return this.analysis && this.analysis.possibleTitles && this.analysis.possibleTitles.length > 0;
    }

    getPossibleTitles() {
        return this.analysis?.possibleTitles || [];
    }

    getAnalyzedChapter() {
        return this.analysis?.chapterNumber || null;
    }

    getAnalyzedVolume() {
        return this.analysis?.volumeNumber || null;
    }

    // For backward compatibility with existing code
    hasMatches() {
        return this.mangaMatches && this.mangaMatches.length > 0;
    }

    getMatches() {
        return this.mangaMatches || [];
    }

    getBestMatch() {
        const matches = this.getMatches();
        return matches.length > 0 ? matches[0] : null;
    }

    // Selection helpers
    isReadyForNewImport() {
        return this.selectedManga && (this.selectedChapterId || this.selectedVolume !== null);
    }

    getImportType() {
        if (this.selectedChapterId) return 'chapter';
        if (this.selectedVolume !== null) return 'volume';
        return null;
    }

    // File validation
    isValidFormat() {
        const validExtensions = ['cbz', 'cbr'];
        return validExtensions.includes(this.extension);
    }

    // Update methods for new workflow
    setAnalysis(analysis) {
        this.analysis = analysis;
        this.status = 'analyzed';
    }

    setMangaMatches(matches) {
        this.mangaMatches = matches;

        // Auto-select best match if available
        const bestMatch = this.getBestMatch();
        if (bestMatch) {
            this.selectedManga = bestMatch;
        }
    }

    setSelectedManga(manga) {
        this.selectedManga = manga;
        // Reset chapter/volume selection when manga changes
        this.selectedChapterId = null;
        this.selectedVolume = null;
    }

    setSelectedChapterById(chapterId) {
        this.selectedChapterId = chapterId;
        this.selectedVolume = null; // Can't have both
    }

    setSelectedVolumeNumber(volumeNumber) {
        this.selectedVolume = volumeNumber;
        this.selectedChapterId = null; // Can't have both
    }

    setImporting() {
        this.status = 'importing';
        this.errorMessage = null;
    }

    setImported() {
        this.status = 'imported';
        this.processedAt = new Date().toISOString();
        this.errorMessage = null;
    }

    setError(message) {
        this.status = 'error';
        this.errorMessage = message;
    }

    // Export selection for API
    getImportData() {
        if (!this.isReadyForNewImport()) {
            throw new Error('File is not ready for import');
        }

        const data = {
            mangaId: this.selectedManga.id
        };

        if (this.selectedChapterId) {
            data.chapterId = this.selectedChapterId;
        }

        if (this.selectedVolume !== null) {
            data.volumeNumber = this.selectedVolume;
        }

        return data;
    }
}
