from odoo import models, fields, api
from odoo.exceptions import UserError


class StockPicking(models.Model):
    _inherit = 'stock.picking'

    user_id = fields.Many2one(string='Driver')

    @api.onchange('user_id')
    def _onchange_user_id(self):
        self.sale_id.driver_id = self.user_id

    def action_upload_attachments(self):
        return {
            'name': 'Upload Attachment',
            'type': 'ir.actions.act_window',
            'res_model': 'upload.attachment.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'default_picking_id': self.id,
            }
        }

    # def button_validate(self):
    #     for picking in self:
    #         if picking.picking_type_code == 'outgoing':
    #             attachments = self.env['ir.attachment'].search([
    #                 ('res_model', '=', 'stock.picking'),
    #                 ('res_id', '=', picking.id)
    #             ])
    #             if not attachments:
    #                 raise UserError("Please add the attachments before validating this delivery.")
    #     return super(StockPicking, self).button_validate()