import { Request, Response } from 'express';
import { AuthRequest } from '../middleware/auth';
import * as documentService from '../services/documentService';

export const getDocuments = async (req: Request, res: Response) => {
    try {
        const leadId = parseInt(req.params.leadId, 10);
        const documents = await documentService.getDocumentsByLead(leadId);
        res.json(documents);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to get documents' });
    }
};

export const uploadDocument = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const { leadId, documentType, documentName, filePath, fileSize, mimeType, password } = req.body;

        const document = await documentService.uploadDocument(
            leadId,
            documentType,
            documentName,
            filePath,
            fileSize,
            mimeType,
            req.user.userId,
            password
        );

        res.status(201).json(document);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to upload document' });
    }
};

export const createVersion = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const documentId = parseInt(req.params.documentId, 10);
        const { filePath, fileSize } = req.body;

        const version = await documentService.createDocumentVersion(
            documentId,
            filePath,
            fileSize,
            req.user.userId
        );

        res.status(201).json(version);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to create version' });
    }
};

export const verifyDocument = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const documentId = parseInt(req.params.documentId, 10);
        const { isVerified } = req.body;

        const document = await documentService.verifyDocument(documentId, isVerified, req.user.userId);
        res.json(document);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to verify document' });
    }
};

export const processOCR = async (req: Request, res: Response) => {
    try {
        const documentId = parseInt(req.params.documentId, 10);
        const document = await documentService.processOCR(documentId);
        res.json(document);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to process OCR' });
    }
};

export const aiVerify = async (req: Request, res: Response) => {
    try {
        const documentId = parseInt(req.params.documentId, 10);
        const document = await documentService.aiVerifyDocument(documentId);
        res.json(document);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to AI verify document' });
    }
};

export const deleteDocument = async (req: Request, res: Response) => {
    try {
        const documentId = parseInt(req.params.documentId, 10);
        const result = await documentService.deleteDocument(documentId);
        res.json(result);
    } catch (error: any) {
        res.status(404).json({ error: error.message || 'Document not found' });
    }
};
