<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Web\AuthController;
use App\Http\Controllers\Web\DashboardController;
use App\Http\Controllers\Web\PackageController;
use App\Http\Controllers\Web\ContractController;
use App\Http\Controllers\Web\ProfileController;
use App\Http\Controllers\Web\AdminController;
use App\Http\Controllers\Web\MatchingController;
use App\Http\Controllers\Web\BankController;
use App\Http\Controllers\Web\AuditorController;
use App\Livewire\Marketplace;
use App\Livewire\Admin\SliderManagement;

// ============================================================
// LANDING PAGE
// ============================================================
Route::get('/', function () {
    return view('landing.index');
})->name('landing');

// ============================================================
// AUTH (Login & Register)
// ============================================================
Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegisterForm'])->name('register');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// ============================================================
// DASHBOARD UTAMA (Redirect ke role masing-masing)
// ============================================================
Route::middleware(['auth'])->get('/dashboard', function () {
    $role = Auth::user()->role;
    return redirect()->route($role . '.dashboard');
})->name('dashboard');

// ============================================================
// MARKETPLACE ROUTES (PUBLIC - TANPA LOGIN)
// ============================================================
Route::get('/marketplace', Marketplace::class)->name('marketplace');
Route::get('/checkout', function() {
    return view('checkout');
})->name('checkout');

// ============================================================
// TAMBAHKAN INI UNTUK DETAIL PAKET (PUBLIC)
// ============================================================
Route::get('/package-detail/{id}', [App\Http\Controllers\Web\PackageController::class, 'show'])->name('package.detail');

// ============================================================
// ROLE: PPK
// ============================================================
Route::middleware(['auth', 'role:ppk'])->prefix('ppk')->name('ppk.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'ppk'])->name('dashboard');

    Route::get('/packages', [PackageController::class, 'index'])->name('packages.index');
    Route::get('/packages/create', [PackageController::class, 'create'])->name('packages.create');
    Route::post('/packages', [PackageController::class, 'store'])->name('packages.store');
    Route::get('/packages/{package}/edit', [PackageController::class, 'edit'])->name('packages.edit');
    Route::put('/packages/{package}', [PackageController::class, 'update'])->name('packages.update');
    Route::delete('/packages/{package}', [PackageController::class, 'destroy'])->name('packages.destroy');
    Route::get('/packages/{package}', [PackageController::class, 'show'])->name('packages.show');

    Route::get('/contracts', [ContractController::class, 'index'])->name('contracts.index');
    Route::get('/contracts/create', [ContractController::class, 'create'])->name('contracts.create');
    Route::post('/contracts', [ContractController::class, 'store'])->name('contracts.store');
    Route::get('/contracts/{contract}/edit', [ContractController::class, 'edit'])->name('contracts.edit');
    Route::put('/contracts/{contract}', [ContractController::class, 'update'])->name('contracts.update');
    Route::delete('/contracts/{contract}', [ContractController::class, 'destroy'])->name('contracts.destroy');
    Route::get('/contracts/{contract}', [ContractController::class, 'show'])->name('contracts.show');
});

// ============================================================
// ROLE: CONTRACTOR
// ============================================================
Route::middleware(['auth', 'role:contractor'])->prefix('contractor')->name('contractor.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'contractor'])->name('dashboard');

    Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');

    Route::get('/my-contracts', [ContractController::class, 'myContracts'])->name('my-contracts');
    Route::post('/contracts/{contract}/sign', [ContractController::class, 'sign'])->name('contracts.sign');
});

// ============================================================
// ROLE: SUPER ADMIN
// ============================================================
Route::middleware(['auth', 'role:super_admin'])->prefix('super-admin')->name('super_admin.')->group(function () {
    
    // ---- DASHBOARD ----
    Route::get('/dashboard', [DashboardController::class, 'superAdmin'])->name('dashboard');
    
    // ---- USER MANAGEMENT ----
    Route::get('/users', [AdminController::class, 'users'])->name('users.index');
    Route::get('/users/create', [AdminController::class, 'createUser'])->name('users.create');
    Route::post('/users', [AdminController::class, 'storeUser'])->name('users.store');
    Route::get('/users/{user}/edit', [AdminController::class, 'editUser'])->name('users.edit');
    Route::put('/users/{user}', [AdminController::class, 'updateUser'])->name('users.update');
    Route::delete('/users/{user}', [AdminController::class, 'deleteUser'])->name('users.destroy');
    
    // ---- CONTRACTOR MANAGEMENT ----
    Route::get('/contractors', [AdminController::class, 'contractors'])->name('contractors.index');
    Route::get('/contractors/create', [AdminController::class, 'createContractor'])->name('contractors.create');
    Route::post('/contractors', [AdminController::class, 'storeContractor'])->name('contractors.store');
    Route::get('/contractors/{contractor}/edit', [AdminController::class, 'editContractor'])->name('contractors.edit');
    Route::put('/contractors/{contractor}', [AdminController::class, 'updateContractor'])->name('contractors.update');
    Route::delete('/contractors/{contractor}', [AdminController::class, 'deleteContractor'])->name('contractors.destroy');
    Route::get('/contractors/{contractor}', [AdminController::class, 'showContractor'])->name('contractors.show');
    
    // ---- STATISTICS ----
    Route::get('/statistics', [AdminController::class, 'statistics'])->name('statistics');
    
    // ---- SLIDER MANAGEMENT ----
    Route::get('/sliders', SliderManagement::class)->name('sliders');
    
    // ---- PACKAGE MANAGEMENT ----
    Route::get('/packages', [PackageController::class, 'index'])->name('packages.index');
    Route::get('/packages/create', [PackageController::class, 'create'])->name('packages.create');
    Route::post('/packages', [PackageController::class, 'store'])->name('packages.store');
    Route::get('/packages/{package}/edit', [PackageController::class, 'edit'])->name('packages.edit');
    Route::put('/packages/{package}', [PackageController::class, 'update'])->name('packages.update');
    Route::delete('/packages/{package}', [PackageController::class, 'destroy'])->name('packages.destroy');
    Route::get('/packages/{package}', [PackageController::class, 'show'])->name('packages.show');
    
    // ---- CONTRACT MANAGEMENT ----
    Route::get('/contracts', [ContractController::class, 'index'])->name('contracts.index');
    Route::get('/contracts/create', [ContractController::class, 'create'])->name('contracts.create');
    Route::post('/contracts', [ContractController::class, 'store'])->name('contracts.store');
    Route::get('/contracts/{contract}/edit', [ContractController::class, 'edit'])->name('contracts.edit');
    Route::put('/contracts/{contract}', [ContractController::class, 'update'])->name('contracts.update');
    Route::delete('/contracts/{contract}', [ContractController::class, 'destroy'])->name('contracts.destroy');
    Route::get('/contracts/{contract}', [ContractController::class, 'show'])->name('contracts.show');
    
    // ---- CONTRACTOR PROFILE ----
    Route::get('/profile', [ProfileController::class, 'index'])->name('profile');
    Route::get('/my-contracts', [ContractController::class, 'myContracts'])->name('my-contracts');
    
    // ---- MATCHING ----
    Route::get('/matching/queue', [MatchingController::class, 'queue'])->name('matching.queue');
    Route::post('/matching/process', [MatchingController::class, 'process'])->name('matching.process');
    Route::get('/matching/history', [MatchingController::class, 'history'])->name('matching.history');
    Route::get('/matching/result/{packageId}', [MatchingController::class, 'result'])->name('matching.result');
    
    // ---- FINANCING ----
    Route::get('/pending', [BankController::class, 'pending'])->name('pending');
    Route::get('/financing', [BankController::class, 'financing'])->name('financing');
    Route::get('/contracts/financing', [BankController::class, 'contracts'])->name('contracts.financing');
    Route::get('/contracts/{contract}/financing', [BankController::class, 'financingDetail'])->name('financing.detail');
    Route::post('/contracts/{contract}/financing', [BankController::class, 'approveFinancing'])->name('financing.approve');
    Route::post('/financing/{financing}/disburse', [BankController::class, 'disburse'])->name('financing.disburse');
    Route::get('/statistics/bank', [BankController::class, 'statistics'])->name('bank.statistics');
    
    // ---- AUDIT ----
    Route::get('/audit/logs', [AuditorController::class, 'logs'])->name('audit.logs');
    Route::get('/audit/contracts', [AuditorController::class, 'contracts'])->name('audit.contracts');
    Route::get('/audit/contracts/{contract}/audit', [AuditorController::class, 'auditDetail'])->name('audit.detail');
    Route::post('/audit/contracts/{contract}/approve', [AuditorController::class, 'approveAudit'])->name('audit.approve');
});

// ============================================================
// ROLE: ADMIN
// ============================================================
Route::middleware(['auth', 'role:admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'admin'])->name('dashboard');

    Route::get('/users', [AdminController::class, 'users'])->name('users.index');
    Route::get('/users/create', [AdminController::class, 'createUser'])->name('users.create');
    Route::post('/users', [AdminController::class, 'storeUser'])->name('users.store');
    Route::get('/users/{user}/edit', [AdminController::class, 'editUser'])->name('users.edit');
    Route::put('/users/{user}', [AdminController::class, 'updateUser'])->name('users.update');
    Route::delete('/users/{user}', [AdminController::class, 'deleteUser'])->name('users.destroy');

    Route::get('/contractors', [AdminController::class, 'contractors'])->name('contractors.index');
    Route::get('/contractors/create', [AdminController::class, 'createContractor'])->name('contractors.create');
    Route::post('/contractors', [AdminController::class, 'storeContractor'])->name('contractors.store');
    Route::get('/contractors/{contractor}/edit', [AdminController::class, 'editContractor'])->name('contractors.edit');
    Route::put('/contractors/{contractor}', [AdminController::class, 'updateContractor'])->name('contractors.update');
    Route::delete('/contractors/{contractor}', [AdminController::class, 'deleteContractor'])->name('contractors.destroy');
    Route::get('/contractors/{contractor}', [AdminController::class, 'showContractor'])->name('contractors.show');

    Route::get('/statistics', [AdminController::class, 'statistics'])->name('statistics');

    // ============================================================
    // SLIDER MANAGEMENT (ADMIN)
    // ============================================================
    Route::get('/sliders', SliderManagement::class)->name('sliders');
});

// ============================================================
// ROLE: AGENT
// ============================================================
Route::middleware(['auth', 'role:agent'])->prefix('agent')->name('agent.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'agent'])->name('dashboard');

    Route::get('/matching/queue', [MatchingController::class, 'queue'])->name('matching.queue');
    Route::post('/matching/process', [MatchingController::class, 'process'])->name('matching.process');
    Route::get('/matching/history', [MatchingController::class, 'history'])->name('matching.history');
    Route::get('/matching/result/{packageId}', [MatchingController::class, 'result'])->name('matching.result');
});

// ============================================================
// ROLE: BANK
// ============================================================
Route::middleware(['auth', 'role:bank'])->prefix('bank')->name('bank.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'bank'])->name('dashboard');

    Route::get('/pending', [BankController::class, 'pending'])->name('pending');
    Route::get('/financing', [BankController::class, 'financing'])->name('financing');
    Route::get('/contracts', [BankController::class, 'contracts'])->name('contracts');
    Route::get('/contracts/{contract}/financing', [BankController::class, 'financingDetail'])->name('financing.detail');
    Route::post('/contracts/{contract}/financing', [BankController::class, 'approveFinancing'])->name('financing.approve');
    Route::post('/financing/{financing}/disburse', [BankController::class, 'disburse'])->name('financing.disburse');
    Route::get('/statistics', [BankController::class, 'statistics'])->name('statistics');
});

// ============================================================
// ROLE: AUDITOR
// ============================================================
Route::middleware(['auth', 'role:auditor'])->prefix('auditor')->name('auditor.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'auditor'])->name('dashboard');

    Route::get('/logs', [AuditorController::class, 'logs'])->name('logs');
    Route::get('/contracts', [AuditorController::class, 'contracts'])->name('contracts');
    Route::get('/contracts/{contract}/audit', [AuditorController::class, 'auditDetail'])->name('contracts.audit');
    Route::post('/contracts/{contract}/approve', [AuditorController::class, 'approveAudit'])->name('contracts.approve');
});