from odoo import models, fields, api, _
import werkzeug.urls
import logging

_logger = logging.getLogger(__name__)


class RestaurantTable(models.Model):
    _inherit = 'restaurant.table'

    self_order_url = fields.Char(
        string='Self-Order URL',
        compute='_compute_self_order_url',
        help='The unique URL for this table to open the ordering menu'
    )
    
    @api.depends('floor_id', 'name')
    def _compute_self_order_url(self):
        base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
        for table in self:
            # We use a unique access token if we want more security, but for now ID is fine for the flow
            params = {
                'table_id': table.id,
                'floor_id': table.floor_id.id,
            }
            # The public URL that the QR will point to
            url = f"{base_url}/dine360/menu?{werkzeug.urls.url_encode(params)}"
            table.self_order_url = url

    def action_generate_qr_code(self):
        """Action to generate or print the QR code for this table"""
        # In a full implementation, we'd use report logic to print a PDF with the QR
        # For now, we'll expose the URL for testing
        return {
            'type': 'ir.actions.act_url',
            'url': self.self_order_url,
            'target': 'new',
        }
