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

import logging
from odoo import http, fields, _
from odoo.http import request

_logger = logging.getLogger(__name__)


class FusionClockKiosk(http.Controller):
    """Kiosk mode controller for shared-device clock-in/out."""

    @http.route('/fusion_clock/kiosk', type='http', auth='user', website=True)
    def kiosk_page(self, **kw):
        """Kiosk clock-in/out page for shared tablets."""
        user = request.env.user
        if not user.has_group('fusion_clock.group_fusion_clock_manager'):
            return request.redirect('/my')

        ICP = request.env['ir.config_parameter'].sudo()
        if ICP.get_param('fusion_clock.enable_kiosk', 'False') != 'True':
            return request.redirect('/my')

        values = {
            'pin_required': ICP.get_param('fusion_clock.kiosk_pin_required', 'True') == 'True',
            'page_name': 'kiosk',
        }
        return request.render('fusion_clock.kiosk_page', values)

    @http.route('/fusion_clock/kiosk/search', type='jsonrpc', auth='user', methods=['POST'])
    def kiosk_search(self, query='', **kw):
        """Search employees for kiosk identification."""
        user = request.env.user
        if not user.has_group('fusion_clock.group_fusion_clock_manager'):
            return {'error': 'Access denied.'}

        employees = request.env['hr.employee'].sudo().search([
            ('x_fclk_enable_clock', '=', True),
            ('name', 'ilike', query),
        ], limit=20)

        return {
            'employees': [{
                'id': emp.id,
                'name': emp.name,
                'department': emp.department_id.name or '',
                'is_checked_in': emp.attendance_state == 'checked_in',
            } for emp in employees],
        }

    @http.route('/fusion_clock/kiosk/verify_pin', type='jsonrpc', auth='user', methods=['POST'])
    def kiosk_verify_pin(self, employee_id=0, pin='', **kw):
        """Verify employee PIN for kiosk mode."""
        user = request.env.user
        if not user.has_group('fusion_clock.group_fusion_clock_manager'):
            return {'error': 'Access denied.'}

        employee = request.env['hr.employee'].sudo().browse(employee_id)
        if not employee.exists():
            return {'error': 'Employee not found.'}

        if employee.x_fclk_kiosk_pin and employee.x_fclk_kiosk_pin != pin:
            return {'error': 'Invalid PIN.'}

        return {
            'success': True,
            'employee_name': employee.name,
            'is_checked_in': employee.attendance_state == 'checked_in',
        }

    @http.route('/fusion_clock/kiosk/clock', type='jsonrpc', auth='user', methods=['POST'])
    def kiosk_clock(self, employee_id=0, latitude=0, longitude=0, **kw):
        """Perform clock action from kiosk on behalf of an employee."""
        user = request.env.user
        if not user.has_group('fusion_clock.group_fusion_clock_manager'):
            return {'error': 'Access denied.'}

        employee = request.env['hr.employee'].sudo().browse(employee_id)
        if not employee.exists() or not employee.x_fclk_enable_clock:
            return {'error': 'Employee not found or clock not enabled.'}

        from .clock_api import FusionClockAPI, haversine_distance
        api = FusionClockAPI()

        location, distance, err, method = api._verify_location(latitude, longitude, employee)
        if not location:
            return {
                'error': api._location_error_message(err, distance),
                'allowed': False,
            }

        is_checked_in = employee.attendance_state == 'checked_in'
        now = fields.Datetime.now()
        today = now.date()

        geo_info = {
            'latitude': latitude,
            'longitude': longitude,
            'browser': 'kiosk',
            'ip_address': request.httprequest.remote_addr or '',
        }

        try:
            attendance = employee.sudo()._attendance_action_change(geo_info)

            if not is_checked_in:
                attendance.sudo().write({
                    'x_fclk_location_id': location.id,
                    'x_fclk_in_distance': round(distance, 1),
                    'x_fclk_clock_source': 'kiosk',
                })

                api._log_activity(
                    employee, 'clock_in',
                    f"Kiosk clock-in at {location.name}",
                    attendance=attendance, location=location,
                    latitude=latitude, longitude=longitude, distance=distance,
                    source='kiosk',
                )

                scheduled_in, _ = api._get_scheduled_times(employee, today)
                api._check_and_create_penalty(employee, attendance, 'late_in', scheduled_in, now)

                return {
                    'success': True,
                    'action': 'clock_in',
                    'employee_name': employee.name,
                    'message': f'{employee.name} clocked in at {location.name}',
                }
            else:
                attendance.sudo().write({
                    'x_fclk_out_distance': round(distance, 1),
                })
                api._apply_break_deduction(attendance, employee)

                _, scheduled_out = api._get_scheduled_times(employee, today)
                api._check_and_create_penalty(employee, attendance, 'early_out', scheduled_out, now)

                api._log_activity(
                    employee, 'clock_out',
                    f"Kiosk clock-out from {location.name}. Net: {attendance.x_fclk_net_hours:.1f}h",
                    attendance=attendance, location=location,
                    latitude=latitude, longitude=longitude, distance=distance,
                    source='kiosk',
                )

                return {
                    'success': True,
                    'action': 'clock_out',
                    'employee_name': employee.name,
                    'message': f'{employee.name} clocked out from {location.name}',
                    'net_hours': round(attendance.x_fclk_net_hours or 0, 2),
                }

        except Exception as e:
            _logger.error("Fusion Clock kiosk error: %s", str(e))
            return {'error': str(e)}
