import uuid
from odoo import models, fields, api


class MarketplaceOrderLine(models.Model):
    _name = 'marketplace.order.line'
    _description = 'Marketplace Order Line'

    # Основные поля
    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')
    product_id = fields.Many2one('products.product', string='Product', required=True)
    
    # Количество и цены
    quantity = fields.Float(string='Quantity', required=True, default=1.0)
    unit = fields.Char(string='Unit', required=True, default='tons')
    price_unit = fields.Float(string='Unit Price', default=0.0)
    subtotal = fields.Float(string='Subtotal', compute='_compute_subtotal', store=True)
    currency_id = fields.Many2one('res.currency', string='Currency', related='order_id.currency_id', store=True)
    
    # Дополнительная информация
    notes = fields.Text(string='Notes')
    
    @api.depends('quantity', 'price_unit')
    def _compute_subtotal(self):
        for line in self:
            line.subtotal = line.quantity * line.price_unit
    
    @api.onchange('product_id')
    def _onchange_product_id(self):
        if self.product_id:
            self.unit = self.product_id.unit or 'tons'