<?php

namespace App\Modules\Settings\Rbac\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\Settings\Rbac\Requests\InviteTenantUserRequest;
use App\Modules\Settings\Rbac\Requests\StoreTenantUserRequest;
use App\Modules\Settings\Rbac\Services\TenantRoleManagementService;
use App\Modules\Settings\Rbac\Services\TenantUserCreationService;
use App\Modules\Settings\Rbac\Services\TenantUserDirectoryService;
use App\Modules\Settings\Rbac\Services\TenantUserInviteService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class TenantUsersPageController extends Controller
{
    public function __construct(
        private readonly TenantUserDirectoryService $directory,
        private readonly TenantUserInviteService $invites,
        private readonly TenantUserCreationService $userCreation,
        private readonly TenantRoleManagementService $roles,
    ) {}

    public function index(): View
    {
        $tenant = $this->tenant();

        return view('pages.settings.users.index', [
            'users' => $this->directory->paginateForTenant($tenant),
        ]);
    }

    public function create(): View
    {
        $tenant = $this->tenant();

        return view('pages.settings.users.create', [
            'roles' => $this->roles->listAssignable($tenant),
            'employeeOptions' => $this->directory->employeeOptionsForTenant($tenant),
        ]);
    }

    public function store(StoreTenantUserRequest $request): RedirectResponse
    {
        $tenant = $this->tenant();
        $this->userCreation->create(
            $tenant,
            (string) $request->validated('name'),
            (string) $request->validated('email'),
            (string) $request->validated('password'),
            array_map('intval', $request->validated('role_ids')),
            $request->filled('employee_id') ? (int) $request->validated('employee_id') : null,
        );

        return redirect()
            ->route('settings.users.index')
            ->with('status', __('rbac.flash.created'));
    }

    public function invite(): View
    {
        $tenant = $this->tenant();

        return view('pages.settings.users.invite', [
            'roles' => $this->roles->listAssignable($tenant),
            'employeeOptions' => $this->directory->employeeOptionsForTenant($tenant),
        ]);
    }

    public function storeInvite(InviteTenantUserRequest $request): RedirectResponse
    {
        $tenant = $this->tenant();
        $result = $this->invites->invite(
            $tenant,
            (string) $request->validated('name'),
            (string) $request->validated('email'),
            array_map('intval', $request->validated('role_ids')),
            $request->filled('employee_id') ? (int) $request->validated('employee_id') : null,
        );
        $url = route('settings.users.invite.accept', ['token' => $result['plain_token']]);

        return redirect()
            ->route('settings.users.index')
            ->with('status', __('rbac.flash.invited'))
            ->with('invite_link', $url);
    }

    private function tenant(): Tenant
    {
        $tenant = auth()->user()?->tenant;
        abort_if($tenant === null, 403);

        return $tenant;
    }
}
