<?php

use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\ChurchServiceController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\JumuiyaController;
use App\Http\Controllers\MemberController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SacramentController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SystemUserController;
use Illuminate\Support\Facades\Route;

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

Route::middleware('auth')->group(function () {
    Route::post('logout', [AuthController::class, 'logout'])->name('logout');
    Route::get('profile', [ProfileController::class, 'show'])->name('profile.show');
    Route::put('profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('profile/password', [ProfileController::class, 'password'])->name('profile.password');

    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');

    Route::resource('jumuiyas', JumuiyaController::class);

    Route::get('members/qr-cards', [MemberController::class, 'qrCards'])->name('members.qr-cards');
    Route::post('members/{member}/regenerate-qr', [MemberController::class, 'regenerateQr'])->name('members.regenerate-qr');
    Route::resource('members', MemberController::class);

    Route::resource('services', ChurchServiceController::class);

    Route::prefix('attendance')->name('attendance.')->group(function () {
        Route::get('qr', [AttendanceController::class, 'qr'])->name('qr');
        Route::get('manual', [AttendanceController::class, 'manual'])->name('manual');
        Route::post('manual/filter', [AttendanceController::class, 'manualFilter'])->name('manual.filter');
        Route::post('mark', [AttendanceController::class, 'mark'])->name('mark');
        Route::get('records', [AttendanceController::class, 'records'])->name('records');
        Route::get('today', [AttendanceController::class, 'today'])->name('today');
        Route::get('absent', [AttendanceController::class, 'absent'])->name('absent');
    });

    Route::prefix('sacrament')->name('sacrament.')->group(function () {
        Route::get('/', [SacramentController::class, 'index'])->name('index');
        Route::get('create', [SacramentController::class, 'create'])->name('create');
        Route::post('/', [SacramentController::class, 'store'])->name('store');
        Route::get('history', [SacramentController::class, 'history'])->name('history');
        Route::get('eligible', [SacramentController::class, 'eligible'])->name('eligible');
    });

    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('attendance', [ReportController::class, 'attendance'])->name('attendance');
        Route::get('sacrament', [ReportController::class, 'sacrament'])->name('sacrament');
        Route::get('members', [ReportController::class, 'members'])->name('members');
        Route::get('jumuiya', [ReportController::class, 'jumuiya'])->name('jumuiya');
        Route::get('absent', [ReportController::class, 'absent'])->name('absent');
        Route::get('user-activity', [ReportController::class, 'userActivity'])->name('user-activity');
    });

    Route::get('users/permissions', [SystemUserController::class, 'permissions'])->name('users.permissions');
    Route::post('users/permissions/{role}', [SystemUserController::class, 'syncPermissions'])->name('users.permissions.sync');
    Route::resource('users', SystemUserController::class)->except(['show']);

    Route::prefix('settings')->name('settings.')->group(function () {
        Route::get('church-profile', [SettingsController::class, 'churchProfile'])->name('church-profile');
        Route::post('church-profile', [SettingsController::class, 'updateChurchProfile'])->name('church-profile.update');

        Route::get('user-positions', [SettingsController::class, 'userPositions'])->name('user-positions');
        Route::post('user-positions', [SettingsController::class, 'storeUserPosition'])->name('user-positions.store');
        Route::delete('user-positions/{position}', [SettingsController::class, 'destroyUserPosition'])->name('user-positions.destroy');

        Route::get('member-positions', [SettingsController::class, 'memberPositions'])->name('member-positions');
        Route::post('member-positions', [SettingsController::class, 'storeMemberPosition'])->name('member-positions.store');
        Route::delete('member-positions/{position}', [SettingsController::class, 'destroyMemberPosition'])->name('member-positions.destroy');

        Route::get('service-types', [SettingsController::class, 'serviceTypes'])->name('service-types');
        Route::post('service-types', [SettingsController::class, 'storeServiceType'])->name('service-types.store');
        Route::delete('service-types/{type}', [SettingsController::class, 'destroyServiceType'])->name('service-types.destroy');

        Route::get('sacrament-types', [SettingsController::class, 'sacramentTypes'])->name('sacrament-types');
        Route::post('sacrament-types', [SettingsController::class, 'storeSacramentType'])->name('sacrament-types.store');
        Route::delete('sacrament-types/{type}', [SettingsController::class, 'destroySacramentType'])->name('sacrament-types.destroy');

        Route::get('attendance', [SettingsController::class, 'attendanceSettings'])->name('attendance');
        Route::get('registration', [SettingsController::class, 'registrationSettings'])->name('registration');
        Route::post('registration', [SettingsController::class, 'updateRegistrationSettings'])->name('registration.update');
        Route::get('qr', [SettingsController::class, 'qrSettings'])->name('qr');
        Route::post('{group}', [SettingsController::class, 'updateSimpleSettings'])->name('simple.update');
    });
});
