<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <!-- Monthly transaction summary → affiliate partner.
             Anchored on res.partner (one email per affiliate). The body
             queries job.partner.commission directly so the partner stays
             the single source of truth. -->
        <record id="email_template_affiliate_monthly_summary"
                model="mail.template">
            <field name="name">[Custom] Affiliate Monthly Transaction Summary</field>
            <field name="model_id" ref="base.model_res_partner"/>
            <field name="email_layout_xmlid" eval="False"/>
            <field name="lang">{{ object.lang }}</field>
            <field name="subject">{{ 'Transactions summary for partner %s — %s' % (object.name, format_date(context_timestamp(datetime.datetime.now()), date_format='MMMM yyyy')) }}</field>
            <field name="email_from">{{ (object.env.company.email or 'noreply@karriere-jura.de') }}</field>
            <field name="email_to">{{ object.email }}</field>
            <field name="body_html"><![CDATA[
<t t-call="kj_email_layout.mail_notification_layout_kj">
<t t-set="commissions" t-value="object.env['job.partner.commission'].sudo().search([('partner_id', '=', object.id)], order='create_date desc')"/>
<t t-set="cur" t-value="object.affiliate_currency_id.symbol"/>
<t t-set="total" t-value="sum(c.commission_amount for c in commissions if c.transaction_type == 'commission' and c.state == 'completed')"/>
<t t-set="payable" t-value="object.affiliate_current_amount"/>
<t t-set="base_url" t-value="object.env['ir.config_parameter'].sudo().get_param('web.base.url')"/>
    <p>Dear <t t-out="object.name"/>,</p>
    <p>
        Thank you for your partnership with Karriere-Jura. Below is the current
        summary of the transactions on your affiliate account.
    </p>
    <table width="100%" border="0" cellspacing="0" cellpadding="6"
           style="font-size:12px; line-height:17px; margin:15px 0; border-collapse:collapse;">
        <thead bgcolor="#d9d9d9" style="background-color:#d9d9d9; font-weight:bold;">
            <tr>
                <td style="border-bottom:1px solid #c0c0c0;">Job</td>
                <td style="border-bottom:1px solid #c0c0c0;">Date</td>
                <td style="border-bottom:1px solid #c0c0c0;">Type</td>
                <td align="right" style="border-bottom:1px solid #c0c0c0;">Amount</td>
                <td style="border-bottom:1px solid #c0c0c0;">Status</td>
            </tr>
        </thead>
        <tbody>
            <t t-foreach="commissions" t-as="c">
                <tr>
                    <td style="border-bottom:1px solid #eeeeee;">
                        <t t-out="c.service_title or (c.job_id.name if c.job_id else '—')"/>
                    </td>
                    <td style="border-bottom:1px solid #eeeeee;">
                        <t t-out="format_date(c.create_date)"/>
                    </td>
                    <td style="border-bottom:1px solid #eeeeee;">
                        <t t-if="c.transaction_type == 'commission'">Commission</t>
                        <t t-else="">Payout</t>
                    </td>
                    <td align="right" style="border-bottom:1px solid #eeeeee;">
                        <t t-out="'%.2f' % c.commission_amount"/> <t t-out="c.currency_id.symbol"/>
                    </td>
                    <td style="border-bottom:1px solid #eeeeee;">
                        <t t-if="c.state == 'completed'">Completed</t>
                        <t t-elif="c.state == 'canceled'">Canceled</t>
                        <t t-else="">Pending</t>
                    </td>
                </tr>
            </t>
            <tr t-if="not commissions">
                <td colspan="5" style="color:#666666;">No transactions recorded yet.</td>
            </tr>
        </tbody>
    </table>
    <p>
        <strong>Available commissions:</strong> <t t-out="'%.2f' % payable"/> <t t-out="cur"/><br/>
        <strong>Pending payouts:</strong> <t t-out="'%.2f' % object.affiliate_withdraw_amount"/> <t t-out="cur"/><br/>
        <strong>Paid out to date:</strong> <t t-out="'%.2f' % object.affiliate_paid_amount"/> <t t-out="cur"/>
    </p>
    <t t-if="object.affiliate_payable">
        <p>You are eligible to request a payout of your available commissions.</p>
        <p>
            <table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>
            <table border="0" cellspacing="0" cellpadding="0"><tr><td>
                <a t-attf-href="{{ base_url }}/my/affiliate/payout/request"
                   target="_blank"
                   style="font-size:16px; font-family:Helvetica,Arial,sans-serif;
                          color:#ffffff; text-decoration:none; border-radius:3px;
                          background-color:#b92c28;
                          border-top:12px solid #b92c28; border-bottom:12px solid #b92c28;
                          border-right:18px solid #b92c28; border-left:18px solid #b92c28;
                          display:inline-block;">
                    Request a payout →
                </a>
            </td></tr></table>
            </td></tr></table>
        </p>
    </t>
    <p>
        Want to earn more? Refer additional employers through your affiliate widget —
        our team is happy to provide additional marketing material on request.
    </p>
    <p>If you have any questions about your commissions, please don't hesitate to contact us.</p>
    <p>Kind regards,<br/>Your Karriere-Jura Team</p>
</t>
            ]]></field>
        </record>

        <!-- Admin alert when a new commission row appears (kept on the
             commission model — fired per-event, not per-partner). -->
        <record id="email_template_affiliate_transaction_admin"
                model="mail.template">
            <field name="name">[Custom] Affiliate Transaction – Admin Alert</field>
            <field name="model_id" ref="model_job_partner_commission"/>
            <field name="email_layout_xmlid" eval="False"/>
            <field name="lang">{{ object.partner_id.lang or 'de_DE' }}</field>
            <field name="subject">{{ ('New affiliate %s for %s [needs attention]') % (object.transaction_type, object.partner_id.name) }}</field>
            <field name="email_from">{{ (object.env.company.email or 'noreply@karriere-jura.de') }}</field>
            <field name="email_to">{{ ','.join(object.env.ref('base.group_system').users.mapped('email')) }}</field>
            <field name="body_html"><![CDATA[
<t t-call="kj_email_layout.mail_notification_layout_kj">
<t t-set="row_url" t-value="'%s/odoo/job-partner-commission/%s' % (object.get_base_url(), object.id)"/>
    <p>
        A new affiliate <t t-out="object.transaction_type"/> was recorded:<br/><br/>
        <strong>Partner:</strong> <t t-out="object.partner_id.name"/><br/>
        <t t-if="object.job_id">
            <strong>Job:</strong> <t t-out="object.job_id.name"/><br/>
        </t>
        <strong>Type:</strong> <t t-out="object.transaction_type"/><br/>
        <strong>Status:</strong> <t t-out="object.state"/><br/>
        <strong>Amount:</strong>
        <t t-out="'%.2f' % object.commission_amount"/> <t t-out="object.currency_id.symbol"/><br/>
        <t t-if="object.affiliate_note">
            <strong>Affiliate note:</strong> <t t-out="object.affiliate_note"/><br/>
        </t>
        <br/>
        Please review and confirm.
    </p>
    <p>
        <table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>
        <table border="0" cellspacing="0" cellpadding="0"><tr><td>
            <a t-att-href="row_url" target="_blank"
               style="font-size:16px; font-family:Helvetica,Arial,sans-serif;
                      color:#ffffff; text-decoration:none; border-radius:3px;
                      background-color:#b92c28;
                      border-top:12px solid #b92c28; border-bottom:12px solid #b92c28;
                      border-right:18px solid #b92c28; border-left:18px solid #b92c28;
                      display:inline-block;">
                Open record →
            </a>
        </td></tr></table>
        </td></tr></table>
    </p>
</t>
            ]]></field>
        </record>
    </data>
</odoo>
