# -*- coding: utf-8 -*-
# Copyright 2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)

from datetime import datetime, timedelta
from odoo import models, fields


class FusionClockAICache(models.Model):
    _name = 'fusion.clock.ai.cache'
    _description = 'AI Response Cache'
    _order = 'created_at desc'

    cache_key = fields.Char(required=True, index=True)
    prompt_key = fields.Char(index=True)
    response_text = fields.Text(required=True)
    created_at = fields.Datetime(default=fields.Datetime.now, required=True)

    def _gc_expired_cache(self):
        """Cron: delete cache entries older than 24 hours."""
        cutoff = datetime.now() - timedelta(hours=24)
        self.search([('created_at', '<', cutoff)]).unlink()
