import { Op } from 'sequelize';
import { Accountant } from '../models/index.js';

export const createAccountant = async (req, res) => {
    try {
        const { name, email, phone, role, clients, invoices, status } = req.body;

        if (!name || !email) {
            return res.status(400).json({
                success: false,
                message: 'Name and email are required'
            });
        }

        const existing = await Accountant.findOne({
            where: {
                [Op.or]: [{ email }, { name }]
            }
        });

        if (existing) {
            return res.status(400).json({
                success: false,
                message: 'Accountant with that email or name already exists'
            });
        }

        const accountant = await Accountant.create({
            name,
            email,
            phone,
            role,
            clients,
            invoices,
            status: status || 'active'
        });

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

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

export const getAccountantById = async (req, res) => {
    try {
        const { id } = req.params;
        const accountant = await Accountant.findByPk(id);

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

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

export const updateAccountant = async (req, res) => {
    try {
        const { id } = req.params;
        const accountant = await Accountant.findByPk(id);

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

        await accountant.update(req.body);

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

export const deleteAccountant = async (req, res) => {
    try {
        const { id } = req.params;
        const accountant = await Accountant.findByPk(id);

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

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