import uuid
from odoo import models, fields


class MarketplaceOrderTracking(models.Model):
    _name = 'marketplace.order.tracking'
    _description = 'Marketplace Order Tracking'
    _order = 'timestamp desc'

    # Основные поля
    uuid = fields.Char(string='UUID', required=True, default=lambda self: str(uuid.uuid4()), index=True)
    
    # Связи
    order_id = fields.Many2one('marketplace.order', string='Order', required=True, ondelete='cascade')
    stage_id = fields.Many2one('marketplace.order.stage', string='Stage', ondelete='cascade')
    trip_id = fields.Many2one('marketplace.order.trip', string='Trip', ondelete='cascade')
    location_id = fields.Many2one('logistics.node', string='Location')
    
    # Трекинг данные
    status = fields.Selection([
        ('created', 'Order Created'),
        ('confirmed', 'Order Confirmed'),
        ('processing', 'Processing'),
        ('picked_up', 'Picked Up'),
        ('in_transit', 'In Transit'),
        ('delivered', 'Delivered'),
        ('cancelled', 'Cancelled'),
        # Статусы этапов
        ('stage_started', 'Stage Started'),
        ('stage_completed', 'Stage Completed'),
        # Статусы рейсов
        ('trip_assigned', 'Trip Assigned'),
        ('loading_started', 'Loading Started'),
        ('loading_completed', 'Loading Completed'),
        ('unloading_started', 'Unloading Started'),
        ('trip_completed', 'Trip Completed'),
    ], string='Status', required=True)
    
    timestamp = fields.Datetime(string='Timestamp', required=True, default=fields.Datetime.now)
    description = fields.Text(string='Description')
    
    # Метаданные
    created_by = fields.Char(string='Created By')  # User ID who created this event
    external_tracking_id = fields.Char(string='External Tracking ID')  # ID from logistics provider
    
    # Дополнительные данные для трекинга
    latitude = fields.Float(string='Latitude', digits=(10, 7))
    longitude = fields.Float(string='Longitude', digits=(10, 7))
    estimated_arrival = fields.Datetime(string='Estimated Arrival')
    actual_arrival = fields.Datetime(string='Actual Arrival')
    
    # Связанная информация
    carrier_name = fields.Char(string='Carrier Name')
    tracking_number = fields.Char(string='Tracking Number')
    vehicle_info = fields.Char(string='Vehicle Info')
    driver_contact = fields.Char(string='Driver Contact')