#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
#
# UCS@school Diagnosis Module
#
# SPDX-FileCopyrightText: 2020-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only
"""This module checks the consistency of USC@school users, shares, containers and groups"""

import argparse

from ucsschool.lib import consistency
from ucsschool.lib.models.school import School
from univention.admin.uldap import getMachineConnection


def print_problematic_objects(objects, entry_string):
    if objects:
        details = "\n\n" + entry_string
        for dn, problems in objects.items():
            details += "\n\n  {}".format(dn)
            for problem in problems:
                details += "\n  - {}".format(problem)
        print(details)


def main():
    parser = argparse.ArgumentParser(
        description="UCS@school Object Consistency Check", formatter_class=argparse.RawTextHelpFormatter
    )
    exclusive_group = parser.add_mutually_exclusive_group()
    exclusive_group.add_argument(
        "--school",
        help="When specified, only objects of this school get checked.\n"
        "Give the desired school abbreviation.",
    )
    exclusive_group.add_argument(
        "--user_dn",
        help="When specified, only the given user(s) gets checked.\n"
        "Give the desired DN(s) of the user(s) seperated by spaces.",
        nargs="+",
    )
    args = parser.parse_args()

    user_dn = school = None
    if args.user_dn:
        user_dn = args.user_dn
    if args.school:
        school = args.school

    connection = getMachineConnection()[0]
    if not School(name=school).exists(connection) and school:
        print("The given school with abbreviation '{}' does not exist.".format(school))
        return

    user_check = consistency.UserCheck()
    users_from_ldap = user_check.get_users_from_ldap(school, user_dn)
    user_problematic_objects = {}
    for dn, attrs in users_from_ldap:
        user_issues = user_check.check_user(dn, attrs)
        if user_issues:
            user_problematic_objects[dn] = user_issues

    group_problematic_objects = {}
    container_problematic_objects = {}
    share_problematic_objects = {}
    server_group_problematic_objects = {}
    if not user_dn:
        group_problematic_objects = consistency.check_mandatory_groups_exist(school)
        container_problematic_objects = consistency.check_containers(school)
        share_problematic_objects = consistency.check_shares(school)
        server_group_problematic_objects = consistency.check_server_group_membership(school)

    print_problematic_objects(user_problematic_objects, "The following USER PROBLEMS were found:")
    print_problematic_objects(group_problematic_objects, "The following GROUP PROBLEMS were found:")
    print_problematic_objects(
        container_problematic_objects, "The following CONTAINER PROBLEMS were found:"
    )
    print_problematic_objects(share_problematic_objects, "The following SHARE PROBLEMS were found:")
    print_problematic_objects(
        server_group_problematic_objects, "The following SERVER GROUP PROBLEMS were found:"
    )


if __name__ == "__main__":
    main()
