from odoo import models, fields, api


class LogisticsCompatibility(models.Model):
    _name = 'logistics.compatibility'
    _description = 'Logistics Compatibility Rules'
    _order = 'cargo_type, container_type_id, vehicle_type_id'

    name = fields.Char(
        string='Rule Name',
        compute='_compute_name',
        store=True,
        help='Automatically generated rule name'
    )
    
    cargo_type = fields.Selection([
        ('general', 'General Cargo'),
        ('bulk_dry', 'Dry Bulk'),
        ('bulk_liquid', 'Liquid Bulk'),
        ('containerized', 'Containerized'),
        ('refrigerated', 'Refrigerated'),
        ('hazmat', 'Hazardous Materials'),
        ('oversized', 'Oversized/Heavy'),
        ('livestock', 'Livestock'),
        ('vehicles', 'Vehicles'),
        ('project', 'Project Cargo'),
    ], string='Cargo Type', required=True)
    
    vehicle_type_id = fields.Many2one(
        'logistics.vehicle.type',
        string='Compatible Vehicle Type',
        required=True,
        help='Vehicle type that can handle this cargo/container combination'
    )
    
    container_type_id = fields.Many2one(
        'logistics.container.type',
        string='Container Type',
        help='Required container type for this cargo'
    )
    
    active = fields.Boolean(
        string='Active',
        default=True
    )

    @api.depends('cargo_type', 'container_type_id', 'vehicle_type_id')
    def _compute_name(self):
        for record in self:
            cargo = dict(record._fields['cargo_type'].selection).get(record.cargo_type, 'Unknown')
            container = record.container_type_id.name if record.container_type_id else 'Any Container'
            vehicle = record.vehicle_type_id.name if record.vehicle_type_id else 'Unknown Vehicle'
            record.name = f"{cargo} / {container} / {vehicle}"

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