<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Http\Controllers\Controller;
use App\Http\Controllers\InquiryConfirmController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\MemberRegistrationController;
use App\Http\Controllers\PasswordReminderController;
use App\Http\Controllers\MypageController;
use App\Http\Controllers\UserInfoController;
use App\Http\Controllers\UserEditController;
use App\Http\Controllers\UserEditConfirmController;
use App\Http\Controllers\UserWithdrawController;
use App\Http\Controllers\UserTagReissueController;
use App\Http\Controllers\RegularContractController;
use App\Http\Controllers\RegularContractCreateController;
use App\Http\Controllers\ParkingSearchController;
use App\Http\Controllers\ParkWaitlistController;
use App\Http\Controllers\ReceiptController;
use App\Http\Controllers\SealReissueController;
use App\Http\Controllers\ParkDetailController;
use App\Http\Controllers\UserInformationController;
use App\Http\Controllers\NewsController;

// 全体トップページ
Route::get('/', function(){return redirect() -> route('top');});
Route::get('/top', function () { return view('general.top'); })->name('top');

// ログアウト処理
Route::get('/logout', function () {
    // セッション全削除
    session()->flush();
    return redirect()->route('top');
})->name('logout');

Route::middleware([\App\Http\Middleware\ManagementMiddleware::class])->group(function () {

    // 一般 (運営元毎のページ)
    Route::get('/{management_code}', [NewsController::class, 'init'])->name('management_top');
    Route::get('/{management_code}/member_regist', function () { return view('general.member_regist'); })->name('member_regist');
    Route::post('/{management_code}/member_regist_send_mail', [MemberRegistrationController::class, 'sendMail'])->name('member_regist_send_mail');
    Route::get('/{management_code}/member_regist_input', [MemberRegistrationController::class, 'index'])->name('member_regist_input')->middleware('signed');
    Route::get('/{management_code}/member_regist_input_back', [MemberRegistrationController::class, 'indexBack'])->name('member_regist_input_back');
    Route::post('/{management_code}/member_regist_confirm', [MemberRegistrationController::class, 'confirm'])->name('member_regist_confirm');
    Route::post('/{management_code}/member_regist_complete',  [MemberRegistrationController::class, 'complete'])->name('member_regist_complete');
    Route::get('/{management_code}/password_reminder', function () { return view('general.password_reminder'); })->name('password_reminder');
    Route::post('/{management_code}/password_reminder_complete', [PasswordReminderController::class, 'sendMail'])->name('password_reminder_complete');
    Route::get('/{management_code}/login_input', function () { return view('general.login_input'); })->name('login_input');
    Route::post('/{management_code}/login', [LoginController::class, 'login'])->name('login');
    Route::get('/{management_code}/faq', function () { return view('general.faq'); })->name('faq');
    Route::get('/{management_code}/inquiry_input', [InquiryConfirmController::class, 'index'])->name('inquiry_input');
    Route::post('/{management_code}/inquiry_confirm', [InquiryConfirmController::class, 'confirm'])->name('inquiry_confirm');
    Route::post('/{management_code}/inquiry_complete', [InquiryConfirmController::class, 'complete'])->name('inquiry_complete');
    Route::post('/{management_code}/news', [NewsController::class, 'detail'])->name('news');
    Route::get('/{management_code}/company', function () { return view('general.company'); })->name('company');
    Route::get('/{management_code}/privacy_policy', function () { return view('general.privacy_policy'); })->name('privacy_policy');
    Route::get('/{management_code}/terms_of_service', function () { return view('general.terms_of_service'); })->name('terms_of_service');
    Route::get('/{management_code}/special_commercial_law', function () { return view('general.special_commercial_law'); })->name('special_commercial_law');
    Route::get('/{management_code}/privacy_handling', function () { return view('general.privacy_handling'); })->name('privacy_handling');
    Route::get('/{management_code}/disclosure_matters', function () { return view('general.disclosure_matters'); })->name('disclosure_matters');
    Route::get('/{management_code}/guide', function () { return view('general.guide'); })->name('guide');
    Route::get('/{management_code}/sitemap', function () { return view('general.sitemap'); })->name('sitemap');
    Route::get('/{management_code}/error', function () { return view('general.error'); })->name('error');

    // マイページ
    Route::get('/{management_code}/mypage', [MypageController::class, 'index'])->name('mypage');

    // ユーザー情報確認・編集
    Route::get('/{management_code}/user/info', [UserInfoController::class, 'show'])->name('user.info');
    Route::get('/{management_code}/user/edit', [UserEditController::class, 'show'])->name('user.edit');
    Route::post('/{management_code}/user/edit', [UserEditController::class, 'update'])->name('user.edit.post');
    Route::get('/{management_code}/user/edit/confirm', [UserEditConfirmController::class, 'show'])->name('user.confirm');
    Route::post('/{management_code}/user/edit/submit', [UserEditConfirmController::class, 'submit'])->name('user.edit.submit');
    Route::get('/{management_code}/user/edit/verify', [UserEditConfirmController::class, 'verify'])->name('user.edit.verify');

    // 退会
    Route::get('/{management_code}/user/withdraw', [UserWithdrawController::class, 'showConfirm'])->name('user.withdraw');
    Route::post('/{management_code}/user/withdraw/confirm', [UserWithdrawController::class, 'withdraw'])->name('user.withdraw.confirm');

    // タグ再発行
    Route::get('/{management_code}/user/tag_reissue', [UserTagReissueController::class, 'index'])->name('user.tag_reissue');
    Route::get('/{management_code}/user/tag_reissue/complete', [UserTagReissueController::class, 'complete'])->name('user.tag_reissue.complete');

    // 定期契約情報確認
    Route::get('/{management_code}/regular_contract/info', [RegularContractController::class, 'showInfo'])->name('regular_contract.info');

    // 領収書発行
    Route::get('/{management_code}/receipt/input/{contract_id}', [ReceiptController::class, 'input'])->name('receipt.input');
    Route::get('/{management_code}/receipt/download/{contract_id}', [ReceiptController::class, 'download'])->name('receipt.download');
    Route::post('/{management_code}/receipt/issue/{contract_id}', [ReceiptController::class, 'issue'])->name('receipt.issue');

    // シール再発行
    Route::get('/{management_code}/seal/reissue/{contract_id}', [SealReissueController::class, 'index'])->name('seal.reissue');
    Route::get('/{management_code}/seal/reissue/reason/{contract_id}', [SealReissueController::class, 'reason'])->name('seal.reissue.reason');
    Route::post('/{management_code}/seal/reissue/complete/{contract_id}', [SealReissueController::class, 'complete'])->name('seal.reissue.complete');

    // 新規定期契約
    Route::get('/{management_code}/regular_contract/create', [RegularContractCreateController::class, 'show'])->name('regular_contract.create');
    Route::get('/{management_code}/api/park-detail/{park_id}', [ParkDetailController::class, 'show']);
    Route::get('/{management_code}/regular-contract/regulationCheck', [RegularContractCreateController::class, 'regulationCheck']);
    Route::get('/{management_code}/regular-contract/regulation', [RegularContractCreateController::class, 'showRegulation'])->name('regular_contract.regulation');
    Route::post('/{management_code}/regular-contract/insertRegulation', [RegularContractCreateController::class, 'insertRegulation']);
    Route::get('/{management_code}/regular-contract/input', [RegularContractCreateController::class, 'showContractForm'])->name('regular_contract.input');
    Route::post('/{management_code}/regular_contract/input/check', [RegularContractCreateController::class, 'inputCheck'])->name('regular_contract.input.check');
    Route::get('/{management_code}/regular-contract/upload_identity_create', [RegularContractCreateController::class, 'showUploadIdentityCreate'])->name('regular_contract.upload_identity_create');
    Route::post('/{management_code}/regular_contract/confirm_upload_identity/{contract_id}', [RegularContractCreateController::class, 'confirmUploadIdentity'])->name('regular_contract.confirm_upload_identity');

    // SHJ-1バッチ処理結果表示ページ
    Route::get('/{management_code}/regular-contract/upload_identity_success', [RegularContractCreateController::class, 'showUploadIdentitySuccess'])->name('regular_contract.upload_identity_success');
    Route::get('/{management_code}/regular-contract/upload_identity_fail', [RegularContractCreateController::class, 'showUploadIdentityFail'])->name('regular_contract.upload_identity_fail');
    Route::get('/{management_code}/regular_contract/create_confirm', [RegularContractCreateController::class, 'createConfirm'])->name('regular_contract.create_confirm');
    Route::post('/{management_code}/regular_contract/create_confirm_next/{contract_id}', [RegularContractCreateController::class, 'createConfirmNext'])->name('regular_contract.create_confirm_next');
    Route::get('/{management_code}/regular_contract/create_confirm_error/{contract_id}', [RegularContractCreateController::class, 'createConfirmNext'])->name('regular_contract.create_confirm_error');
    Route::post('/{management_code}/regular_contract/create_select_period', [RegularContractCreateController::class, 'selectPeriod'])->name('regular_contract.create_select_period');

    // 定期契約更新
    Route::get('/{management_code}/regular_contract/update', [RegularContractController::class, 'showInfo'])->name('regular_contract.update');
    Route::get('/{management_code}/regular_contract/update/{contract_id}', [RegularContractController::class, 'update']);
    Route::get('/{management_code}/regular_contract/confirm_category/{contract_id}', [RegularContractController::class, 'confirmCategory'])->name('regular_contract.confirm_category');
    Route::get('/{management_code}/regular_contract/confirm_category_next/{contract_id}', [RegularContractController::class, 'confirmCategoryNext'])->name('regular_contract.confirm_category_next');
    Route::get('/{management_code}/regular_contract/upload_identity/{contract_id}', [RegularContractController::class, 'uploadIdentity'])->name('regular_contract.upload_identity');
    Route::post('/{management_code}/regular_contract/upload_identity/{contract_id}', [RegularContractController::class, 'uploadIdentitySubmit'])->name('regular_contract.upload_identity_submit');
    Route::get('/{management_code}/regular_contract/select_period/{contract_id}', [RegularContractController::class, 'selectPeriod'])->name('regular_contract.select_period');
    Route::post('/{management_code}/regular_contract/update_period', [RegularContractController::class, 'updatePeriod'])->name('regular_contract.update_period');

    // 定期契約履歴
    Route::get('/{management_code}/regular_contract/history', [RegularContractController::class, 'showHistory'])->name('regular_contract.history');

    // 駐輪場検索
    Route::get('/{management_code}/park_search', [RegularContractCreateController::class, 'show'])->name('park_search');

    // 空き待ち状況確認
    Route::get('/{management_code}/park_waitlist', [ParkWaitlistController::class, 'index'])->name('park_waitlist.index');
    Route::get('/{management_code}/park-waitlist/check', [ParkWaitlistController::class, 'check'])->name('park_waitlist.check');
    Route::get('/{management_code}/park-waitlist/create', [ParkWaitlistController::class, 'create'])->name('park_waitlist.create');
    Route::get('/{management_code}/api/park-detail-wait/{reserve_id}', [ParkDetailController::class, 'showWait']);
    Route::get('/{management_code}/park_waitlist/cancel/{reserve_id}', [ParkWaitlistController::class, 'cancelConfirm'])->name('park_waitlist.cancel');
    Route::post('/{management_code}/park_waitlist/cancel/{reserve_id}', [ParkWaitlistController::class, 'cancel'])->name('park_waitlist.cancel.post');
    Route::get('/{management_code}/park_waitlist/cancel/complete', function () {
        return view('park_waitlist.cancel_complete');
    })->name('park_waitlist.cancel.complete');

    // 会員へのお知らせ
    Route::get('/{management_code}/user_information', [UserInformationController::class, 'index'])->name('user_information.index');
    Route::get('/{management_code}/user_information/history', [UserInformationController::class, 'history'])->name('user_information.history');
});
// ウェルネット決済画面（仮）
Route::get('/wellnet/payment', function (): mixed {
    $html = <<<HTML
    <!DOCTYPE html>
    <html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>ウェルネット決済画面（仮）</title>
        <style>
            body { background:#f7f7f7; font-family:sans-serif; }
            .pay-container {
                max-width:500px; margin:40px auto; background:#fff; border-radius:10px;
                box-shadow:0 2px 8px rgba(0,0,0,0.08); padding:32px 24px;
            }
            .pay-title { font-size:1.2em; font-weight:bold; margin-bottom:12px; }
            .pay-section { margin-bottom:24px; }
            .pay-row { display:flex; align-items:center; margin-bottom:8px; }
            .pay-label { width:140px; font-weight:bold; }
            .pay-icons img { height:22px; margin-right:6px; vertical-align:middle; }
            .pay-bank-icons img { height:18px; margin-right:4px; vertical-align:middle; }
            .pay-box { background:#e6f7e6; border:1px solid #7ed957; border-radius:6px; padding:8px 12px; margin-bottom:8px; }
        </style>
    </head>
    <body>
        <div class="pay-container">
            <div class="pay-section">
                <div class="pay-title">クレジットでのお支払い</div>
                <div class="pay-box">
                    <span class="pay-label">クレジット</span>
                </div>
            </div>
            <div class="pay-section">
                <div class="pay-title">コンビニでのお支払い</div>
                <div class="pay-box"><span class="pay-label">ローソン</span><span class="pay-icons"></span></div>
                <div class="pay-box"><span class="pay-label">ファミリーマート</span><span class="pay-icons"></span></div>
                <div class="pay-box"><span class="pay-label">サークルKサンクス</span><span class="pay-icons"></span></div>
                <div class="pay-box"><span class="pay-label">ミニストップ</span><span class="pay-icons"></span></div>
                <div class="pay-box"><span class="pay-label">セイコーマート</span><span class="pay-icons"></span></div>
                <div class="pay-box"><span class="pay-label">デイリーヤマザキ</span><span class="pay-icons"></span></div>
            </div>
            <div class="pay-section">
                <div class="pay-title">銀行でのお支払い</div>
                <div class="pay-box">
                    <span class="pay-label">ATM</span>
                </div>
                <div class="pay-box">
                    <span class="pay-label">ネットバンキング</span>
                </div>
            </div>
        </div>
    </body>
    </html>
    HTML;
    return $html;
})->name('wellnet.payment');
