import { Salary } from '../models/index.js';

export const createSalary = async (req, res) => {
    try {
        const {
            employee_name,
            employee_id,
            designation,
            joining_date,
            amount,
            status,
            payment_date
        } = req.body;

        //  SYSTEM GENERATED DATE
        const now = new Date();

        const currentYear = now.getFullYear();

        const monthNames = [
            "January", "February", "March", "April",
            "May", "June", "July", "August",
            "September", "October", "November", "December"
        ];

        const currentMonth = monthNames[now.getMonth()];

        if (!employee_name || !employee_id || !amount) {
            return res.status(400).json({
                success: false,
                message: "Employee ID, name and amount are required"
            });
        }

        const existingSalary = await Salary.findOne({
            where: {
                employee_id,
                salary_month: currentMonth,
                salary_year: currentYear
            }
        });

        if (existingSalary) {
            return res.status(400).json({
                success: false,
                message: `Salary already created for ${currentMonth} ${currentYear}`
            });
        }

        const salary = await Salary.create({
            employee_name,
            employee_id,
            designation,
            joining_date,
            salary_month: currentMonth,   
            salary_year: currentYear,     
            amount,
            status: status || 'pending',
            payment_date,
        });

        return res.status(201).json({
            success: true,
            message: `Salary created for ${currentMonth} ${currentYear}`,
            salary
        });

    } catch (error) {
        console.log(error);
        return res.status(500).json({
            success: false,
            message: 'Internal server error'
        });
    }
};
export const getSalaries = async (req, res) => {
    try {
        const salaries = await Salary.findAll({ order: [['created_at', 'DESC']] });
        res.status(200).json({ success: true, salaries });
    } catch (error) {
        console.log(error);
        res.status(500).json({ success: false, message: 'Internal server error' });
    }
};

export const getSalaryById = async (req, res) => {
    try {
        const { id } = req.params;
        const salary = await Salary.findByPk(id);

        if (!salary) {
            return res.status(404).json({ success: false, message: 'Salary record not found' });
        }

        res.status(200).json({ success: true, salary });
    } catch (error) {
        console.log(error);
        res.status(500).json({ success: false, message: 'Internal server error' });
    }
};

// export const updateSalary = async (req, res) => {
//     try {
//         const { id } = req.params;
//         const salary = await Salary.findByPk(id);

//         if (!salary) {
//             return res.status(404).json({ success: false, message: 'Salary record not found' });
//         }

//         await salary.update(req.body);
//         res.status(200).json({ success: true, message: 'Salary record updated successfully', salary });
//     } catch (error) {
//         console.log(error);
//         res.status(500).json({ success: false, message: 'Internal server error' });
//     }
// };

// export const deleteSalary = async (req, res) => {
//     try {
//         const { id } = req.params;
//         const salary = await Salary.findByPk(id);

//         if (!salary) {
//             return res.status(404).json({ success: false, message: 'Salary record not found' });
//         }

//         await salary.destroy();
//         res.status(200).json({ success: true, message: 'Salary record deleted successfully' });
//     } catch (error) {
//         console.log(error);
//         res.status(500).json({ success: false, message: 'Internal server error' });
//     }
// };
