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

export const createBill = async (req, res) => {
    try {
        const { vendor, bill_number, amount, due_date, status } = req.body;

        if (!bill_number || !vendor || !amount) {
            return res.status(400).json({
                success: false,
                message: 'Bill number, vendor, and amount are required'
            });
        }

        const existing = await Bill.findOne({ where: { bill_number } });
        if (existing) {
            return res.status(400).json({ success: false, message: 'Bill number already exists' });
        }

        const bill = await Bill.create({
           vendor, bill_number, amount, due_date, status
        });

        res.status(201).json({ success: true, message: 'Bill created successfully', bill });
    } catch (error) {
        console.log(error);
        res.status(500).json({ success: false, message: 'Internal server error' });
    }
};

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

export const getBillById = async (req, res) => {
    try {
        const { id } = req.params;
        const bill = await Bill.findByPk(id);

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

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

export const updateBill = async (req, res) => {
    try {
        const { id } = req.params;
        const bill = await Bill.findByPk(id);

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

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

export const deleteBill = async (req, res) => {
    try {
        const { id } = req.params;
        const bill = await Bill.findByPk(id);

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

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



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

export const allpaidBills = async (req, res) => {
    try {
        const bills = await Bill.findAll({
            where: { status: 'paid' },
            order: [['created_at', 'DESC']]
        });

        res.status(200).json({
            success: true,
            bills
        });

    } catch (error) {
        console.log(error);

        res.status(500).json({
            success: false,
            message: 'Internal server error'
        });
    }
};

export const alloverdueBills = async (req, res) => {
    try {
        const bills = await Bill.findAll({
            where: { status: 'overdue' },
            order: [['created_at', 'DESC']]
        });

        res.status(200).json({
            success: true,
            bills
        });

    } catch (error) {
        console.log(error);

        res.status(500).json({
            success: false,
            message: 'Internal server error'
        });
    }
};