package Daje::Workflow::Activities::Authorities::Standard::AdminRole;
use Mojo::Base 'Daje::Workflow::Activities::Authorities::Standard::Base', -base;
use v5.42;

# NAME
# ====
#
# Daje::Workflow::Activities::Authorities::Standard::AdminRole - Daje helper,
# creates a new admin role and attach it to user and company
#
# SYNOPSIS
# ========
#
#
# DESCRIPTION
# ===========
#
# Daje::Workflow::Activities::Authorities::Standard::AdminRole a helper clas
#
# METHODS
# =======
#
#
#
#
# SEE ALSO
# ========
#
# Mojolicious, Mojolicious::Guides, https://mojolicious.org.
#
# LICENSE
# =======
#
# Copyright (C) janeskil1525.
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# AUTHOR
# ======
#
# janeskil1525 E<lt>janeskil1525@gmail.com
#


use Daje::Database::View::vAuthoritiesPluginList;
use Daje::Database::View::vAuthoritiesFunction;
use Daje::Database::View::vAuthoritiesPermissionsList;
use Daje::Database::Model::UsersUsers;

sub create_admin($self) {
    $self->model->insert_history(
        "Admin role for new client",
        "Daje::Workflow::Activities::Authorities::Standard::AdminRole::create_admin",
        1
    );

    my $authorities_role_pkey = $self->createRole('Administrator', 1);

    my $plugins = Daje::Database::View::vAuthoritiesPluginList->new(
        db => $self->db
    )->load_all_authorities_plugin()->{data};

    my $permissions = Daje::Database::View::vAuthoritiesPermissionsList->new(
        db => $self->db
    )->load_all_authorities_permissions()->{data};

    my $length = scalar @{$plugins};
    for(my $i = 0; $i < $length; $i++) {
        my $authorities_plugin_role_pkey = $self->create_role_plugin(
            $authorities_role_pkey,
            @{$plugins}[$i]->{authorities_plugin_pkey},
            "Autogenerated"
        );
        my $functions = Daje::Database::View::vAuthoritiesFunction->new(
            db => $self->db
        )->load_authorities_plugin_fkey(
            @{$plugins}[$i]->{authorities_plugin_pkey}
        )->{data};

        my $len = scalar @{$functions};
        for(my $j = 0; $j < $len; $j++) {
            my $authorities_function_plugin_role_pkey = $self->create_function_plugin_role(
                $authorities_role_pkey,
                @{$functions}[$j]->{authorities_function_pkey},
                @{$plugins}[$i]->{authorities_plugin_pkey},
                "Autogenerated"
            );

            my $le = scalar @{$permissions};
            for(my $k = 0; $k < $le; $k++) {
                $self->create_role_permissions(
                    @{$permissions}[$k]->{authorities_permissions_pkey},
                    $authorities_role_pkey,
                    @{$functions}[$j]->{authorities_function_pkey},
                    @{$plugins}[$i]->{authorities_plugin_pkey}
                );
            }
        }
    }
    Daje::Database::Model::UsersUsers->new(db => $self->db)->update({
        users_users_pkey       => $self->context->{context}->{users_fkey},
        authorities_role_fkey => $authorities_role_pkey,
    });
}
1;