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

from odoo import models, fields, api


class ProductBrandPricingRule(models.Model):
    _name = 'product.brand.pricing.rule'
    _description = 'Brand Pricing Rule'
    _order = 'sequence, id'

    brand_id = fields.Many2one(
        'product.brand', string='Brand', required=True,
        ondelete='cascade', index=True)
    name = fields.Char(
        string='Description', required=True,
        help='Short label for this rule, e.g. "Patient Lifts" or "Special Contract".')
    sequence = fields.Integer(default=10)
    active = fields.Boolean(default=True)

    apply_on = fields.Selection([
        ('all', 'All Products'),
        ('category', 'Product Category'),
        ('product', 'Specific Product'),
    ], string='Applies To', default='all', required=True)
    categ_id = fields.Many2one(
        'product.category', string='Product Category',
        help='Only used when Applies To = Product Category.')
    product_tmpl_id = fields.Many2one(
        'product.template', string='Product',
        help='Only used when Applies To = Specific Product.')

    pricing_method = fields.Selection([
        ('tiered_pct', 'Primary + Secondary %'),
        ('flat_pct', 'Single Discount %'),
        ('fixed_rebate', 'Fixed $ Rebate off MSRP'),
        ('fixed_cost', 'Flat Cost Price'),
    ], string='Pricing Method', default='tiered_pct', required=True)

    primary_discount_pct = fields.Float(string='Primary Discount (%)')
    secondary_discount_pct = fields.Float(string='Secondary Discount (%)')
    flat_discount_pct = fields.Float(string='Discount (%)')
    fixed_rebate_amount = fields.Float(string='Rebate Amount ($)')
    fixed_cost_price = fields.Float(string='Cost Price ($)')

    net_discount_pct = fields.Float(
        string='Effective Discount (%)',
        compute='_compute_net_discount', store=True,
        help='Combined effective discount shown as a single percentage.')

    @api.depends('pricing_method', 'primary_discount_pct',
                 'secondary_discount_pct', 'flat_discount_pct')
    def _compute_net_discount(self):
        for rule in self:
            if rule.pricing_method == 'tiered_pct':
                p = rule.primary_discount_pct or 0.0
                s = rule.secondary_discount_pct or 0.0
                remaining = (1 - p / 100) * (1 - s / 100)
                rule.net_discount_pct = round((1 - remaining) * 100, 2)
            elif rule.pricing_method == 'flat_pct':
                rule.net_discount_pct = rule.flat_discount_pct or 0.0
            else:
                rule.net_discount_pct = 0.0

    def calculate_cost(self, msrp):
        """Return the expected purchase cost for a given MSRP using this rule's method."""
        self.ensure_one()
        if self.pricing_method == 'tiered_pct':
            p = self.primary_discount_pct or 0.0
            s = self.secondary_discount_pct or 0.0
            return round(msrp * (1 - p / 100) * (1 - s / 100), 2)
        if self.pricing_method == 'flat_pct':
            d = self.flat_discount_pct or 0.0
            return round(msrp * (1 - d / 100), 2)
        if self.pricing_method == 'fixed_rebate':
            return round(max(msrp - (self.fixed_rebate_amount or 0.0), 0), 2)
        if self.pricing_method == 'fixed_cost':
            return self.fixed_cost_price or 0.0
        return msrp
