# -*- coding: utf-8 -*-
# Copyright 2024-2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)

from odoo import models


class LoanerEmailMixin(models.AbstractModel):
    _name = 'fusion.loaner.email.mixin'
    _description = 'Loaner Email Builder Mixin'

    _EMAIL_COLORS = {
        'info': '#2B6CB0',
        'success': '#38a169',
        'attention': '#d69e2e',
        'urgent': '#c53030',
    }

    def _email_build(
        self,
        title,
        summary,
        sections=None,
        note=None,
        note_color=None,
        email_type='info',
        attachments_note=None,
        button_url=None,
        button_text='View Details',
        sender_name=None,
        extra_html='',
    ):
        accent = self._EMAIL_COLORS.get(email_type, self._EMAIL_COLORS['info'])
        company = self._get_company_info()

        parts = []
        parts.append(
            f'<div style="font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,Arial,sans-serif;'
            f'max-width:600px;margin:0 auto;">'
            f'<div style="height:4px;background-color:{accent};"></div>'
            f'<div style="padding:32px 28px;">'
        )
        parts.append(
            f'<p style="color:{accent};font-size:13px;font-weight:600;letter-spacing:0.5px;'
            f'text-transform:uppercase;margin:0 0 24px 0;">{company["name"]}</p>'
        )
        parts.append(
            f'<h2 style="font-size:22px;font-weight:700;'
            f'margin:0 0 6px 0;line-height:1.3;">{title}</h2>'
        )
        parts.append(
            f'<p style="opacity:0.65;font-size:15px;line-height:1.5;'
            f'margin:0 0 24px 0;">{summary}</p>'
        )

        if sections:
            for heading, rows in sections:
                parts.append(self._email_section(heading, rows))

        if note:
            nc = note_color or accent
            parts.append(self._email_note(note, nc))

        if extra_html:
            parts.append(extra_html)

        if attachments_note:
            parts.append(self._email_attachment_note(attachments_note))

        if button_url:
            parts.append(self._email_button(button_url, button_text, accent))

        signer = sender_name or (self.env.user.name if self.env.user else '')
        parts.append(
            f'<p style="font-size:14px;line-height:1.6;margin:24px 0 0 0;">'
            f'Best regards,<br/>'
            f'<strong>{signer}</strong><br/>'
            f'<span style="opacity:0.6;">{company["name"]}</span></p>'
        )
        parts.append('</div>')

        footer_parts = [company['name']]
        if company['phone']:
            footer_parts.append(company['phone'])
        if company['email']:
            footer_parts.append(company['email'])
        footer_text = ' &middot; '.join(footer_parts)

        parts.append(
            f'<div style="padding:16px 28px;text-align:center;">'
            f'<p style="opacity:0.5;font-size:11px;line-height:1.5;margin:0;">'
            f'{footer_text}<br/>'
            f'This is an automated notification from the Loaner Management System.</p>'
            f'</div>'
        )
        parts.append('</div>')
        return ''.join(parts)

    def _email_section(self, heading, rows):
        if not rows:
            return ''
        html = (
            '<table style="width:100%;border-collapse:collapse;margin:0 0 24px 0;">'
            f'<tr><td colspan="2" style="padding:10px 14px;font-size:12px;font-weight:600;'
            f'opacity:0.55;text-transform:uppercase;letter-spacing:0.5px;'
            f'border-bottom:2px solid rgba(128,128,128,0.25);">{heading}</td></tr>'
        )
        for label, value in rows:
            if value is None or value == '' or value is False:
                continue
            html += (
                f'<tr>'
                f'<td style="padding:10px 14px;opacity:0.6;font-size:14px;'
                f'border-bottom:1px solid rgba(128,128,128,0.15);width:35%;">{label}</td>'
                f'<td style="padding:10px 14px;font-size:14px;'
                f'border-bottom:1px solid rgba(128,128,128,0.15);">{value}</td>'
                f'</tr>'
            )
        html += '</table>'
        return html

    def _email_note(self, text, color='#2B6CB0'):
        return (
            f'<div style="border-left:3px solid {color};padding:12px 16px;'
            f'margin:0 0 24px 0;">'
            f'<p style="margin:0;font-size:14px;line-height:1.5;">{text}</p>'
            f'</div>'
        )

    def _email_button(self, url, text='View Details', color='#2B6CB0'):
        return (
            f'<p style="text-align:center;margin:28px 0;">'
            f'<a href="{url}" style="display:inline-block;background:{color};color:#ffffff;'
            f'padding:12px 28px;text-decoration:none;border-radius:6px;'
            f'font-size:14px;font-weight:600;">{text}</a></p>'
        )

    def _email_attachment_note(self, description):
        return (
            f'<div style="padding:10px 14px;border:1px dashed rgba(128,128,128,0.35);border-radius:6px;'
            f'margin:0 0 24px 0;">'
            f'<p style="margin:0;font-size:13px;opacity:0.65;">'
            f'<strong style="opacity:1;">Attached:</strong> {description}</p>'
            f'</div>'
        )

    def _email_status_badge(self, label, color='#2B6CB0'):
        bg_map = {
            '#38a169': 'rgba(56,161,105,0.12)',
            '#2B6CB0': 'rgba(43,108,176,0.12)',
            '#d69e2e': 'rgba(214,158,46,0.12)',
            '#c53030': 'rgba(197,48,48,0.12)',
        }
        bg = bg_map.get(color, 'rgba(43,108,176,0.12)')
        return (
            f'<span style="display:inline-block;background:{bg};color:{color};'
            f'padding:2px 10px;border-radius:12px;font-size:12px;font-weight:600;">'
            f'{label}</span>'
        )

    def _get_company_info(self):
        company = getattr(self, 'company_id', None) or self.env.company
        return {
            'name': company.name or 'Our Company',
            'phone': company.phone or '',
            'email': company.email or '',
        }

    def _email_is_enabled(self):
        ICP = self.env['ir.config_parameter'].sudo()
        val = ICP.get_param('fusion_loaners.enable_email_notifications', 'True')
        return val.lower() in ('true', '1', 'yes')
