from odoo import api, fields, models


class WooPricelistMap(models.Model):
    _name = 'woo.pricelist.map'
    _description = 'WooCommerce Pricelist Mapping'

    instance_id = fields.Many2one('woo.instance', required=True, ondelete='cascade')
    pricelist_id = fields.Many2one('product.pricelist', required=True)
    woo_role = fields.Char(required=True)
    woo_role_name = fields.Char()
    company_id = fields.Many2one(
        'res.company', required=True, default=lambda self: self.env.company,
    )

    # ------------------------------------------------------------------
    # Lookup helper (Task 26)
    # ------------------------------------------------------------------

    @api.model
    def get_pricelist_for_role(self, instance, wc_role):
        """Return the Odoo product.pricelist mapped to a WC customer role.

        Args:
            instance: woo.instance record
            wc_role: WC customer role slug (e.g. 'wholesale', 'customer')

        Returns:
            product.pricelist record or empty recordset
        """
        mapping = self.search([
            ('instance_id', '=', instance.id),
            ('woo_role', '=', wc_role),
        ], limit=1)
        return mapping.pricelist_id if mapping else self.env['product.pricelist']
