<?php

use App\Http\Controllers\CandidateController;
use App\Http\Controllers\CityController;
use App\Http\Controllers\DistrictController;
use App\Http\Controllers\DivisionController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SeatController;
use App\Http\Controllers\TeamController;
use App\Http\Controllers\UpazilaController;
use App\Http\Controllers\VoterAreaController;
use App\Http\Controllers\VoterController;
use App\Http\Controllers\WardController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn() => view('welcome'))->name('home');


// public
Route::get('/seats', [SeatController::class, 'index'])->name('seats.index');
Route::get('/seats/statistics', [SeatController::class, 'statistics'])->name('seats.statistics');
Route::get('/seats/{seat}', [SeatController::class, 'show'])
    ->whereNumber('seat')
    ->name('seats.show');
    




// AUTH ONLY
Route::middleware('auth')->group(function () {
    // dashboard redirect by role
    Route::get('/dashboard', function () {
        $role = auth()->user()->role ?? 'user';

        return match ($role) {
            'admin' => redirect()->route('admin.dashboard'),
            'candidate' => redirect()->route('candidate.dashboard'),
            default => redirect()->route('user.dashboard'),
        };
    })->name('dashboard');

    // Dashboards by role
    Route::middleware('role:admin')->get('/admin/dashboard', fn() => view('admin.dashboard'))->name('admin.dashboard');

    Route::middleware('role:candidate')->get('/candidate/dashboard', fn() => view('candidate.dashboard'))->name('candidate.dashboard');

    Route::middleware('role:user')->get('/user/dashboard', fn() => view('user.dashboard'))->name('user.dashboard');

    // Voters (protected)
    Route::get('/voters', [VoterController::class, 'index'])->name('voters.index');
    Route::get('/voters/create', [VoterController::class, 'create'])->name('voters.create');
    Route::post('/voters/bulk-store', [VoterController::class, 'bulkStore'])->name('voters.bulk.store');

  
    Route::middleware('role:admin')->group(function () {
        Route::get('/seats/by-district', [SeatController::class, 'getByDistrict'])->name('seats.by-district');

        Route::get('/seats/create', [SeatController::class, 'create'])->name('seats.create');
        Route::post('/seats', [SeatController::class, 'store'])->name('seats.store');
        Route::get('/seats/{seat}/edit', [SeatController::class, 'edit'])->name('seats.edit');
        Route::put('/seats/{seat}', [SeatController::class, 'update'])->name('seats.update');
        Route::delete('/seats/{seat}', [SeatController::class, 'destroy'])->name('seats.destroy');
    });


    Route::middleware('role:candidate')->group(function () {
        Route::get('/candidates/create', [CandidateController::class, 'create'])->name('candidates.create');
        Route::post('/candidates', [CandidateController::class, 'store'])->name('candidates.store');
        Route::get('/candidates/{candidate}/edit', [CandidateController::class, 'edit'])->name('candidates.edit');
        Route::put('/candidates/{candidate}', [CandidateController::class, 'update'])->name('candidates.update');
        Route::delete('/candidates/{candidate}', [CandidateController::class, 'destroy'])->name('candidates.destroy');

        Route::resource('teams', TeamController::class);

        Route::prefix('teams/{team}')
            ->name('teams.')
            ->group(function () {
                Route::get('members', [TeamController::class, 'members'])->name('members.index');
                Route::post('members', [TeamController::class, 'addMembers'])->name('members.store');
                Route::get('members/create', [TeamController::class, 'membersCreate'])->name('members.create');
                Route::get('members/{teamMember}', [TeamController::class, 'memberShow'])->name('members.show');
                Route::patch('members/{teamMember}', [TeamController::class, 'updateMemberRole'])->name('members.update');
                Route::delete('members/{teamMember}', [TeamController::class, 'removeMember'])->name('members.destroy');
            });
    });

    
    Route::middleware('role:admin')->group(function () {
        Route::resource('divisions', DivisionController::class);
        Route::resource('districts', DistrictController::class);
        Route::resource('upazilas', UpazilaController::class)->except(['show']);
        Route::resource('cities', CityController::class)->except(['show']);
        Route::resource('wards', WardController::class)->except(['show']);
        Route::resource('voter-areas', VoterAreaController::class)->except(['show']);
    });

    // Profile (any logged in)
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

Route::get('/candidates', [CandidateController::class, 'index'])->name('candidates.index');
    Route::get('/candidates/{candidate}', [CandidateController::class, 'show'])->name('candidates.show');



require __DIR__ . '/auth.php';
