from odoo import api, fields, models


class WooTaxMap(models.Model):
    _name = 'woo.tax.map'
    _description = 'WooCommerce Tax Mapping'

    instance_id = fields.Many2one('woo.instance', required=True, ondelete='cascade')
    tax_id = fields.Many2one('account.tax', required=True)
    woo_tax_class = fields.Char(required=True)
    woo_tax_class_name = fields.Char()
    company_id = fields.Many2one(
        'res.company', required=True, default=lambda self: self.env.company,
    )

    # ------------------------------------------------------------------
    # Lookup helpers (Task 26)
    # ------------------------------------------------------------------

    @api.model
    def get_odoo_tax(self, instance, wc_tax_class):
        """Return the Odoo account.tax mapped to a WC tax class.

        Args:
            instance: woo.instance record
            wc_tax_class: WC tax class slug (e.g. 'standard', 'reduced-rate')

        Returns:
            account.tax record or empty recordset
        """
        mapping = self.search([
            ('instance_id', '=', instance.id),
            ('woo_tax_class', '=', wc_tax_class),
        ], limit=1)
        return mapping.tax_id if mapping else self.env['account.tax']

    @api.model
    def get_wc_tax_class(self, instance, tax_id):
        """Return the WC tax class slug mapped to an Odoo tax.

        Args:
            instance: woo.instance record
            tax_id: account.tax record id

        Returns:
            str: WC tax class slug, or empty string if not mapped
        """
        mapping = self.search([
            ('instance_id', '=', instance.id),
            ('tax_id', '=', tax_id),
        ], limit=1)
        return mapping.woo_tax_class if mapping else ''
