<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ApplicantController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\Admin\AdminApplicantController;
use App\Http\Controllers\Admin\AdminVacancyController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminAnnouncementController;
use App\Http\Controllers\Admin\AdminSettingController;
use App\Models\Vacancy;
use App\Models\Announcement;

// Frontend (Public)
Route::get('/', function () {
    $vacancies = Vacancy::where('is_active', true)->latest()->get();
    return view('frontend.index', compact('vacancies'));
})->name('index');

Route::get('/lowongan/{vacancy}', function (Vacancy $vacancy) {
    return view('frontend.detail', compact('vacancy'));
})->name('detail');

Route::get('/pengumuman', function () {
    $announcements = Announcement::where('is_active', true)->latest()->get();
    return view('frontend.pengumuman', compact('announcements'));
})->name('pengumuman');

Route::get('/panduan/{vacancy?}', function (Vacancy $vacancy = null) { return view('frontend.panduan', compact('vacancy')); })->name('panduan');
Route::get('/faq', function () { return view('frontend.faq'); })->name('faq');

// Auth Pelamar (Guest only)
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.post');
    Route::get('/daftar', [AuthController::class, 'showRegister'])->name('daftar');
    Route::post('/daftar', [AuthController::class, 'register'])->name('daftar.post');
});
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Form Lamar (Harus Login)
Route::middleware('auth')->group(function () {
    Route::get('/form/{vacancy}', function (Vacancy $vacancy) {
        if (\App\Models\Applicant::where('user_id', auth()->id())->where('vacancy_id', $vacancy->id)->exists()) {
            return redirect()->route('index')->withErrors(['vacancy' => 'Anda sudah melamar posisi ini.']);
        }
        return view('frontend.form', compact('vacancy'));
    })->name('form');
    Route::post('/apply', [ApplicantController::class, 'store'])->name('apply.store');
});

// Admin Redirect & Login
Route::get('/admin', function () {
    return redirect()->route('admin.dashboard');
});

Route::get('/admin/login', function () {
    if (auth()->check()) {
        if (auth()->user()->email === 'admin@admin.com') {
            return redirect()->route('admin.dashboard');
        }
        auth()->logout();
        request()->session()->invalidate();
        request()->session()->regenerateToken();
    }
    return view('admin.login');
})->name('admin.login');

Route::post('/admin/login', function (\Illuminate\Http\Request $request) {
    $request->validate(['email' => 'required|email', 'password' => 'required']);

    if (\Illuminate\Support\Facades\Auth::attempt($request->only('email', 'password'))) {
        $request->session()->regenerate();
        return redirect()->route('admin.dashboard');
    }

    return back()->withErrors(['email' => 'Email atau password salah.'])->withInput($request->only('email'));
})->name('admin.login.post');

// Admin Panel
Route::prefix('panel')->middleware(['auth', \App\Http\Middleware\EnsureAdmin::class])->group(function () {
    Route::get('/', [AdminDashboardController::class, 'index'])->name('admin.dashboard');

    // Route to serve files without storage symlink
    Route::get('/file/{path}', function ($path) {
        $filePath = storage_path('app/public/' . $path);
        if (file_exists($filePath)) {
            return response()->file($filePath);
        }
        abort(404, 'File tidak ditemukan.');
    })->where('path', '.*')->name('admin.file');
    Route::post('/logout', function () {
        auth()->logout();
        request()->session()->invalidate();
        request()->session()->regenerateToken();
        return redirect()->route('admin.login');
    })->name('admin.logout');

    // Pelamar
    Route::get('/applicants/export', [AdminApplicantController::class, 'export'])->name('admin.applicants.export');
    Route::get('/applicants/{applicant}/export', [AdminApplicantController::class, 'exportSingle'])->name('admin.applicants.export-single');
    Route::resource('/applicants', AdminApplicantController::class)->names('admin.applicants');

    // Lowongan
    Route::resource('/vacancies', AdminVacancyController::class)->names('admin.vacancies');

    // Pengumuman
    Route::resource('/announcements', AdminAnnouncementController::class)->names('admin.announcements')->except(['show']);

    // Pengaturan
    Route::get('/settings', [AdminSettingController::class, 'index'])->name('admin.settings.index');
    Route::post('/settings', [AdminSettingController::class, 'update'])->name('admin.settings.update');
});
