# authentic2 - versatile identity manager
# Copyright (C) 2010-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from django.utils.functional import lazy
from django.views.i18n import JavaScriptCatalog

from authentic2.utils import misc as utils_misc

from ..decorators import required
from . import journal_views, ou_views, role_views, service_views, user_views, views


def manager_login_required(func):
    return login_required(func, login_url=lazy(utils_misc.get_manager_login_url, str)())


urlpatterns = required(
    manager_login_required,
    [
        # homepage
        url(r'^$', views.homepage, name='a2-manager-homepage'),
        url(r'^me/$', user_views.me, name='a2-manager-me'),
        # Authentic2 users
        url(r'^users/$', user_views.users, name='a2-manager-users'),
        url(r'^users/export/(?P<format>csv)/$', user_views.users_export, name='a2-manager-users-export'),
        url(
            r'^users/export/(?P<uuid>[a-z0-9-]+)/progress/$',
            user_views.users_export_progress,
            name='a2-manager-users-export-progress',
        ),
        url(
            r'^users/export/(?P<uuid>[a-z0-9-]+)/$',
            user_views.users_export_file,
            name='a2-manager-users-export-file',
        ),
        url(r'^users/add/$', user_views.user_add_default_ou, name='a2-manager-user-add-default-ou'),
        url(r'^users/add/choose-ou/$', user_views.user_add_choose_ou, name='a2-manager-user-add-choose-ou'),
        url(r'^users/import/$', user_views.user_imports, name='a2-manager-users-imports'),
        url(
            r'^users/import/(?P<uuid>[a-z0-9]+)/download/(?P<filename>.*)$',
            user_views.user_import,
            name='a2-manager-users-import-download',
        ),
        url(r'^users/import/(?P<uuid>[a-z0-9]+)/$', user_views.user_import, name='a2-manager-users-import'),
        url(
            r'^users/import/(?P<import_uuid>[a-z0-9]+)/(?P<report_uuid>[a-z0-9]+)/$',
            user_views.user_import_report,
            name='a2-manager-users-import-report',
        ),
        url(r'^users/(?P<ou_pk>\d+)/add/$', user_views.user_add, name='a2-manager-user-add'),
        url(r'^users/(?P<pk>\d+)/$', user_views.user_detail, name='a2-manager-user-detail'),
        url(r'^users/(?P<pk>\d+)/edit/$', user_views.user_edit, name='a2-manager-user-edit'),
        url(r'^users/(?P<pk>\d+)/delete/$', user_views.user_delete, name='a2-manager-user-delete'),
        url(r'^users/(?P<pk>\d+)/roles/$', user_views.roles, name='a2-manager-user-roles'),
        url(
            r'^users/(?P<pk>\d+)/change-password/$',
            user_views.user_change_password,
            name='a2-manager-user-change-password',
        ),
        url(
            r'^users/(?P<pk>\d+)/change-email/$',
            user_views.user_change_email,
            name='a2-manager-user-change-email',
        ),
        url(r'^users/(?P<pk>\d+)/su/$', user_views.su, name='a2-manager-user-su'),
        url(
            r'^users/(?P<pk>\d+)/authorizations/$',
            user_views.user_authorizations,
            name='a2-manager-user-authorizations',
        ),
        url(r'^users/(?P<pk>\d+)/journal/$', user_views.user_journal, name='a2-manager-user-journal'),
        # by uuid
        url(
            r'^users/uuid:(?P<slug>[a-z0-9]+)/$',
            user_views.user_detail,
            name='a2-manager-user-by-uuid-detail',
        ),
        url(
            r'^users/uuid:(?P<slug>[a-z0-9]+)/edit/$',
            user_views.user_edit,
            name='a2-manager-user-by-uuid-edit',
        ),
        url(
            r'^users/uuid:(?P<slug>[a-z0-9]+)/roles/$', user_views.roles, name='a2-manager-user-by-uuid-roles'
        ),
        url(
            r'^users/uuid:(?P<slug>[a-z0-9]+)/change-password/$',
            user_views.user_change_password,
            name='a2-manager-user-by-uuid-change-password',
        ),
        url(
            r'^users/uuid:(?P<slug>[a-z0-9]+)/change-email/$',
            user_views.user_change_email,
            name='a2-manager-user-by-uuid-change-email',
        ),
        url(
            r'^users/uuid:(?P<slug>[a-z0-9]+)/journal/$',
            user_views.user_journal,
            name='a2-manager-user-journal',
        ),
        # Authentic2 roles
        url(r'^roles/$', role_views.listing, name='a2-manager-roles'),
        url(r'^roles/import/$', role_views.roles_import, name='a2-manager-roles-import'),
        url(r'^roles/csv-import/$', role_views.roles_csv_import, name='a2-manager-roles-csv-import'),
        url(
            r'^roles/csv-import-sample/$',
            role_views.roles_csv_import_sample,
            name='a2-manager-roles-csv-import-sample',
        ),
        url(r'^roles/add/$', role_views.add, name='a2-manager-role-add'),
        url(r'^roles/export/(?P<format>csv|json)/$', role_views.export, name='a2-manager-roles-export'),
        url(r'^roles/journal/$', role_views.roles_journal, name='a2-manager-roles-journal'),
        url(r'^roles/(?P<pk>\d+)/$', role_views.members, name='a2-manager-role-members'),
        url(r'^roles/(?P<pk>\d+)/add-child/$', role_views.add_child, name='a2-manager-role-add-child'),
        url(r'^roles/(?P<pk>\d+)/add-parent/$', role_views.add_parent, name='a2-manager-role-add-parent'),
        url(
            r'^roles/(?P<pk>\d+)/remove-child/(?P<child_pk>\d+)/$',
            role_views.remove_child,
            name='a2-manager-role-remove-child',
        ),
        url(
            r'^roles/(?P<pk>\d+)/remove-parent/(?P<parent_pk>\d+)/$',
            role_views.remove_parent,
            name='a2-manager-role-remove-parent',
        ),
        url(
            r'^roles/(?P<pk>\d+)/add-admin-user/$',
            role_views.add_admin_user,
            name='a2-manager-role-add-admin-user',
        ),
        url(
            r'^roles/(?P<pk>\d+)/remove-admin-user/(?P<user_pk>\d+)/$',
            role_views.remove_admin_user,
            name='a2-manager-role-remove-admin-user',
        ),
        url(
            r'^roles/(?P<pk>\d+)/add-admin-role/$',
            role_views.add_admin_role,
            name='a2-manager-role-add-admin-role',
        ),
        url(
            r'^roles/(?P<pk>\d+)/remove-admin-role/(?P<role_pk>\d+)/$',
            role_views.remove_admin_role,
            name='a2-manager-role-remove-admin-role',
        ),
        url(
            r'^roles/(?P<pk>\d+)/export/(?P<format>csv)/$',
            role_views.members_export,
            name='a2-manager-role-members-export',
        ),
        url(r'^roles/(?P<pk>\d+)/delete/$', role_views.delete, name='a2-manager-role-delete'),
        url(r'^roles/(?P<pk>\d+)/edit/$', role_views.edit, name='a2-manager-role-edit'),
        url(r'^roles/(?P<pk>\d+)/permissions/$', role_views.permissions, name='a2-manager-role-permissions'),
        url(r'^roles/(?P<pk>\d+)/journal/$', role_views.journal, name='a2-manager-role-journal'),
        # Authentic2 organizational units
        url(r'^organizational-units/$', ou_views.listing, name='a2-manager-ous'),
        url(r'^organizational-units/add/$', ou_views.add, name='a2-manager-ou-add'),
        url(r'^organizational-units/(?P<pk>\d+)/$', ou_views.detail, name='a2-manager-ou-detail'),
        url(r'^organizational-units/(?P<pk>\d+)/edit/$', ou_views.edit, name='a2-manager-ou-edit'),
        url(r'^organizational-units/(?P<pk>\d+)/delete/$', ou_views.delete, name='a2-manager-ou-delete'),
        url(r'^organizational-units/export/(?P<format>json)/$', ou_views.export, name='a2-manager-ou-export'),
        url(r'^organizational-units/import/$', ou_views.ous_import, name='a2-manager-ous-import'),
        # Services
        url(r'^services/$', service_views.listing, name='a2-manager-services'),
        url(r'^services/(?P<service_pk>\d+)/$', service_views.roles, name='a2-manager-service'),
        url(r'^services/(?P<service_pk>\d+)/edit/$', service_views.edit, name='a2-manager-service-edit'),
        # Journal
        url(r'^journal/$', journal_views.journal, name='a2-manager-journal'),
        # backoffice menu as json
        url(r'^menu.json$', views.menu_json),
        # general management
        url(r'^site-export/$', views.site_export, name='a2-manager-site-export'),
        url(r'^site-import/$', views.site_import, name='a2-manager-site-import'),
    ],
)

urlpatterns += [
    url(
        r'^jsi18n/$',
        JavaScriptCatalog.as_view(packages=['authentic2.manager']),
        name='a2-manager-javascript-catalog',
    ),
    url(r'^select2.json$', views.select2, name='django_select2-json'),
]
