from odoo import models, fields, api


class SaleOrder(models.Model):
    _name = 'sale.order'
    _inherit = ["sale.order", "directive.mixin"]

    def _directive_generation_spec(self):
        return [
            {
                "event": "on_write",
                "key": "so_trigger_wms",
                "field": "state",
                "from": "draft",
                "to": "sale",
                "use_record_template_group": True,
                "mode": "create_once",
            }
        ]

    def action_wms_response(self, payload: dict):
        for order in self:
            bin_location = "ZONE-A-ROW-5"

            order.message_post(
                body=f"WMS Агент: Товар зарезервирован."
                     f"Ячейка: {bin_location}<br/>"
                     f"Директива #{payload.get('directive_id')} закрыта успешно."
            )

        return True