<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Stevebauman\Location\Facades\Location;
use App\Http\Controllers\DramaController;
use App\Http\Controllers\InstallController;
use App\Http\Controllers\AdminController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

Route::get('/', function (Request $request) {
    if (session()->has('locale')) {
        return redirect()->route('home', ['lang' => session('locale')]);
    }
    $ip = $request->ip(); 
    $position = Location::get($ip);
    $countryCode = $position ? strtolower($position->countryCode) : 'id';
    $map = [
        'id' => 'in', // Indonesia -> /in
        'us' => 'en', // USA -> /en
        'gb' => 'en', // UK -> /en
        'es' => 'es', // Spain -> /es
        'mx' => 'es', // Mexico -> /es
        'de' => 'de', // Germany -> /de
        'fr' => 'fr', // France -> /fr
        'br' => 'pt', // Brazil -> /pt
        'pt' => 'pt', // Portugal -> /pt
    ];
    $langToRedirect = $map[$countryCode] ?? 'en';
    session(['locale' => $langToRedirect]);
    return redirect()->route('home', ['lang' => $langToRedirect]);
})->name('home.root');


Route::prefix('{lang}')
    ->whereIn('lang', ['in', 'en', 'es', 'de', 'fr', 'pt'])
    ->group(function () {

    Route::get('/', [DramaController::class, 'home'])
        ->name('home');

    Route::get('/browse/{page?}', [DramaController::class, 'archive'])
        ->where('page', '[0-9]+')
        ->name('archive');

    Route::get('/search/{page?}', [DramaController::class, 'search'])
        ->where('page', '[0-9]+')
        ->name('search');

    Route::get('/{type}/{pid}/{slug}', [DramaController::class, 'single'])
        ->where('type', 'movie') 
        ->name('single');

    Route::get('/ep/{pid_slug}/{epslug}', [DramaController::class, 'episode'])
        ->name('episode');

    Route::get('/genres/{gid?}/{page?}', [DramaController::class, 'genre'])
        ->where('gid', '[0-9]+')
        ->where('page', '[0-9]+')
        ->name('genre');
});
    
Route::get('/privacy-policy', function () {
    return view('privacy-policy');
})->name('privacy-policy');
Route::get('/syarat-penggunaan', function () {
    return view('syarat-penggunaan');
})->name('syarat-penggunaan');

Route::get('/clear/{type}', function ($type) {
    Artisan::call($type . ':clear');
    return '<pre>' . Artisan::output() . '</pre>';
});

Route::get('/install', [InstallController::class, 'index'])->name('install.index');
Route::post('/install', [InstallController::class, 'install'])->name('install.process');

Route::prefix('admin-panel')->group(function () {
    Route::get('/', [AdminController::class, 'showLogin'])->name('admin.login');
    Route::get('/login', [AdminController::class, 'showLogin'])->name('admin.login');
    Route::post('/login', [AdminController::class, 'login'])->name('admin.login.process');
    Route::get('/logout', [AdminController::class, 'logout'])->name('admin.logout');
    
    Route::get('/dashboard', [AdminController::class, 'index'])->name('admin.dashboard');
    Route::post('/update', [AdminController::class, 'update'])->name('admin.update');
});


Route::get('/manifest.json', [App\Http\Controllers\AdminController::class, 'manifest'])->name('manifest.json');

Route::get('{lang}/history', function ($lang) {
    return view('history', ['lang' => $lang]);
})->name('history');
