from odoo import models, fields, api


class LogisticsTransportAttribute(models.Model):
    _name = 'logistics.transport.attribute'
    _description = 'Transport Attribute'
    _order = 'sequence, name'

    name = fields.Char(
        string='Attribute Name',
        required=True,
        help='Name of the transport attribute (e.g., "Loading Type", "Max Weight")'
    )
    
    sequence = fields.Integer(
        string='Sequence',
        default=10,
        help='Sequence for ordering attributes'
    )
    
    value_ids = fields.One2many(
        'logistics.transport.attribute.value',
        'attribute_id',
        string='Attribute Values',
        help='Possible values for this attribute'
    )
    
    active = fields.Boolean(
        string='Active',
        default=True
    )

    def name_get(self):
        result = []
        for record in self:
            result.append((record.id, record.name))
        return result


class LogisticsTransportAttributeValue(models.Model):
    _name = 'logistics.transport.attribute.value'
    _description = 'Transport Attribute Value'
    _order = 'attribute_id, sequence, name'

    name = fields.Char(
        string='Value',
        required=True,
        help='Value for the attribute (e.g., "Container", "Bulk", "Tank")'
    )
    
    attribute_id = fields.Many2one(
        'logistics.transport.attribute',
        string='Attribute',
        required=True,
        ondelete='cascade',
        help='Related transport attribute'
    )
    
    sequence = fields.Integer(
        string='Sequence',
        default=10,
        help='Sequence for ordering values'
    )
    
    active = fields.Boolean(
        string='Active',
        default=True
    )

    def name_get(self):
        result = []
        for record in self:
            result.append((record.id, record.name))
        return result


class LogisticsTransportAttributeLine(models.Model):
    _name = 'logistics.transport.attribute.line'
    _description = 'Transport Attribute Line'
    _order = 'vehicle_type_id, attribute_id'

    vehicle_type_id = fields.Many2one(
        'logistics.vehicle.type',
        string='Vehicle Type',
        required=True,
        ondelete='cascade',
        help='Related vehicle type'
    )
    
    attribute_id = fields.Many2one(
        'logistics.transport.attribute',
        string='Attribute',
        required=True,
        help='Transport attribute'
    )
    
    value_ids = fields.Many2many(
        'logistics.transport.attribute.value',
        'transport_attr_line_value_rel',
        'line_id',
        'value_id',
        string='Values',
        help='Selected values for this attribute'
    )

    @api.onchange('attribute_id')
    def _onchange_attribute_id(self):
        if self.attribute_id:
            # Clear current values when attribute changes
            self.value_ids = False
            
            # Set domain for value_ids to show only values for selected attribute
            return {
                'domain': {
                    'value_ids': [('attribute_id', '=', self.attribute_id.id)]
                }
            }
        return {'domain': {'value_ids': []}}