# Copyright © 2023 Garazd Creation (<https://garazd.biz>)
# @author: Yurii Razumovskyi (<support@garazd.biz>)
# @author: Iryna Razumovska (<support@garazd.biz>)
# License OPL-1 (https://www.odoo.com/documentation/15.0/legal/licenses.html).

from datetime import datetime, timedelta

from odoo import api, models
from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT

LABEL_ATTACHMENT_NAME = 'Product Label Direct Printing'


class PrintProductLabel(models.TransientModel):
    _inherit = "print.product.label"

    def action_print_direct(self):
        """ Print labels directly without download. """
        self.ensure_one()
        attachment = self.env['ir.attachment'].create({
            'name': LABEL_ATTACHMENT_NAME,
            'type': 'binary',
            'datas': self.get_pdf(),
            'mimetype': 'application/pdf',
            'public': False,
        })
        return {
            'type': 'ir.actions.act_url',
            'url': f'/print_label/{attachment.id}',
            'target': 'new',
        }

    @api.autovacuum
    def _gc_print_label_attachments(self):
        timeout_ago = datetime.utcnow() - timedelta(days=1)
        domain = [
            ('name', '=', LABEL_ATTACHMENT_NAME),
            ('mimetype', '=', 'application/pdf'),
            ('create_date', '<', timeout_ago.strftime(DEFAULT_SERVER_DATETIME_FORMAT)),
        ]
        return self.env['ir.attachment'].sudo().search(domain).unlink()
