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

import logging
from datetime import date, timedelta
from odoo import models, fields, api

_logger = logging.getLogger(__name__)


class HrEmployeeAI(models.Model):
    _inherit = 'hr.employee'

    x_fclk_ai_summary = fields.Text(string='AI Attendance Summary', readonly=True)
    x_fclk_ai_summary_date = fields.Date(string='Summary Generated', readonly=True)
    x_fclk_ai_coach_tip = fields.Text(string='AI Coach Tip', readonly=True)

    def action_generate_ai_summary(self):
        AI = self.env['fusion.clock.ai.service'].sudo()
        for emp in self:
            context_data = AI._build_employee_context(
                emp, date.today() - timedelta(days=30), date.today()
            )
            system_prompt = AI._get_system_prompt('activity_log_summary')
            try:
                summary = AI.chat_completion(
                    [
                        {'role': 'system', 'content': system_prompt},
                        {'role': 'user', 'content': context_data},
                    ],
                    feature='log_summary',
                )
                emp.write({
                    'x_fclk_ai_summary': summary,
                    'x_fclk_ai_summary_date': date.today(),
                })
            except Exception as e:
                _logger.warning("Failed to generate AI summary for %s: %s", emp.name, e)

    def action_generate_coach_tip(self):
        AI = self.env['fusion.clock.ai.service'].sudo()
        for emp in self:
            context_data = AI._build_employee_context(
                emp, date.today() - timedelta(days=14), date.today()
            )
            system_prompt = AI._get_system_prompt('attendance_coach')
            try:
                tip = AI.chat_completion(
                    [
                        {'role': 'system', 'content': system_prompt},
                        {'role': 'user', 'content': context_data},
                    ],
                    feature='attendance_coach',
                )
                emp.write({'x_fclk_ai_coach_tip': tip})
            except Exception as e:
                _logger.warning("Failed to generate coach tip for %s: %s", emp.name, e)
