# Copyright (C) 2024 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, fields, models
from odoo.exceptions import AccessError, ValidationError


class CxTowerJetState(models.Model):
    """Jet States represent the different states a jet can be in during its lifecycle"""

    _name = "cx.tower.jet.state"
    _description = "Cetmix Tower Jet State"
    _inherit = ["cx.tower.reference.mixin", "cx.tower.access.mixin"]
    _order = "sequence, id"

    sequence = fields.Integer(default=10, required=True)
    active = fields.Boolean(default=True)
    color = fields.Integer()
    note = fields.Text()

    # Set default access level to User
    access_level = fields.Selection(default="1")

    def unlink(self):
        """
        Do not allow to unlink a state
        if it is used in any action
        """
        actions = self.env["cx.tower.jet.action"].search(
            [
                "|",
                "|",
                ("state_from_id", "in", self.ids),
                ("state_to_id", "in", self.ids),
                ("state_transit_id", "in", self.ids),
            ]
        )
        if actions:
            raise ValidationError(
                _(
                    "Some states are still used in the following actions: %(actions)s"
                    "\nJet templates: %(templates)s",
                    actions=", ".join(set(actions.mapped("name"))),
                    templates=", ".join(set(actions.mapped("jet_template_id.name"))),
                )
            )
        return super().unlink()

    def set_state(self, jet=None):
        """Sets the state of the jet

        Args:
            jet (cx.tower.jet): Jet to set the state.
        """
        self.ensure_one()

        # Try to obtain jet from context if not provided as an argument
        if jet is None:
            jet_id = self.env.context.get("jet_id")

            # Just return, no exceptions for now
            if not jet_id:
                return

            jet = self.env["cx.tower.jet"].browse(jet_id)

        # Ensure that the state is set for a single jet
        if not jet or len(jet) > 1:
            raise ValidationError(_("State can be set only for a single jet"))

        # Check access to the jet
        jet.check_access_rights("read")
        jet.check_access_rule("write")

        # Get user access level
        user_access_level = self.env.user._cetmix_tower_access_level()

        # If user is manager but is not added as a manager to the jet,
        # his access level is considered as user.
        # NB: record access is already checked above.
        if user_access_level == "2" and self.env.user not in jet.manager_ids:
            user_access_level = "1"

        # Check if user access level is equal or greater
        if self.access_level > user_access_level:
            raise AccessError(
                _("You are not allowed to set the '%(state)s' state!", state=self.name)
            )

        # Bring the jet to the state
        jet._bring_to_state(self)
