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

from odoo import api, fields, models, _


class WheelchairSectionOption(models.Model):
    _name = 'fusion.wc.section.option'
    _description = 'Wheelchair Section Product Option'
    _order = 'is_standard desc, sequence, id'

    section_id = fields.Many2one('fusion.wc.section', string='Section',
        required=True, ondelete='cascade', index=True)

    # ── Parent product (template level) ──
    product_tmpl_id = fields.Many2one('product.template',
        string='Product', required=True, index=True,
        help='The parent product. Variants (size, colour) are chosen '
             'when the sales rep adds this item to an assessment.')

    sequence = fields.Integer(string='Sequence', default=10)

    is_standard = fields.Boolean(string='Standard Option', default=False,
        help='Standard options are shown prominently; non-standard found via search')

    # ── Variant count (computed) ──
    variant_count = fields.Integer(string='Variants',
        compute='_compute_variant_count')

    # ── ADP info (from template) ──
    adp_device_code = fields.Char(string='ADP Code',
        related='product_tmpl_id.x_fc_adp_device_code', readonly=True)
    adp_price = fields.Float(string='ADP Price',
        related='product_tmpl_id.x_fc_adp_price', readonly=True)
    list_price = fields.Float(string='Sale Price',
        related='product_tmpl_id.list_price', readonly=True)

    # Build type applicability (for seating sections)
    available_build_types = fields.Selection([
        ('modular', 'Modular Only'),
        ('custom_fabricated', 'Custom Fabricated Only'),
        ('both', 'Both'),
    ], string='Build Types', default='both',
        help='Which build types this option is available for')

    requires_clinical_rationale = fields.Boolean(string='Requires Clinical Rationale',
        help='Sales rep must provide clinical rationale when selecting this option')

    # Compatibility rules
    incompatible_option_ids = fields.Many2many(
        'fusion.wc.section.option',
        'wc_option_incompatible_rel',
        'option_id', 'incompatible_option_id',
        string='Incompatible With',
        help='Cannot be selected together with these options')
    requires_option_ids = fields.Many2many(
        'fusion.wc.section.option',
        'wc_option_requires_rel',
        'option_id', 'required_option_id',
        string='Requires Options',
        help='These options must be selected first')

    @api.depends('product_tmpl_id')
    def _compute_variant_count(self):
        for record in self:
            if record.product_tmpl_id:
                record.variant_count = record.product_tmpl_id.product_variant_count
            else:
                record.variant_count = 0

    @api.depends('product_tmpl_id', 'section_id')
    def _compute_display_name(self):
        for record in self:
            parts = []
            if record.section_id:
                parts.append(record.section_id.name)
            if record.product_tmpl_id:
                parts.append(record.product_tmpl_id.display_name)
            record.display_name = ' / '.join(parts) if parts else ''
