<?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\ClientIndex;
use App\Components\Client\ClientProfile;
use App\Components\Client\Create;
use App\Components\PensionCase\Create as PensionCaseCreate;
use App\Components\PensionCase\Edit as PensionCaseEdit;
use App\Components\PensionCase\Index as PensionCaseIndex;
use App\Components\PensionCase\StatusController as PensionStatusController;
use App\Components\PensionCase\View as PensionCaseView;
use App\Components\WillsCase\Create as WillsCaseCreate;
use App\Components\WillsCase\Edit as WillsCaseEdit;
use App\Components\WillsCase\Index as WillsCaseIndex;
use App\Components\WillsCase\View as WillsCaseView;
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');
});

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'])->group(function () {

    Route::get('/a', fn() => view('index'))->name('dashboard');
    Route::get('/task-hub', \App\Components\Task\TaskHub::class)->name('task-hub.index');
    Route::get('/monitoring', \App\Components\Monitoring\Index::class)->name('monitoring.index');

    // Cross-workspace client profile (shows HA + Pension + Wills claims, notes, docs, address)
    // Client CRUD — no workspace restriction (shared across all workspaces)
    Route::get('/clients', ClientIndex::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}', ClientProfile::class)->name('clients.show');

    // Workspace switcher — sets the session only if the user is whitelisted
    Route::get('/workspace/{workspace}', function (string $workspace) {
        if (in_array($workspace, ['ha', 'pension', 'wills'])
            && auth()->user()->canAccessWorkspace($workspace)
        ) {
            session(['workspace' => $workspace]);
        }
        return redirect()->back();
    })->name('workspace.switch');

    // Lead Sources
    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');


    /*
    |--------------------------------------------------------------------------
    | HA WORKSPACE
    |--------------------------------------------------------------------------
    */

    Route::middleware(['workspace:ha'])->group(function () {

        // HA Claims
        Route::prefix('ha-claims')->name('ha-claims.')->group(function () {
            Route::get('/', Index::class)->lazy()->name('index');
            Route::get('/{client}', View::class)->lazy()->name('show');
            Route::get('/{client}/create', CreateClaim::class)->name('create');
            Route::get('/{client}/edit', \App\Components\Claim\Edit::class)->name('edit');
        });
        
        // Institutions
        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');

        // DSARs
        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');

        // Reports
        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');

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

        // HA Settings
        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('/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('settings')->name('settings.')->group(function () {
            Route::get('/', \App\Components\Settings\Index::class)->name('index');
            Route::get('/email-senders', \App\Components\Settings\EmailSenders::class)->name('email-senders');
            Route::get('/phone-numbers', \App\Components\Settings\PhoneNumbers::class)->name('phone-numbers');
        });

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

    /*
    |--------------------------------------------------------------------------
    | PENSION WORKSPACE
    |--------------------------------------------------------------------------
    */
    Route::middleware(['workspace:pension'])->group(function () {

        Route::get('/pension-cases', PensionCaseIndex::class)->lazy()->name('pension-cases.index');
        Route::get('/pension-cases/create', PensionCaseCreate::class)->lazy()->name('pension-cases.create');
        Route::get('/pension-cases/{pensionCase}', PensionCaseView::class)->name('pension-cases.view');
        Route::get('/pension-cases/{pensionCase}/edit', PensionCaseEdit::class)->lazy()->name('pension-cases.edit');

        // Pension Settings
        Route::get('/pension-statuses', PensionStatusController::class)->lazy()->name('pension-statuses.index');

        // Client-scoped pension case creation (requires HA client context — only reachable from HA workspace links)
        Route::get('/clients/{client}/pension-cases/create', PensionCaseCreate::class)->lazy()->name('clients.pension-cases.create');
    });

    /*
    |--------------------------------------------------------------------------
    | WILLS & TRUSTS WORKSPACE
    |--------------------------------------------------------------------------
    */
    Route::middleware(['workspace:wills'])->group(function () {

        Route::get('/wills-cases', WillsCaseIndex::class)->lazy()->name('wills-cases.index');
        Route::get('/wills-cases/create', WillsCaseCreate::class)->lazy()->name('wills-cases.create');
        Route::get('/wills-cases/{willsCase}', WillsCaseView::class)->name('wills-cases.view');
        Route::get('/wills-cases/{willsCase}/edit', WillsCaseEdit::class)->lazy()->name('wills-cases.edit');
    });
});

/*
|--------------------------------------------------------------------------
| ADMIN (platform users only — no workspace restriction)
|--------------------------------------------------------------------------
*/
Route::middleware(['web', 'auth', 'platform'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        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');

        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');
        });

        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');
