<?php

use App\Http\Controllers\Admins\DashboardController;
use App\Http\Controllers\Admins\ProfileController;
use App\Http\Controllers\Auths\AuthController;
use App\Http\Controllers\Admins\SurveyController;
use App\Http\Controllers\Admins\RespondenController;
use App\Http\Controllers\Admins\Users\UserController;
use App\Http\Controllers\Master\ManagementMasterController;
use App\Http\Controllers\Master\MasterAgamaController;
use App\Http\Controllers\Master\MasterBantuanPemerintahController;
use App\Http\Controllers\Master\MasterDisabilitasController;
use App\Http\Controllers\Master\MasterJenjangPendidikanController;
use App\Http\Controllers\Master\MasterKabupatenController;
use App\Http\Controllers\Master\MasterKecamatanController;
use App\Http\Controllers\Master\MasterKelurahanController;
use App\Http\Controllers\Master\MasterPekerjaanController;
use App\Http\Controllers\Master\MasterProvinsiController;
use App\Http\Controllers\Master\MasterStatusPerkawinanController;
use App\Http\Controllers\Master\MasterStatusTempatTinggalController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Redirect;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    // return view('welcome');
    return Redirect::to('auth/login');
});

// Authentication
Route::group(['prefix' => 'auth'], function () {
    // Login
    Route::get('login', [AuthController::class, 'login'])->name('auth.login');
    Route::post('login', [AuthController::class, 'auth_login'])->name('auth.login');

    // Register
    Route::get('register', [AuthController::class, 'register'])->name('auth.register');
    Route::post('register', [AuthController::class, 'auth_register'])->name('auth.register');

    // Logout
    Route::get('logout', [AuthController::class, 'logout'])->name('auth.logout');
});

// auth only role Admin
Route::group(['prefix' => 'admin', 'middleware' => ['role:Admin']], function () {
    // Users
    Route::get('users', [UserController::class, 'index'])->name('users.index');
    Route::get('users/create', [UserController::class, 'create'])->name('users.create');
    Route::post('users', [UserController::class, 'store'])->name('users.store');
    Route::get('users/edit/{id}', [UserController::class, 'edit'])->name('users.edit');
    Route::put('users/update/{id}', [UserController::class, 'update'])->name('users.update');
    Route::delete('users/delete/{id}', [UserController::class, 'destroy'])->name('users.delete');
});
Route::group(['prefix' => 'master', 'middleware' => ['role:Admin']], function () {

    // Management Master
    Route::get('management-master', [ManagementMasterController::class, 'index'])->name('master.management-master.index');
    Route::get('management-master/create', [ManagementMasterController::class, 'create'])->name('master.management-master.create');
    Route::post('management-master', [ManagementMasterController::class, 'store'])->name('master.management-master.store');
    Route::get('management-master/edit/{id}', [ManagementMasterController::class, 'edit'])->name('master.management-master.edit');
    Route::put('management-master/update/{id}', [ManagementMasterController::class, 'update'])->name('master.management-master.update');

    // Master Agama
    Route::get('master-agama', [MasterAgamaController::class, 'index'])->name('master.master-agama');
    // Master Jenjang Pendidikan
    Route::get('master-jenjang-pendidikan', [MasterJenjangPendidikanController::class, 'index'])->name('master.master-jenjang-pendidikan');
    // Master Disabilitas
    Route::get('master-disabilitas', [MasterDisabilitasController::class, 'index'])->name('master.master-disabilitas');
    // Master Bantuan Pemerintah
    Route::get('master-bantuan-pemerintah', [MasterBantuanPemerintahController::class, 'index'])->name('master.master-bantuan-pemerintah');
    // Master Pekerjaan
    Route::get('master-pekerjaan', [MasterPekerjaanController::class, 'index'])->name('master.master-pekerjaan');
    // Master Status Perkawinan
    Route::get('master-status-perkawinan', [MasterStatusPerkawinanController::class, 'index'])->name('master.master-status-perkawinan');
    // Master Status Tempat Tinggal
    Route::get('master-status-tempat-tinggal', [MasterStatusTempatTinggalController::class, 'index'])->name('master.master-status-tempat-tinggal');
    // Master Provinsi
    Route::get('master-provinsi', [MasterProvinsiController::class, 'index'])->name('master.master-provinsi');
    // Master Kabupaten
    Route::get('master-kabupaten', [MasterKabupatenController::class, 'index'])->name('master.master-kabupaten');
    // Master Kecamatan
    Route::get('master-kecamatan', [MasterKecamatanController::class, 'index'])->name('master.master-kecamatan');
    // Master Kelurahan
    Route::get('master-kelurahan', [MasterKelurahanController::class, 'index'])->name('master.master-kelurahan');
});

// Auth with Admin && Staff
Route::group(['prefix' => 'admin', 'middleware' => ['role:Admin|Staff']], function () {

    // Responden
    Route::get('respondens', [RespondenController::class, 'index'])->name('respondens.index');
    Route::get('respondens/create', [RespondenController::class, 'create'])->name('respondens.create');
    Route::post('respondens', [RespondenController::class, 'store'])->name('respondens.store');
    Route::get('respondens/edit/{id}', [RespondenController::class, 'edit'])->name('respondens.edit');
    Route::put('respondens/update/{id}', [RespondenController::class, 'update'])->name('respondens.update');
    Route::delete('respondens/delete/{id}', [RespondenController::class, 'destroy'])->name('respondens.delete');

    // Surveys
    Route::get('surveys/preview-admin/{id}', [SurveyController::class, 'preview_admin'])->name('admin.surveys.preview-admin');
    Route::post('surveys/save-survey/{id}', [SurveyController::class, 'saveSurvey'])->name('admin.surveys.save-survey');
    Route::post('surveys/save-question/{id}', [SurveyController::class, 'saveQuestion'])->name('admin.surveys.save-question');
    Route::post('surveys/add-question/{id}', [SurveyController::class, 'addQuestion'])->name('admin.surveys.add-question');
    Route::delete('surveys/delete-question/{id}', [SurveyController::class, 'deleteQuestion'])->name('admin.surveys.delete-question');
    Route::get('surveys/create', [SurveyController::class, 'create'])->name('admin.surveys.create');
    Route::get('surveys/{id}/edit', [SurveyController::class, 'edit'])->name('admin.surveys.edit');
    // Route::resource('surveys', SurveyController::class);
    // Route::delete('surveys/delete-survey/{id}', [SurveyController::class, 'deleteSurvey'])->name('admin.surveys.delete-survey');
});

// Auth with All role
Route::group(['prefix' => 'admin', 'middleware' => ['role:Admin|Staff|Responden']], function () {
    // Dashboard
    Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
    Route::get('coba', [DashboardController::class, 'coba'])->name('coba');
    Route::get('dashboard/total', [DashboardController::class, 'total'])->name('admin.dashboard.total');

    // Profile
    Route::get('profile', [ProfileController::class, 'index'])->name('profile.index');
    Route::post('profile', [ProfileController::class, 'update'])->name('profile.update');
    // Survey
    Route::get('surveys', [SurveyController::class, 'index'])->name('admin.surveys');
    Route::get('surveys/{id}', [SurveyController::class, 'show'])->name('admin.surveys');
    Route::get('surveys/cari-responden/{survey_id}', [SurveyController::class, "cariResponden"])->name("admin.surveys.cari-responden");
    Route::get('surveys/preview-user/{id}', [SurveyController::class, 'preview_user'])->name('admin.surveys.preview-user');

    // Search master with ajax
    Route::get('surveys/cari-master/{master}', [SurveyController::class, "cariMaster"])->name("admin.surveys.cari-master");
    Route::get('surveys/cari/provinsi', [SurveyController::class, "cariProvinsi"])->name("admin.surveys.cari.provinsi");
    Route::get('surveys/cari/kabupaten/{provId}', [SurveyController::class, "cariKabupaten"])->name("admin.surveys.cari.kabupaten");
    Route::get('surveys/cari/kecamatan/{kabId}', [SurveyController::class, "cariKecamatan"])->name("admin.surveys.cari.kecamatan");
    Route::get('surveys/cari/kelurahan/{kecId}', [SurveyController::class, "cariKelurahan"])->name("admin.surveys.cari.kelurahan");

    // Surveys (Answers)
    Route::post('surveys/question-answers', [SurveyController::class, 'addAnswers'])->name('admin.surveys.question-answers');
});
