<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\Web\Admin\AdministradoresController;
use App\Http\Controllers\Web\Admin\BancadasController;
use App\Http\Controllers\Web\Admin\ComerciantesController;
use App\Http\Controllers\Web\Admin\ConfiguracoesController;
use App\Http\Controllers\Web\Admin\DesembarquesController;
use App\Http\Controllers\Web\Admin\EmbarquesController;
use App\Http\Controllers\Web\Admin\EscamadoresController;
use App\Http\Controllers\Web\Admin\FiscalController;
use App\Http\Controllers\Web\Admin\MercadosController;
use App\Http\Controllers\Web\Admin\OcorrenciasMercadoController;
use App\Http\Controllers\Web\Admin\OcorrenciasPescaController;
use App\Http\Controllers\Web\Admin\PagamentosController;
use App\Http\Controllers\Web\Admin\PescadoController;
use App\Http\Controllers\Web\Admin\PescadoresController;
use App\Http\Controllers\Web\Admin\RastreioController;
use App\Http\Controllers\Web\Admin\RelatoriosController;
use App\Http\Controllers\Web\Admin\TaxasController;
use App\Http\Controllers\Web\Util\LocationController;
use Illuminate\Support\Facades\Route;

// Página inicial - redireciona baseado na autenticação
Route::get('/', function () {
    return auth()->check() ? redirect()->route('dashboard') : redirect()->route('login');
})->name('home');

// Rota pública de login
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login'])->name('login.post');
Route::get('/password', [LoginController::class, 'showPasswordForm'])->name('password.request');
Route::post('/password', [LoginController::class, 'resetPassword'])->name('password.reset');
Route::post('/password', [LoginController::class, 'resetPassword'])->name('password.email');



// Rotas protegidas (Admin e Staff)
Route::middleware(['auth', 'user.type:admin,staff'])->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // =====================================================
    // ROTAS UTILITÁRIAS (UTIL)
    // =====================================================
    Route::prefix('util')->name('util.')->group(function () {

        // Rotas de Localização (AJAX)
        Route::get('provinces', [LocationController::class, 'getProvinces'])
            ->name('provinces');

        Route::get('municipalities/{provinceId}', [LocationController::class, 'getMunicipalities'])
            ->name('municipalities');

        Route::get('neighborhoods/{municipalityId}', [LocationController::class, 'getNeighborhoods'])
            ->name('neighborhoods');

        Route::get('location-tree', [LocationController::class, 'getLocationTree'])
            ->name('location-tree');

        // Rotas de Dados Dinâmicos (AJAX)
        Route::get('markets', [LocationController::class, 'getMarkets'])
            ->name('markets');

        Route::get('business-types', [LocationController::class, 'getBusinessTypes'])
            ->name('business-types');

        Route::get('merchants', [LocationController::class, 'getMerchants'])
            ->name('merchants');

        Route::get('fiscals', [LocationController::class, 'getFiscals'])
            ->name('fiscals');
    });

    Route::prefix('mercado')->name('mercado.')->group(function () {

        // =====================================================
        // MERCADOS (Gestão)
        // =====================================================
        Route::prefix('mercados')->name('mercados.')->group(function () {
            Route::get('table-data', [MercadosController::class, 'getTableData'])->name('table-data');
            Route::get('/', [MercadosController::class, 'index'])->name('index');
            Route::get('create', [MercadosController::class, 'create'])->name('create');
            Route::post('/', [MercadosController::class, 'store'])->name('store');
            Route::get('{mercado}', [MercadosController::class, 'show'])->name('show');
            Route::get('{mercado}/edit', [MercadosController::class, 'edit'])->name('edit');
            Route::put('{mercado}', [MercadosController::class, 'update'])->name('update');
            Route::delete('{mercado}', [MercadosController::class, 'destroy'])->name('destroy');
            Route::post('{mercado}/activate', [MercadosController::class, 'activate'])->name('activate');
            Route::post('{mercado}/inactivate', [MercadosController::class, 'inactivate'])->name('inactivate');
            Route::get('{mercado}/fiscais', [MercadosController::class, 'fiscais'])->name('fiscais');
            Route::post('{mercado}/assign-fiscal', [MercadosController::class, 'assignFiscal'])->name('assign-fiscal');
            Route::post('{mercado}/remove-fiscal', [MercadosController::class, 'removeFiscal'])->name('remove-fiscal');
            Route::get('{mercado}/statistics', [MercadosController::class, 'statistics'])->name('statistics');
            Route::get('{mercado}/bancadas', [MercadosController::class, 'bancadas'])->name('bancadas');
            Route::get('{mercado}/comerciantes', [MercadosController::class, 'comerciantes'])->name('comerciantes');
        });
        // =====================================================
        // COMERCIANTES
        // =====================================================
        Route::prefix('comerciantes')->name('comerciantes.')->group(function () {
            // Rota para DataTables Server-side
            Route::get('table-data', [ComerciantesController::class, 'getTableData'])
                ->name('table-data');
            Route::get('/', [ComerciantesController::class, 'index'])->name('index');
            Route::get('create', [ComerciantesController::class, 'create'])->name('create');
            Route::post('/', [ComerciantesController::class, 'store'])->name('store');
            Route::get('{comerciante}', [ComerciantesController::class, 'show'])->name('show');
            Route::get('{comerciante}/edit', [ComerciantesController::class, 'edit'])->name('edit');
            Route::put('{comerciante}', [ComerciantesController::class, 'update'])->name('update');
            Route::delete('{comerciante}', [ComerciantesController::class, 'destroy'])->name('destroy');

            Route::post('{comerciante}/approve', [ComerciantesController::class, 'approve'])->name('approve');
            Route::post('{comerciante}/block', [ComerciantesController::class, 'block'])->name('block');
            Route::post('{comerciante}/unblock', [ComerciantesController::class, 'unblock'])->name('unblock');
        });

        // =====================================================
        // BANCADAS
        // =====================================================
        Route::prefix('bancadas')->name('bancadas.')->group(function () {
            Route::get('table-data', [BancadasController::class, 'getTableData'])->name('table-data');
            Route::get('/', [BancadasController::class, 'index'])->name('index');
            Route::get('create', [BancadasController::class, 'create'])->name('create');
            Route::post('/', [BancadasController::class, 'store'])->name('store');
            Route::get('{stall}', [BancadasController::class, 'show'])->name('show');
            Route::get('{stall}/edit', [BancadasController::class, 'edit'])->name('edit');
            Route::put('{stall}', [BancadasController::class, 'update'])->name('update');
            Route::delete('{stall}', [BancadasController::class, 'destroy'])->name('destroy');
            Route::post('{stall}/assign', [BancadasController::class, 'assign'])->name('assign');
            Route::post('{stall}/release', [BancadasController::class, 'release'])->name('release');
        });

        // =====================================================
        // PESCADORES
        // =====================================================
        Route::prefix('pescadores')->name('pescadores.')->group(function () {
            Route::get('table-data', [PescadoresController::class, 'getTableData'])->name('table-data');
            Route::get('/', [PescadoresController::class, 'index'])->name('index');
            Route::get('create', [PescadoresController::class, 'create'])->name('create');
            Route::post('/', [PescadoresController::class, 'store'])->name('store');
            Route::get('{pescador}', [PescadoresController::class, 'show'])->name('show');
            Route::get('{pescador}/edit', [PescadoresController::class, 'edit'])->name('edit');
            Route::put('{pescador}', [PescadoresController::class, 'update'])->name('update');
            Route::delete('{pescador}', [PescadoresController::class, 'destroy'])->name('destroy');
            Route::post('{pescador}/approve', [PescadoresController::class, 'approve'])->name('approve');
            Route::post('{pescador}/suspend', [PescadoresController::class, 'suspend'])->name('suspend');
            Route::post('{pescador}/activate', [PescadoresController::class, 'activate'])->name('activate');
        });


        // =====================================================
        // ESCAMADORES
        // =====================================================
        Route::prefix('escamadores')->name('escamadores.')->group(function () {
            Route::get('table-data', [EscamadoresController::class, 'getTableData'])->name('table-data');
            Route::get('/', [EscamadoresController::class, 'index'])->name('index');
            Route::get('create', [EscamadoresController::class, 'create'])->name('create');
            Route::post('/', [EscamadoresController::class, 'store'])->name('store');
            Route::get('{escamador}', [EscamadoresController::class, 'show'])->name('show');
            Route::get('{escamador}/edit', [EscamadoresController::class, 'edit'])->name('edit');
            Route::put('{escamador}', [EscamadoresController::class, 'update'])->name('update');
            Route::delete('{escamador}', [EscamadoresController::class, 'destroy'])->name('destroy');
            Route::post('{escamador}/approve', [EscamadoresController::class, 'approve'])->name('approve');
            Route::post('{escamador}/suspend', [EscamadoresController::class, 'suspend'])->name('suspend');
            Route::post('{escamador}/activate', [EscamadoresController::class, 'activate'])->name('activate');
            Route::post('{escamador}/assign-market', [EscamadoresController::class, 'assignMarket'])->name('assign-market');
            Route::post('{escamador}/remove-market', [EscamadoresController::class, 'removeMarket'])->name('remove-market');
        });

    });


    Route::prefix('pesca')->name('pesca.')->group(function () {
        Route::resource('pescado', PescadoController::class);
        Route::resource('embarques', EmbarquesController::class);
        Route::resource('desembarques', DesembarquesController::class);
        Route::resource('rastreio', RastreioController::class);
    });

    Route::prefix('ocorrencias')->name('ocorrencias.')->group(function () {
        Route::resource('mercado', OcorrenciasMercadoController::class);
        Route::resource('pesca', OcorrenciasPescaController::class);
    });

    Route::prefix('financas')->name('financas.')->group(function () {
        Route::resource('taxas', TaxasController::class);
        Route::resource('pagamentos', PagamentosController::class);
        Route::resource('relatorios', RelatoriosController::class);
    });




    Route::prefix('utilizadores')->name('utilizadores.')->group(function () {
        Route::resource('administradores', AdministradoresController::class);
        // Fiscais
        Route::prefix('fiscais')->name('fiscais.')->group(function () {
            Route::resource('/', FiscalController::class);
            Route::put('{fiscal}/suspend', [FiscalController::class, 'suspend'])->name('fiscais.suspend');
            Route::put('{fiscal}/activate', [FiscalController::class, 'activate'])->name('fiscais.activate');
            Route::post('{fiscal}/assign-market', [FiscalController::class, 'assignMarket'])->name('fiscais.assign-market');
            Route::delete('{fiscal}/unassign-market/{market}', [FiscalController::class, 'unassignMarket'])->name('fiscais.unassign-market');
        });
    });

    Route::prefix('sistema')->name('sistema.')->group(function () {
        Route::get('configuracoes', [ConfiguracoesController::class, 'index'])->name('configuracoes');
    });


});
