<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\Auth\ExternalLoginController;
use App\Services\SiteApiClient;

Route::get('/', [HomeController::class, 'index'])
    ->name('index');

Route::get('/sebaransdgs', function () {
    return view('home.sebaransdgs');
})->name('home.sebaransdgs');

Route::get('/sebaranrusun', function () {
    return view('home.sebaranrusun');
})->name('home.sebaranrusun');

Route::get('/sebaranrusunprovinsi', function () {
    return view('home.sebaranrusunprovinsi');
})->name('home.sebaranrusunprovinsi');

Route::get('/simulasikpr', function () {
    return view('home.simulasikpr');
})->name('home.simulasikpr');

Route::get('/designprototype', function () {
    return view('home.designprototype');
})->name('home.designprototype');

Route::get('/designkrsjawa3', function () {
    return view('home.designkrsjawa3');
})->name('home.designkrsjawa3');

Route::get('/designliliput', function () {
    return view('home.designliliput');
})->name('home.designliliput');

Route::get('/peraturan', function () {
    return view('home.peraturan');
})->name('home.peraturan');

Route::get('/artikel', function () {
    return view('home.artikel');
})->name('artikel');

Route::get('/artikel/{id}', function ($id) {
    return view('home.artikel_detail', ['id' => $id]);
})->name('artikel.detail');

Route::get('/bukuumum', function () {
    return view('home.bukuumum');
})->name('home.bukuumum');

Route::get('/bukukrs3', function () {
    return view('home.bukukrs3');
})->name('home.bukukrs3');

Route::get('/material', function () {
    return view('home.material');
})->name('home.material');

Route::get('/tentang', function () {
    return view('home.tentang');
})->name('home.tentang');

Route::get('/sertifikathakmilik', function () {
    return view('home.sertifikathakmilik');
})->name('home.sertifikathakmilik');

Route::get('/pbg', function () {
    return view('home.pbg');
})->name('home.pbg');

Route::get('/sikumbangkabkota', function () {
    return view('home.sikumbangkabkota');
})->name('home.sikumbangkabkota');

Route::get('/sikumbangcek', function () {
    return view('home.sikumbangcek');
})->name('home.sikumbangcek');

Route::get('/sikumbangprovinsi', function () {
    return view('home.sikumbangprovinsi');
})->name('home.sikumbangprovinsi');

Route::get('/timmagang', function () {
    return view('home.timmagang');
})->name('home.timmagang');

Route::get('/index-one-page', [HomeController::class, 'index_one_page'])
    ->name('index-one-page');

Route::get('/index2', [HomeController::class, 'index2'])
    ->name('index2');

Route::get('/index2-one-page', [HomeController::class, 'index2_one_page'])
    ->name('index2-one-page');

Route::get('/index3', [HomeController::class, 'index3'])
    ->name('index3');

Route::get('/index3-one-page', [HomeController::class, 'index3_one_page'])
    ->name('index3-one-page');

Route::get('/index-dark', [HomeController::class, 'index_dark'])
    ->name('index-dark');

Route::get('/about', [PagesController::class, 'about'])
    ->name('about');

Route::get('/lawyer', [PagesController::class, 'lawyer'])
    ->name('lawyer');

Route::get('/lawyer-details', [PagesController::class, 'lawyer_details'])
    ->name('lawyer-details');

Route::get('/case', [PagesController::class, 'case'])
    ->name('case');

Route::get('/case-details', [PagesController::class, 'case_details'])
    ->name('case-details');

Route::get('/testimonials', [PagesController::class, 'testimonials'])
    ->name('testimonials');

Route::get('/pricing', [PagesController::class, 'pricing'])
    ->name('pricing');

Route::get('/faq', [PagesController::class, 'faq'])
    ->name('faq');

Route::get('/404', [PagesController::class, 'notFound'])
    ->name('404');

Route::get('/services', [PagesController::class, 'services'])
    ->name('services');

Route::get('/business-law', [PagesController::class, 'business_law'])
    ->name('business-law');

Route::get('/family-law', [PagesController::class, 'family_law'])
    ->name('family-law');

Route::get('/insurance-defence', [PagesController::class, 'insurance_defence'])
    ->name('insurance-defence');

Route::get('/industrial-law', [PagesController::class, 'industrial_law'])
    ->name('industrial-law');

Route::get('/criminal-law', [PagesController::class, 'criminal_law'])
    ->name('criminal-law');

Route::get('/employement-law', [PagesController::class, 'employement_law'])
    ->name('employement-law');

Route::get('/products', [PagesController::class, 'products'])
    ->name('products');

Route::get('/product-details', [PagesController::class, 'product_details'])
    ->name('product-details');

Route::get('/cart', [PagesController::class, 'cart'])
    ->name('cart');

Route::get('/checkout', [PagesController::class, 'checkout'])
    ->name('checkout');

Route::get('/wishlist', [PagesController::class, 'wishlist'])
    ->name('wishlist');

Route::get('/sign-up', [PagesController::class, 'sign_up'])
    ->name('sign-up');

Route::get('/login', [ExternalLoginController::class, 'showLogin'])->name('login');
Route::post('/login', [ExternalLoginController::class, 'login'])->name('login');

Route::get('/admin/dashboard', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    // Jika session('auth.user') belum ada, decode dari JWT token
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    // Simpan field user yang penting saja
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.dashboard');
})->name('admin.dashboard');

Route::get('/admin/kelola-admin', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    // Check if user is superadmin
    $user = session('auth.user');
    if (!$user || strtolower($user['role'] ?? '') !== 'superadmin') {
        return redirect()->route('admin.dashboard')->with('error', 'Anda tidak memiliki akses ke halaman ini. Hanya superadmin yang diizinkan.');
    }
    return view('admin.kelolaAdmin');
})->name('admin.kelolaAdmin');

Route::get('/admin/manajemen-admin', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    // Check if user is superadmin
    $user = session('auth.user');
    if (!$user || strtolower($user['role'] ?? '') !== 'superadmin') {
        return redirect()->route('admin.dashboard')->with('error', 'Anda tidak memiliki akses ke halaman ini. Hanya superadmin yang diizinkan.');
    }
    return view('admin.manajemenAdmin');
})->name('admin.manajemenAdmin');

// Navbar Config Routes
Route::get('/admin/pengaturan-navbar', [App\Http\Controllers\Admin\NavbarConfigController::class, 'index'])->name('admin.navbarConfig');
Route::post('/admin/pengaturan-navbar/update', [App\Http\Controllers\Admin\NavbarConfigController::class, 'update']);
Route::post('/admin/pengaturan-navbar/add', [App\Http\Controllers\Admin\NavbarConfigController::class, 'addMenu']);
Route::delete('/admin/pengaturan-navbar/delete/{menuKey}', [App\Http\Controllers\Admin\NavbarConfigController::class, 'deleteMenu']);

Route::get('/admin/video', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.videoAdmin');
})->name('admin.video');

Route::get('/admin/bankdesign', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.bankdesainAdmin');
})->name('admin.bankdesign');

Route::get('/admin/design-prototype', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.designprototypeAdmin');
})->name('admin.design-prototype');

Route::get('/admin/statistik-admin', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.statistikAdmin');
})->name('admin.statistik-admin');

Route::get('/admin/statistik-rating', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.statistikRating');
})->name('admin.statistik-rating');

Route::get('/admin/infografis', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.infografisAdmin');
})->name('admin.infografis');

Route::get('/admin/banner', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.bannerAdmin');
})->name('admin.banner');

Route::get('/admin/arsip', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.arsipAdmin');
})->name('admin.arsip');

Route::get('/admin/berita', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.beritaAdmin');
})->name('admin.berita');

Route::get('/admin/peraturan', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.peraturanAdmin');
})->name('admin.peraturan');

Route::get('/admin/feedback', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.feedbackAdmin');
})->name('admin.feedback');

Route::get('/admin/laporanbug', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.laporanbugAdmin');
})->name('admin.laporanbug');

Route::get('/admin/pengaturan-situs', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }
    return view('admin.pengaturansitusAdmin');
})->name('admin.pengaturan-situs');

Route::get('/admin/sebaran-rusun', function () {
    if (! session()->has('auth.token')) {
        return redirect()->route('login')->with('status', 'Silakan login terlebih dahulu.');
    }
    if (! session()->has('auth.user')) {
        $token = session('auth.token');
        if ($token) {
            $parts = explode('.', $token);
            if (count($parts) === 3) {
                $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
                if (is_array($payload)) {
                    $user = [
                        'id' => $payload['id'] ?? null,
                        'email' => $payload['email'] ?? null,
                        'role' => $payload['role'] ?? null,
                        'site_id' => $payload['site_id'] ?? null,
                    ];
                    session(['auth.user' => $user]);
                }
            }
        }
    }

    // Blokir akses untuk admin DIY dan Jateng
    $daerah = strtolower(env('DAERAH', ''));
    $user = session('auth.user');
    $userEmail = strtolower($user['email'] ?? '');

    if ($daerah === 'diy' || $daerah === 'jateng' || $userEmail === 'admin@jateng.com' || $userEmail === 'admin@diy.com') {
        return redirect()->route('admin.dashboard')->with('error', 'Anda tidak memiliki akses ke halaman ini.');
    }

    return view('admin.sebaranrusunAdmin');
})->name('admin.sebaran-rusun');

Route::post('/logout', [ExternalLoginController::class, 'logout'])->name('logout');

Route::get('/blog', [PagesController::class, 'blog'])
    ->name('blog');

Route::get('/blog-standard', [PagesController::class, 'blog_standard'])
    ->name('blog-standard');

Route::get('/blog-left-sidebar', [PagesController::class, 'blog_left_sidebar'])
    ->name('blog-left-sidebar');

Route::get('/blog-right-sidebar', [PagesController::class, 'blog_right_sidebar'])
    ->name('blog-right-sidebar');

Route::get('/blog-details', [PagesController::class, 'blog_details'])
    ->name('blog-details');

Route::get('/contact', [PagesController::class, 'contact'])
    ->name('contact');

Route::fallback(function () {
    return view('pages.404');
});
