import uuid
from odoo import models, fields, api


class MarketplaceOrderTrip(models.Model):
    _name = 'marketplace.order.trip'
    _description = 'Marketplace Order Trip'
    _order = 'stage_id, sequence'

    # Основные поля
    uuid = fields.Char(string='UUID', required=True, default=lambda self: str(uuid.uuid4()), index=True)
    name = fields.Char(string='Trip Name', required=True)
    description = fields.Text(string='Description')
    
    # Связи
    stage_id = fields.Many2one('marketplace.order.stage', string='Stage', required=True, ondelete='cascade')
    order_id = fields.Many2one('marketplace.order', string='Order', related='stage_id.order_id', store=True)
    carrier_id = fields.Many2one('carrier', string='Carrier', required=True)
    
    # Порядок
    sequence = fields.Integer(string='Sequence', default=10)
    
    # 5 ключевых дат для трекинга
    planned_loading_date = fields.Datetime(string='Planned Loading Date')
    actual_loading_date = fields.Datetime(string='Actual Loading Date')
    planned_unloading_date = fields.Datetime(string='Planned Unloading Date')
    actual_unloading_date = fields.Datetime(string='Actual Unloading Date')
    real_loading_date = fields.Datetime(string='Real Loading Date', help='Alternative loading date tracking')
    
    # Контроль веса
    planned_weight = fields.Float(string='Planned Weight', help='Expected weight for this trip')
    weight_at_loading = fields.Float(string='Weight at Loading', help='Actual weight when loaded')
    weight_at_unloading = fields.Float(string='Weight at Unloading', help='Actual weight when unloaded')
    
    # Дополнительная информация
    notes = fields.Text(string='Notes')
    created_at = fields.Datetime(string='Created At', default=fields.Datetime.now, required=True)
    updated_at = fields.Datetime(string='Updated At', default=fields.Datetime.now, required=True)
    
    def write(self, vals):
        vals['updated_at'] = fields.Datetime.now()
        return super().write(vals)