from odoo import models, fields


class EncoachBranding(models.Model):
    _name = 'encoach.branding'
    _description = 'Entity Branding'

    entity_id = fields.Many2one('encoach.entity', required=True, ondelete='cascade')
    primary_color = fields.Char(default='#4F46E5')
    secondary_color = fields.Char(default='#7C3AED')
    background_color = fields.Char(default='#FFFFFF')
    logo = fields.Binary(attachment=True)
    logo_url = fields.Char(string='Logo URL')
    favicon = fields.Binary(attachment=True)
    app_name = fields.Char(default='EnCoach')
    white_label_domain = fields.Char(string='Subdomain')
    login_title = fields.Char(default='Welcome to EnCoach')
    login_description = fields.Text()
    custom_css = fields.Text()

    _entity_unique = models.Constraint('UNIQUE(entity_id)', 'Branding record must be unique per entity.')

    def to_api_dict(self):
        self.ensure_one()
        return {
            'id': self.id,
            'entity_id': self.entity_id.id,
            'entity_name': self.entity_id.name,
            'primary_color': self.primary_color,
            'secondary_color': self.secondary_color,
            'background_color': self.background_color or '#FFFFFF',
            'has_logo': bool(self.logo),
            'logo_url': self.logo_url or '',
            'has_favicon': bool(self.favicon),
            'app_name': self.app_name,
            'white_label_domain': self.white_label_domain or '',
            'login_title': self.login_title or '',
            'login_description': self.login_description or '',
            'custom_css': self.custom_css or '',
        }
