#!/usr/bin/python
#
# Copyright (c) 2025, Jeffrey van Pelt (@Thulium-Drake) <jeff@vanpelt.one>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-FileCopyrightText: (c) 2025, Jeffrey van Pelt (Thulium-Drake) <jeff@vanpelt.one>
# SPDX-License-Identifier: GPL-3.0-or-later


DOCUMENTATION = r"""
module: proxmox_group
short_description: Group management for Proxmox VE cluster
description:
  - Create or delete a user group for Proxmox VE clusters.
author: "Jeffrey van Pelt (@Thulium-Drake) <jeff@vanpelt.one>"
version_added: "1.2.0"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  groupid:
    description:
      - The group name.
    type: str
    aliases: ["name"]
    required: true
  state:
    description:
      - Indicate desired state of the group.
    choices: ['present', 'absent']
    default: present
    type: str
  comment:
    description:
      - Specify the description for the group.
      - Parameter is ignored when group already exists or O(state=absent).
    type: str

extends_documentation_fragment:
  - community.proxmox.proxmox.actiongroup_proxmox
  - community.proxmox.proxmox.documentation
  - community.proxmox.attributes
"""

EXAMPLES = r"""
- name: Create new Proxmox VE user group
  community.proxmox.proxmox_group:
    name: administrators
    comment: IT Admins

- name: Delete a Proxmox VE user group
  community.proxmox.proxmox_group:
    name: administrators
    state: absent
"""

RETURN = r"""
groupid:
  description: The group name.
  returned: success
  type: str
  sample: test
msg:
  description: A short message on what the module did.
  returned: always
  type: str
  sample: "Group administrators successfully created"
"""

from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
    ProxmoxAnsible,
    create_proxmox_module,
)


def module_args():
    return dict(
        groupid=dict(type="str", aliases=["name"], required=True),
        comment=dict(type="str"),
        state=dict(default="present", choices=["present", "absent"]),
    )


def module_options():
    return {}


class ProxmoxGroupAnsible(ProxmoxAnsible):
    def is_group_existing(self, groupid):
        """Check whether group already exist

        :param groupid: str - name of the group
        :return: bool - is group exists?
        """
        try:
            groups = self.proxmox_api.access.groups.get()
            return any(group["groupid"] == groupid for group in groups)
        except Exception as e:
            self.module.fail_json(msg=f"Unable to retrieve groups: {e}")

    def create_group(self, groupid, comment=None):
        """Create Proxmox VE group

        :param groupid: str - name of the group
        :param comment: str, optional - Description of a group
        :return: None
        """
        if self.is_group_existing(groupid):
            self.module.exit_json(changed=False, groupid=groupid, msg=f"Group {groupid} already exists")

        if self.module.check_mode:
            return

        try:
            self.proxmox_api.access.groups.post(groupid=groupid, comment=comment)
        except Exception as e:
            self.module.fail_json(msg=f"Failed to create group with ID {groupid}: {e}")

    def delete_group(self, groupid):
        """Delete Proxmox VE group

        :param groupid: str - name of the group
        :return: None
        """
        if not self.is_group_existing(groupid):
            self.module.exit_json(changed=False, groupid=groupid, msg=f"Group {groupid} doesn't exist")

        if self.module.check_mode:
            return

        try:
            self.proxmox_api.access.groups(groupid).delete()
        except Exception as e:
            self.module.fail_json(msg=f"Failed to delete group with ID {groupid}: {e}")


def main():
    module = create_proxmox_module(module_args(), **module_options())
    proxmox = ProxmoxGroupAnsible(module)

    groupid = module.params["groupid"]
    comment = module.params["comment"]
    state = module.params["state"]

    if state == "present":
        proxmox.create_group(groupid, comment)
        module.exit_json(changed=True, groupid=groupid, msg=f"Group {groupid} successfully created")
    else:
        proxmox.delete_group(groupid)
        module.exit_json(changed=True, groupid=groupid, msg=f"Group {groupid} successfully deleted")


if __name__ == "__main__":
    main()
