<?php

use App\Components\Account\AccountProfile;
use App\Components\Account\AccountSecurity;
use App\Components\Account\AccountSessions;
use App\Components\Admin\Permission\PermissionCreate;
use App\Components\Admin\Permission\PermissionEdit;
use App\Components\Admin\Permission\PermissionIndex;
use App\Components\Admin\Role\RoleCreate;
use App\Components\Admin\Role\RoleEdit;
use App\Components\Admin\Role\RoleIndex;
use App\Components\Admin\Role\RoleShow;
use App\Components\Admin\Users\UserCreate;
use App\Components\Admin\Users\UserEdit;
use App\Components\Admin\Users\UserIndex;
use App\Components\Claim\CreateClaim;
use App\Components\Claim\StatusController;
use App\Components\Client\Create;
use App\Components\Client\Edit;
use App\Components\Client\Index;
use App\Components\Client\View;
use App\Http\Controllers\EsignWebhookController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get("/", function ()
{
    return redirect('login');
});

// Workspace selection — requires auth, no solicitor context needed
Route::middleware(['auth'])->group(function () {
    Route::get('/workspace', \App\Components\Platform\WorkspaceSelector::class)->name('workspace.select');
    Route::get('/workspace/switch', function () {
        session()->forget('workspace_solicitor_id');
        return redirect()->route('workspace.select');
    })->name('workspace.switch');
});

Route::prefix('account')
    ->name('account.')
    ->group(function ()
    {
        Route::get('/profile', AccountProfile::class)->lazy()->name('profile');
        Route::get('/security', AccountSecurity::class)->lazy()->name('security');
        Route::get('/sessions', AccountSessions::class)->lazy()->name('sessions');
    });

Route::middleware(['auth', 'setSolicitor', 'ensureSolicitor', 'passSolicitorToView'])->group(function ()
{
    Route::get('/a', function ()
    {
        return view('index');
    })->name('dashboard');

    Route::get('/clients', Index::class)->lazy()->name('clients.index');
    Route::get('/clients/create', Create::class)->lazy()->name('clients.create');
    Route::get('/clients/{client}/edit', Edit::class)->lazy()->name('clients.edit');
    Route::get('/clients/{client}', View::class)->lazy()->name('clients.show');

    Route::get('/clients/{client}/create', CreateClaim::class)->name('claims.create');
    Route::get('/clients/{client}/claim', \App\Components\Claim\Edit::class)->name('claims.edit');

    Route::get('/sources', \App\Components\LeadSource\Index::class)->lazy()->name('sources.index');
    Route::get('/sources/create', \App\Components\LeadSource\Create::class)->lazy()->name('sources.create');
    Route::get('/sources/{leadSource}/edit', \App\Components\LeadSource\Edit::class)->lazy()->name('sources.edit');
    Route::get('/sources/{leadSource}', \App\Components\LeadSource\View::class)->lazy()->name('sources.show');

    Route::get('/institutions', \App\Components\Institution\Index::class)->lazy()->name('institutions.index');
    Route::get('/institutions/create', \App\Components\Institution\Create::class)->lazy()->name('institutions.create');
    Route::get('/institutions/{institution}/edit', \App\Components\Institution\Edit::class)->lazy()->name('institutions.edit');
    Route::get('/institutions/{institution}', \App\Components\Institution\View::class)->lazy()->name('institutions.show');


    Route::get('/dsars', \App\Components\Dsar\Outstanding::class)->lazy()->name('dsars.outstanding');
    Route::get('/dsars/report', \App\Components\Report\DsarReport::class)->lazy()->name('dsars.report');
    Route::get('/dsars/tranches/{tranche}', App\Components\Dsar\Tranche\View::class)->name('dsars.tranches.view');
    Route::get('/dsars/types', \App\Components\Dsar\TypeManager::class)->lazy()->name('dsars.types');
    Route::get('/dsars/{dsar}', \App\Components\Dsar\View::class)->name('dsars.view');

    Route::get('/reports/tranche', App\Components\Report\Tranche\Index::class)->name('reports.tranche.index');
    Route::get('/reports/tranche/{tranche}', App\Components\Report\Tranche\View::class)->name('reports.tranche.view');
    Route::get('/reports/status', \App\Components\Report\StatusReport::class)->name('reports.status');
    Route::get('/reports/status/days', \App\Components\Report\DaysInStatusReport::class)->name('reports.status.days');

    Route::get('/tranches', App\Components\Tranche\Index::class)->name('tranches.index');

    Route::get('/statuses', StatusController::class)->lazy()->name('statuses.index');
    Route::get('/doc-types', \App\Components\DocumentTypeManager::class)->lazy()->name('doc-types.index');
    Route::get('/tags', \App\Components\Tag\TagManager::class)->lazy()->name('tags.index');
    Route::get('/key-dates', \App\Components\KeyDateManager::class)->lazy()->name('key-dates.index');

    Route::get('/task-hub', \App\Components\Task\TaskHub::class)->name('task-hub.index');

    Route::get('/print-queue', \App\Components\PrintQueueManager::class)->lazy()->name('print-queue.index');
    Route::get('/institution-types', \App\Components\InstitutionTypeManager::class)->lazy()->name('institution-types.index');
    Route::get('/institution-record-types', \App\Components\InstitutionRecordTypeManager::class)->lazy()->name('institution-record-types.index');

    Route::prefix('templates')->group(function ()
    {
        Route::get('/', \App\Components\Template\Index::class)->name('templates.index');
        Route::get('/create', \App\Components\Template\Editor::class)->name('templates.create');
        Route::get('/{template}', \App\Components\Template\Editor::class)->name('templates.edit');
    });

    Route::prefix('solicitors')->group(function () {
        Route::get('/', \App\Components\Solicitor\Index::class)->name('solicitors.index');
        Route::get('/create', \App\Components\Solicitor\Create::class)->name('solicitors.create');
        Route::get('/{solicitor}', \App\Components\Solicitor\View::class)->name('solicitors.show');
        Route::get('/{solicitor}/edit', \App\Components\Solicitor\Edit::class)->name('solicitors.edit');
    });

    Route::get('/q')->name('settings.tools.index');
    Route::get('/w')->name('reports.status.claims');
});

Route::middleware(['web', 'auth', 'platform'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function ()
    {

        /*
        |--------------------------------------------------------------------------
        | USERS
        |--------------------------------------------------------------------------
        */
        Route::prefix('users')->name('users.')->group(function ()
        {

            Route::get('/', UserIndex::class)->lazy()->name('index');
            Route::get('/create', UserCreate::class)->lazy()->name('create');
            Route::get('/{user}', \App\Components\Admin\Users\View::class)->name('show');
            Route::get('/{user}/edit', UserEdit::class)->lazy()->name('edit');
        });

        Route::post('/admin/users/stop-impersonation', function ()
        {
            if (!session()->has('impersonator_id')) {
                return redirect('/');
            }

            Auth::loginUsingId(session()->pull('impersonator_id'));

            return redirect()->route('admin.users.index');
        })->name('admin.users.stop-impersonation');


        /*
        |--------------------------------------------------------------------------
        | ROLES
        |--------------------------------------------------------------------------
        */
        Route::prefix('roles')->name('roles.')->group(function ()
        {
            Route::get('/', RoleIndex::class)->lazy()->name('index');
            Route::get('/create', RoleCreate::class)->lazy()->name('create');
            Route::get('/{role}', RoleShow::class)->lazy()->name('show');
            Route::get('/{role}/edit', RoleEdit::class)->lazy()->name('edit');
        });

        /*
        |--------------------------------------------------------------------------
        | PERMISSIONS
        |--------------------------------------------------------------------------
        */
        Route::prefix('permissions')->name('permissions.')->group(function ()
        {
            Route::get('/', PermissionIndex::class)->lazy()->name('index');
            Route::get('/create', PermissionCreate::class)->lazy()->name('create');
            Route::get('/{permission}/edit', PermissionEdit::class)->lazy()->name('edit');
        });
    });

Route::post('/webhook/esign', [EsignWebhookController::class, 'input'])->name('webhook.esign');
