<?php

use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\IftaQuestionController as AdminIftaQuestionController;
use App\Http\Controllers\Admin\IftaReplyController as AdminIftaReplyController;
use App\Http\Controllers\Admin\SitePageController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\IftaQuestionController;
use App\Http\Controllers\LocaleController;
use App\Http\Middleware\EnsureAdmin;
use App\Models\IftaQuestion;
use App\Support\SiteContent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/language/{locale}', LocaleController::class)->name('locale.switch');

Route::get('/', fn () => view('site.home', SiteContent::home()))->name('home');

Route::get('/about', fn () => view('site.inner', SiteContent::page('about')))->name('about');
Route::get('/departments', fn () => view('site.departments', SiteContent::page('departments')))->name('departments');
Route::get('/admissions', fn () => view('site.inner', SiteContent::page('admissions')))->name('admissions');

Route::get('/darul-ifta', function () {
    return view('site.darul-ifta', array_merge(SiteContent::shared(), [
        'fatwas' => SiteContent::fatwas(),
        'questions' => IftaQuestion::with([
            'user',
            'replies' => fn ($query) => $query
                ->where(fn ($query) => $query->where('is_approved', true)->orWhere('is_official', true))
                ->with('user')
                ->oldest(),
        ])
            ->where('locale', app()->getLocale())
            ->where('is_approved', true)
            ->latest()
            ->paginate(8),
    ]));
})->name('darul-ifta');

Route::get('/publications', fn () => view('site.publications', array_merge(SiteContent::shared(), [
    'publications' => SiteContent::publications(),
])))->name('publications');

Route::get('/contact', fn () => view('site.contact', SiteContent::shared()))->name('contact');
Route::get('/donate', fn () => view('site.donate', SiteContent::shared()))->name('donate');

Route::middleware('guest')->group(function (): void {
    Route::get('/login', [AuthController::class, 'login'])->name('login');
    Route::post('/login', [AuthController::class, 'authenticate'])->name('login.store');
    Route::get('/register', [AuthController::class, 'register'])->name('register');
    Route::post('/register', [AuthController::class, 'store'])->name('register.store');
});

Route::middleware('auth')->group(function (): void {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    Route::post('/questions', [IftaQuestionController::class, 'store'])->name('questions.store');
    Route::post('/questions/{question}/replies', [IftaQuestionController::class, 'reply'])->name('questions.replies.store');
});

Route::post('/contact', function (Request $request) {
    $request->validate([
        'name' => ['required', 'string', 'max:80'],
        'phone' => ['nullable', 'string', 'max:40'],
        'message' => ['required', 'string', 'min:10', 'max:1000'],
    ]);

    return back()->with('status', SiteContent::ui('contact_received'));
})->name('contact.store');

Route::middleware(['auth', EnsureAdmin::class])->prefix('admin')->name('admin.')->group(function (): void {
    Route::get('/', DashboardController::class)->name('dashboard');

    Route::get('/users', [UserController::class, 'index'])->name('users.index');
    Route::post('/users', [UserController::class, 'store'])->name('users.store');
    Route::put('/users/{user}', [UserController::class, 'update'])->name('users.update');

    Route::get('/pages', [SitePageController::class, 'index'])->name('pages.index');
    Route::get('/pages/{slug}/{locale}/edit', [SitePageController::class, 'edit'])->name('pages.edit');
    Route::put('/pages/{slug}/{locale}', [SitePageController::class, 'update'])->name('pages.update');

    Route::get('/questions', [AdminIftaQuestionController::class, 'index'])->name('questions.index');
    Route::put('/questions/{question}', [AdminIftaQuestionController::class, 'update'])->name('questions.update');
    Route::delete('/questions/{question}', [AdminIftaQuestionController::class, 'destroy'])->name('questions.destroy');
    Route::post('/questions/{question}/replies', [AdminIftaReplyController::class, 'store'])->name('questions.replies.store');
    Route::put('/replies/{reply}', [AdminIftaReplyController::class, 'update'])->name('replies.update');
    Route::delete('/replies/{reply}', [AdminIftaReplyController::class, 'destroy'])->name('replies.destroy');
});
