# -*- coding: utf-8 -*-

from odoo import api, fields, models, _


class WheelchairUpchargeRule(models.Model):
    _name = 'fusion.wc.upcharge.rule'
    _description = 'Wheelchair Upcharge Rule'
    _order = 'sequence, id'

    name = fields.Char(string='Name', required=True,
        help='e.g. "Width > 18 inches (WAMA)"')
    active = fields.Boolean(string='Active', default=True)
    sequence = fields.Integer(string='Sequence', default=10)

    # Trigger configuration
    trigger_type = fields.Selection([
        ('measurement', 'Measurement Threshold'),
        ('weight', 'Client Weight'),
        ('dimension_mismatch', 'Dimension Mismatch'),
    ], string='Trigger Type', required=True)

    # For measurement triggers
    measurement_field = fields.Selection([
        ('seat_width', 'Seat Width'),
        ('seat_depth', 'Seat Depth'),
        ('back_width', 'Backrest Width'),
        ('back_height', 'Back Height'),
        ('seat_to_floor', 'Seat to Floor Height'),
        ('leg_rest_length', 'Leg Rest Length'),
    ], string='Measurement Field')
    comparison = fields.Selection([
        ('gt', 'Greater Than'),
        ('gte', 'Greater Than or Equal'),
        ('lt', 'Less Than'),
        ('eq', 'Equal To'),
        ('neq', 'Not Equal To'),
    ], string='Comparison', default='gt')
    threshold_value = fields.Float(string='Threshold Value',
        help='e.g. 18.0 for WAMA width threshold')

    # For weight triggers
    weight_min = fields.Float(string='Min Weight (lbs)',
        help='Trigger when client weight exceeds this value')
    weight_max = fields.Float(string='Max Weight (lbs)',
        help='Upper bound (0 or empty = no upper limit)')

    # For dimension mismatch
    compare_field_1 = fields.Selection([
        ('seat_width', 'Seat Width'),
        ('back_width', 'Backrest Width'),
        ('seat_depth', 'Seat Depth'),
    ], string='Compare Field 1')
    compare_field_2 = fields.Selection([
        ('seat_width', 'Seat Width'),
        ('back_width', 'Backrest Width'),
        ('seat_depth', 'Seat Depth'),
    ], string='Compare Field 2')

    # What to add when triggered
    adp_device_code = fields.Char(string='ADP Device Code', required=True,
        help='ADP code to add (e.g. WAMA, WAMB, WAMF)')
    product_id = fields.Many2one('product.product', string='Product to Add',
        help='If set, this product will be added to the quotation. '
             'If empty, a generic line is created from the ADP code.')

    # Equipment type applicability
    equipment_type = fields.Selection(
        selection='_get_equipment_type_selection',
        string='Equipment Type', default='both', required=True)

    @api.model
    def _get_equipment_type_selection(self):
        types = self.env['fusion.equipment.type'].sudo().search([], order='sequence')
        if types:
            result = [(t.code, t.name) for t in types]
            result.append(('both', 'Both (All Types)'))
            return result
        return [
            ('manual_wheelchair', 'Manual Wheelchair'),
            ('power_wheelchair', 'Power Wheelchair'),
            ('both', 'Both'),
        ]

    # Mutual exclusion
    mutually_exclusive_group = fields.Char(string='Exclusive Group',
        help='Rules in the same group are mutually exclusive '
             '(only the highest-priority matching rule applies). '
             'e.g. "weight" for WAMF/WAMG/WAMH')

    description = fields.Text(string='Description',
        help='Explanation shown to the sales rep when this rule triggers')

    # Linked ADP device code record (for price lookup)
    adp_device_code_id = fields.Many2one('fusion.adp.device.code',
        string='ADP Device Code Record',
        compute='_compute_adp_device_code_id', store=True)

    @api.depends('adp_device_code')
    def _compute_adp_device_code_id(self):
        ADPDevice = self.env['fusion.adp.device.code'].sudo()
        for record in self:
            if record.adp_device_code:
                record.adp_device_code_id = ADPDevice.search(
                    [('device_code', '=', record.adp_device_code), ('active', '=', True)],
                    limit=1
                )
            else:
                record.adp_device_code_id = False
