from odoo import api, fields, models


class RentalRenewalLog(models.Model):
    _name = 'rental.renewal.log'
    _description = 'Rental Renewal Log'
    _order = 'create_date desc'
    _rec_name = 'display_name'

    order_id = fields.Many2one(
        'sale.order',
        string="Rental Order",
        required=True,
        ondelete='cascade',
        index=True,
    )
    partner_id = fields.Many2one(
        related='order_id.partner_id',
        store=True,
        string="Customer",
    )
    renewal_number = fields.Integer(
        string="Renewal #",
        required=True,
    )
    previous_start_date = fields.Datetime(string="Previous Start")
    previous_return_date = fields.Datetime(string="Previous Return")
    new_start_date = fields.Datetime(string="New Start")
    new_return_date = fields.Datetime(string="New Return")
    invoice_id = fields.Many2one(
        'account.move',
        string="Invoice",
        ondelete='set null',
    )
    payment_status = fields.Selection(
        [
            ('pending', 'Pending'),
            ('paid', 'Paid'),
            ('failed', 'Failed'),
        ],
        string="Payment Status",
        default='pending',
    )
    payment_transaction_id = fields.Many2one(
        'payment.transaction',
        string="Payment Transaction",
        ondelete='set null',
    )
    renewal_type = fields.Selection(
        [
            ('automatic', 'Automatic'),
            ('manual', 'Manual'),
        ],
        string="Renewal Type",
        required=True,
    )
    state = fields.Selection(
        [
            ('draft', 'Draft'),
            ('done', 'Done'),
            ('failed', 'Failed'),
            ('cancelled', 'Cancelled'),
        ],
        string="Status",
        default='draft',
        required=True,
    )
    notes = fields.Text(string="Notes")

    @api.depends('order_id', 'renewal_number')
    def _compute_display_name(self):
        for rec in self:
            rec.display_name = (
                f"{rec.order_id.name or 'New'} - Renewal #{rec.renewal_number}"
            )
