<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\Web\Auth\FindIdController;
use App\Http\Controllers\Web\Auth\FindPasswordController;
use App\Http\Controllers\Web\Auth\RegisterController;
use App\Http\Controllers\Web\Auth\LoginController;
use App\Http\Controllers\Web\Mypage\InfoGateController;
use App\Http\Controllers\Web\Cs\NoticeController;
use App\Http\Controllers\Web\Auth\EmailVerificationController;
use App\Http\Controllers\Web\Cs\CsQnaController;
use App\Http\Controllers\Web\Mypage\MypageQnaController;


Route::view('/', 'web.home')->name('web.home');

/*
|--------------------------------------------------------------------------
| CS
|--------------------------------------------------------------------------
*/
Route::prefix('cs')->name('web.cs.')->group(function () {
    Route::get('/notice', [NoticeController::class, 'index'])->name('notice.index');
    Route::get('/notice/{seq}', [NoticeController::class, 'show'])->whereNumber('seq')->name('notice.show');
    Route::get('/notice/{seq}/file/{slot}', [NoticeController::class, 'download'])
        ->whereNumber('seq')
        ->whereIn('slot', ['1','2'])
        ->name('notice.file');

    Route::view('faq',    'web.cs.faq.index')->name('faq.index');
    Route::view('kakao',  'web.cs.kakao.index')->name('kakao.index');
    Route::middleware('legacy.auth')->group(function () {
        Route::get('qna',  [CsQnaController::class, 'create'])->name('qna.index');
        Route::post('qna', [CsQnaController::class, 'store'])->name('qna.store');
    });
    Route::view('guide',  'web.cs.guide.index')->name('guide.index');
});

/*
|--------------------------------------------------------------------------
| My Page
|--------------------------------------------------------------------------
*/
// 전체 로그인필요
Route::prefix('mypage')->name('web.mypage.')
    ->middleware('legacy.auth')
    ->group(function () {
        Route::get('info',  [InfoGateController::class, 'show'])->name('info.index');
        Route::post('info', [InfoGateController::class, 'verify'])->name('info.verify');

        Route::get('info_renew', [InfoGateController::class, 'info_renew'])->name('info.renew');
        Route::post('info/password-update', [InfoGateController::class, 'passwordUpdate'])->name('info.password.update');
        Route::post('info/pin2', [InfoGateController::class, 'updatePin2'])->name('info.pin2.update');
        Route::post('info/pass/ready',   [InfoGateController::class, 'passReady'])->name('info.pass.ready');
        Route::post('info/danal/start',  [InfoGateController::class, 'danalStart'])->name('info.danal.start');
        Route::post('info/danal/result', [InfoGateController::class, 'danalResult'])->name('info.danal.result');
        Route::get('info/gate-reset', [InfoGateController::class, 'gateReset'])->name('info.gate_reset');
        Route::post('info/withdraw/verify-out', [InfoGateController::class, 'verifyOut'])->name('info.withdraw.verify_out');
        Route::post('info/marketing/update', [InfoGateController::class, 'marketingUpdate'])->name('info.marketing.update');
        Route::post('info/withdraw', [InfoGateController::class, 'withdraw'])->name('info.withdraw');

        Route::view('usage',    'web.mypage.usage.index')->name('usage.index');
        Route::view('exchange', 'web.mypage.exchange.index')->name('exchange.index');
        Route::get('qna', [MypageQnaController::class, 'index'])->name('qna.index');
        Route::get('qna/{seq}', [MypageQnaController::class, 'show'])->whereNumber('seq')->name('qna.show');
    });

/*
|--------------------------------------------------------------------------
| Policy
|--------------------------------------------------------------------------
*/
//일반접근
Route::prefix('policy')->name('web.policy.')->group(function () {
    Route::view('/',        'web.policy.index')->name('index');
    Route::view('privacy',  'web.policy.privacy.index')->name('privacy.index');
    Route::view('terms',    'web.policy.terms.index')->name('terms.index');
    Route::view('email',    'web.policy.email.index')->name('email.index');
});

/*
|--------------------------------------------------------------------------
| Auth
|--------------------------------------------------------------------------
*/

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

    // 게스트만 접근 (로그인한 사람은 못 들어옴)
    Route::middleware('legacy.guest')->group(function () {
        // 로그인
        Route::get('login', [LoginController::class, 'show'])->name('login');
        Route::post('login', [LoginController::class, 'prc'])->name('login.prc');
        Route::get('login/dormancy-prc', [LoginController::class, 'dormancyPrc'])->name('login.dormancy_prc');

        // 회원가입 Step0
        Route::get('register', [RegisterController::class, 'showStep0'])->name('register');
        Route::post('register/phone-check', [RegisterController::class, 'postPhoneCheck'])->name('register.phone_check');

        // Step1(약관)
        Route::get('register/terms', [RegisterController::class, 'showTerms'])->name('register.terms');
        Route::post('register/terms', [RegisterController::class, 'termsSubmit'])->name('register.terms.submit');

        // Step2 다날 인증
        Route::post('register/danal/start',  [RegisterController::class, 'danalStart'])->name('register.danal.start');
        Route::post('register/danal/result', [RegisterController::class, 'danalResult'])->name('register.danal.result');

        // Step3 회원정보 입력
        Route::get('register/profile', [RegisterController::class, 'showProfileForm'])->name('register.profile');
        Route::post('register/profile', [RegisterController::class, 'submitProfile'])->name('register.profile.submit');
        Route::post('register/check-login-id', [RegisterController::class, 'checkLoginId'])->name('register.check_login_id');

        // 회원가입 디버그용
        Route::get('register/profile/debug', [RegisterController::class, 'debugProfile'])->name('register.profile.debug');
        Route::post('register/profile/debug/run', [RegisterController::class, 'debugProfileRun'])->name('register.profile.debug.run');

        // 아이디 찾기
        Route::get('find-id',           [FindIdController::class, 'show'])->name('find_id');
        Route::post('find-id/send-code',[FindIdController::class, 'sendCode'])->name('find_id.send_code');
        Route::post('find-id/verify',   [FindIdController::class, 'verify'])->name('find_id.verify');
        Route::post('find-id/reset',    [FindIdController::class, 'reset'])->name('find_id.reset');

        // 비밀번호 찾기
        Route::get('find-password', [FindPasswordController::class, 'show'])->name('find_password.show');
        Route::post('find-password/send-mail', [FindPasswordController::class, 'sendMail'])->name('find_password.send_mail');
        Route::get('find-password/verify', [FindPasswordController::class, 'verifyLink'])->name('find_password.verify')->middleware('signed');
        Route::post('find-password/reset', [FindPasswordController::class, 'reset'])->name('find_password.reset');

        //이메일인증
        Route::get('email-required', [EmailVerificationController::class, 'requiredPage'])->name('email.required');
        Route::post('email/send-verify', [EmailVerificationController::class, 'sendVerify'])->name('email.send_verify');
        Route::get('email/verify', [EmailVerificationController::class, 'verify'])->name('email.verify')->middleware('signed');
    });

    Route::post('logout', [LoginController::class, 'logout'])->name('logout');
});

Route::prefix('auth')->name('web.auth.')->group(function () {
    Route::middleware('legacy.guest')->group(function () {
        Route::get('email-required', [EmailVerificationController::class, 'requiredPage'])->name('email.required');
        Route::post('email/send-verify', [EmailVerificationController::class, 'sendVerify'])->name('email.send_verify');
        Route::get('email/verify', [EmailVerificationController::class, 'verify'])->name('email.verify')->middleware('signed'); // 만료/위변조 자동 차단
    });
});

/*
|--------------------------------------------------------------------------
| Legacy redirects
|--------------------------------------------------------------------------
*/
Route::get('/login', fn() => redirect()->route('web.auth.login'))->name('web.login');
Route::get('/register', fn() => redirect()->route('web.auth.register'))->name('web.signup');

/*디버깅용 세션 개발*/
if (config('app.debug') || app()->environment('local')) {
    require __DIR__.'/dev_web.php';
}


Route::fallback(fn () => abort(404));

