from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

import logging

_logger = logging.getLogger(__name__)


class ResPartner(models.Model):
    _inherit = "res.partner"

    is_education_entity = fields.Boolean(string="Is Education Entity", default=False, )
    education_level_ids = fields.Many2many(
        comodel_name='education.level', relation='res_partner_education_level_rel',
        column1='partner_id', column2='level_id', string='Education Levels', )
    is_supporter = fields.Boolean(string='Supporter', default=False, )

    def unlink(self):
        env = self.env

        for partner in self:
            grants = env['grant.benefit'].search([
                ('partner_id', '=', partner.id),
                ('state', 'not in', ['draft', 'new'])
            ])
            if grants:
                grant_info = ', '.join(
                    '[%s: %s]' % (g.code or g.name or g.id, dict(g._fields['state'].selection).get(g.state)) for g in
                    grants)
                raise ValidationError(
                    _("Cannot delete partner '%s': linked to Benefits - Profiles %s") % (partner.name, grant_info)
                )
        return super().unlink()
