import multer from 'multer';
import path from 'path';
import fs from 'fs';
import TeamDocumentService from '../services/teamDocumentService.js';
import PDFParserService from '../services/pdfParserService.js';
import { getUserByToken } from '../utils/userUtils.js';
import { devLog } from '../utils/logger.js';

// Multer-Konfiguration für Datei-Uploads
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        try {
            fs.mkdirSync('uploads/temp', { recursive: true });
        } catch (mkdirError) {
            console.error('[multer] - Failed to ensure temp upload directory exists:', mkdirError);
        }
        cb(null, 'uploads/temp/');
    },
    filename: (req, file, cb) => {
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
        cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
    }
});

const upload = multer({
    storage: storage,
    limits: {
        fileSize: 10 * 1024 * 1024 // 10MB Limit
    },
    fileFilter: (req, file, cb) => {
        // Erlaube nur PDF, DOC, DOCX, TXT, CSV Dateien
        const allowedExtensions = ['.pdf', '.doc', '.docx', '.txt', '.csv'];
        const allowedMimePatterns = ['pdf', 'msword', 'wordprocessingml.document', 'text/plain', 'csv', 'excel'];

        const extensionValid = allowedExtensions.includes(path.extname(file.originalname).toLowerCase());
        const mimetypeValid = allowedMimePatterns.some((pattern) => file.mimetype && file.mimetype.toLowerCase().includes(pattern));

        if (extensionValid && mimetypeValid) {
            return cb(null, true);
        }

        cb(new Error('Nur PDF, DOC, DOCX, TXT und CSV Dateien sind erlaubt!'));
    }
});

export const uploadMiddleware = upload.single('document');

export const uploadDocument = async (req, res) => {
    try {
        const { authcode: token } = req.headers;
        const { clubteamid: clubTeamId } = req.params;
        const { documentType } = req.body;

        const user = await getUserByToken(token);

        if (!req.file) {
            return res.status(400).json({ error: "nofile" });
        }

        if (!documentType || !['code_list', 'pin_list'].includes(documentType)) {
            return res.status(400).json({ error: "invaliddocumenttype" });
        }

        const document = await TeamDocumentService.uploadDocument(req.file, clubTeamId, documentType);
        
        res.status(201).json(document);
    } catch (error) {
        console.error('[uploadDocument] - Error:', error);
        
        // Lösche temporäre Datei bei Fehler
        if (req.file && req.file.path) {
            try {
                const fs = await import('fs');
                fs.unlinkSync(req.file.path);
            } catch (cleanupError) {
                console.error('Fehler beim Löschen der temporären Datei:', cleanupError);
            }
        }

        if (error.message === 'Club-Team nicht gefunden') {
            return res.status(404).json({ error: "clubteamnotfound" });
        }
        
        res.status(500).json({ error: "internalerror" });
    }
};

export const getDocuments = async (req, res) => {
    try {
        const { authcode: token } = req.headers;
        const { clubteamid: clubTeamId } = req.params;
        
        const user = await getUserByToken(token);
        
        const documents = await TeamDocumentService.getDocumentsByClubTeam(clubTeamId);
        
        res.status(200).json(documents);
    } catch (error) {
        console.error('[getDocuments] - Error:', error);
        res.status(500).json({ error: "internalerror" });
    }
};

export const getDocument = async (req, res) => {
    try {
        const { authcode: token } = req.headers;
        const { documentid: documentId } = req.params;
        
        const user = await getUserByToken(token);
        
        const document = await TeamDocumentService.getDocumentById(documentId);
        if (!document) {
            return res.status(404).json({ error: "notfound" });
        }
        
        res.status(200).json(document);
    } catch (error) {
        console.error('[getDocument] - Error:', error);
        res.status(500).json({ error: "internalerror" });
    }
};

export const downloadDocument = async (req, res) => {
    try {
        const { authcode: token } = req.headers;
        const { documentid: documentId } = req.params;
        
        const user = await getUserByToken(token);
        
        const document = await TeamDocumentService.getDocumentById(documentId);
        if (!document) {
            return res.status(404).json({ error: "notfound" });
        }

        const filePath = await TeamDocumentService.getDocumentPath(documentId);
        if (!filePath) {
            return res.status(404).json({ error: "filenotfound" });
        }

        // Prüfe ob Datei existiert
        const fs = await import('fs');
        if (!fs.existsSync(filePath)) {
            return res.status(404).json({ error: "filenotfound" });
        }

        // Setze Headers für Inline-Anzeige (PDF-Viewer)
        res.setHeader('Content-Disposition', `inline; filename="${document.originalFileName}"`);
        res.setHeader('Content-Type', document.mimeType);
        
        // Sende die Datei
        res.sendFile(filePath);
    } catch (error) {
        console.error('[downloadDocument] - Error:', error);
        res.status(500).json({ error: "internalerror" });
    }
};

export const deleteDocument = async (req, res) => {
    try {
        const { authcode: token } = req.headers;
        const { documentid: documentId } = req.params;
        
        const user = await getUserByToken(token);
        
        const success = await TeamDocumentService.deleteDocument(documentId);
        if (!success) {
            return res.status(404).json({ error: "notfound" });
        }
        
        res.status(200).json({ message: "Document deleted successfully" });
    } catch (error) {
        console.error('[deleteDocument] - Error:', error);
        res.status(500).json({ error: "internalerror" });
    }
};

export const parsePDF = async (req, res) => {
    try {
        const { authcode: token } = req.headers;
        const { documentid: documentId } = req.params;
        const { leagueid: leagueId } = req.query;
        
        const user = await getUserByToken(token);
        
        if (!leagueId) {
            return res.status(400).json({ error: "missingleagueid" });
        }
        
        // Hole Dokument-Informationen
        const document = await TeamDocumentService.getDocumentById(documentId);
        if (!document) {
            return res.status(404).json({ error: "documentnotfound" });
        }
        
        // Prüfe ob es eine PDF- oder TXT-Datei ist
        if (!document.mimeType.includes('pdf') && !document.mimeType.includes('text/plain')) {
            return res.status(400).json({ error: "notapdfortxt" });
        }
        
        // Parse PDF
        const parseResult = await PDFParserService.parsePDF(document.filePath, document.clubTeam.clubId);
        
        // Speichere Matches in Datenbank
        const saveResult = await PDFParserService.saveMatchesToDatabase(parseResult.matches, parseInt(leagueId));
        
        res.status(200).json({
            parseResult: {
                matchesFound: parseResult.matches.length,
                debugInfo: parseResult.debugInfo,
                allLines: parseResult.allLines,
                rawText: parseResult.rawText
            },
            saveResult: {
                created: saveResult.created,
                updated: saveResult.updated,
                errors: saveResult.errors
            }
        });
    } catch (error) {
        console.error('[parsePDF] - Error:', error);
        res.status(500).json({ error: "internalerror" });
    }
};