// import multer from 'multer';
// import path from 'path';
// import { fileURLToPath } from 'url';
// import fs from 'fs';

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);

// // const uploadDir = path.join(__dirname, './uploads');
// const uploadDir = path.join(process.cwd(), 'uploads');
// if (!fs.existsSync(uploadDir)) {
//     fs.mkdirSync(uploadDir, { recursive: true });
// }
// // const uploadDir = path.join(process.cwd(), "./uploads");

// const storage = multer.diskStorage({
//     destination: (req, file, cb) => {
//         cb(null, uploadDir);
//     },
//     filename: (req, file, cb) => {
//         const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
//         cb(null, uniqueSuffix + path.extname(file.originalname));
//     }
// });

// const fileFilter = (req, file, cb) => {
//     const allowedTypes = /jpeg|jpg|png|gif|webp/;
//     const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
//     const mimetype = allowedTypes.test(file.mimetype);
    
//     if (mimetype && extname) {
//         cb(null, true);
//     } else {
//         cb(new Error('Only image files are allowed (jpeg, jpg, png, gif, webp)'));
//     }
// };

// const upload = multer({
//     storage: storage,
//     limits: { fileSize: 5 * 1024 * 1024 },
//     fileFilter: fileFilter
// });

// // Export both single and multiple upload middleware
// export const uploadSingleImage = upload.single('image');
// export const uploadMultipleImages = upload.array('images', 5);
// export const uploadAnyImage = upload.single('image'); // Alias for single

// export const deleteFile = (filename) => {
//     try {

//         if (!filename) return false;

//         const cleanFileName = path.basename(filename);
//         // remove /uploads/ from path
//         // const cleanFileName = filename.replace("/uploads/", "");

//         const filePath = path.join(uploadDir, cleanFileName);

//         if (fs.existsSync(filePath)) {
//             fs.unlinkSync(filePath);

//             console.log("File deleted:", cleanFileName);

//             return true;
//         }

//         return false;

//     } catch (error) {

//         console.error("Error deleting file:", error);

//         return false;
//     }
// };

// export const handleUploadError = (err, req, res, next) => {
//     if (err) {
//         console.error('Upload error:', err);
//         console.error('Error code:', err.code);
//         console.error('Error field:', err.field);
//         return res.status(400).json({
//             success: false,
//             message: err.message || 'File upload error'
//         });
//     }
//     next();
// };

import multer from 'multer';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';

import db from '../models/index.js'; 
const Invoice = db.Invoice;


// import invoiceModel from '../models/invoice.model.js'; 
// const Invoice = invoiceModel(sequelizeInstance, DataTypes); 

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const uploadDir = path.join(process.cwd(), 'uploads');
if (!fs.existsSync(uploadDir)) {
    fs.mkdirSync(uploadDir, { recursive: true });
}

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, uploadDir);
    },
    filename: (req, file, cb) => {
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
        cb(null, uniqueSuffix + path.extname(file.originalname));
    }
});

//  ASYNC FILE FILTER FOR DATABASE DUPLICATE CHECK
const fileFilter = async (req, file, cb) => {
    try {
        const { invoice_number } = req.body;
        
        if (invoice_number) {
            // Database mein check karein duplicate invoice_number
            const existingInvoice = await Invoice.findOne({ where: { invoice_number } });
            if (existingInvoice) {
                const error = new Error('Invoice number already exists');
                error.statusCode = 409;
                return cb(error, false); // File upload block ho jayegi!
            }
        }

        // Image Type Validation
        const allowedTypes = /jpeg|jpg|png|gif|webp/;
        const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
        const mimetype = allowedTypes.test(file.mimetype);
        
        if (mimetype && extname) {
            cb(null, true);
        } else {
            cb(new Error('Only image files are allowed (jpeg, jpg, png, gif, webp)'));
        }
    } catch (err) {
        cb(err, false);
    }
};

const upload = multer({
    storage: storage,
    limits: { fileSize: 5 * 1024 * 1024 }, // 5MB Limit
    fileFilter: fileFilter
});

export const singleUpload = upload.single('owner_signature'); 

export const uploadMultipleImages = upload.array('images', 5);
export const uploadAnyImage = upload.single('image');
export const logoUpload = upload.single('logo'); 
export const avatarUpload = upload.single('avatar');

export const deleteFile = (filename) => {
    try {
        if (!filename) return false;
        const cleanFileName = path.basename(filename);
        const filePath = path.join(uploadDir, cleanFileName);
        if (fs.existsSync(filePath)) {
            fs.unlinkSync(filePath);
            console.log("File deleted:", cleanFileName);
            return true;
        }
        return false;
    } catch (error) {
        console.error("Error deleting file:", error);
        return false;
    }
};

export const handleUploadError = (err, req, res, next) => {
    if (err) {
        console.error('Upload error:', err.message);
        
        if (err.message === 'Invoice number already exists' || err.statusCode === 409) {
            return res.status(409).json({
                success: false,
                message: 'Invoice number already exists'
            });
        }

        return res.status(400).json({
            success: false,
            message: err.message || 'File upload error'
        });
    }
    next();
};
