# Copyright 2025 BMO Soluciones, S.A.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Configuración de parámetros de cálculo."""

from __future__ import annotations

from flask import Blueprint, flash, redirect, render_template, url_for

from coati_payroll.forms import ConfiguracionCalculosForm
from coati_payroll.i18n import _
from coati_payroll.model import ConfiguracionCalculos, db
from coati_payroll.rbac import require_write_access


config_calculos_bp = Blueprint("config_calculos", __name__, url_prefix="/config-calculos")


def _get_or_create_global_config() -> ConfiguracionCalculos:
    """Get or create the global (empresa=None, pais=None) calculation config."""
    config = db.session.execute(
        db.select(ConfiguracionCalculos).filter(
            ConfiguracionCalculos.empresa_id.is_(None),
            ConfiguracionCalculos.pais_id.is_(None),
            ConfiguracionCalculos.activo.is_(True),
        )
    ).scalar_one_or_none()

    if config:
        return config

    config = ConfiguracionCalculos(
        empresa_id=None,
        pais_id=None,
        activo=True,
    )
    db.session.add(config)
    db.session.commit()
    return config


@config_calculos_bp.route("/", methods=["GET", "POST"])
@require_write_access()
def index():
    """Edit global calculation parameters."""
    config = _get_or_create_global_config()
    form = ConfiguracionCalculosForm(obj=config)

    if form.validate_on_submit():
        config.liquidacion_modo_dias = form.liquidacion_modo_dias.data
        config.liquidacion_factor_calendario = form.liquidacion_factor_calendario.data
        config.liquidacion_factor_laboral = form.liquidacion_factor_laboral.data
        db.session.commit()
        flash(_("Configuración de cálculos actualizada."), "success")
        return redirect(url_for("config_calculos.index"))

    return render_template(
        "modules/config_calculos/index.html",
        form=form,
        config=config,
    )
