# -*- coding: utf-8 -*-
from odoo import models, fields, api

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    event_rental_request_id = fields.Many2one(
        'event.rental.request',
        string='Event Rental Request',
        readonly=True,
        copy=False
    )

    def action_confirm(self):
        res = super(SaleOrder, self).action_confirm()
        for order in self:
            if order.event_rental_request_id:
                order.event_rental_request_id.write({
                    'status': 'confirmed'
                })
                order.event_rental_request_id.message_post(
                    body=f"Sales Order {order.name} has been confirmed. Rental booking status set to Confirmed."
                )
        return res

    def action_cancel(self):
        res = super(SaleOrder, self).action_cancel()
        for order in self:
            if order.event_rental_request_id:
                order.event_rental_request_id.write({
                    'status': 'rejected'
                })
                order.event_rental_request_id.message_post(
                    body=f"Sales Order {order.name} was cancelled. Rental booking status set to Rejected."
                )
        return res
