<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\AuthController;

use App\Http\Controllers\admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\admin\taskController as AdminTaskController;
use App\Http\Controllers\admin\userController as AdminUserController;
use App\Http\Controllers\admin\projectController as AdminProjectController;
use App\Http\Controllers\admin\ProfileController as AdminProfileController;
use App\Http\Controllers\admin\DomainController as AdminDomainController;

use App\Http\Controllers\user\DashboardController as UserDashboardController;
use App\Http\Controllers\user\taskController as UserTaskController;
use App\Http\Controllers\user\ProfileController as UserProfileController;
use App\Http\Controllers\user\DomainController as UserDomainController;

try {
    if (!\Illuminate\Support\Facades\Schema::hasColumn('tasks', 'link')) {
        \Illuminate\Support\Facades\Schema::table('tasks', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->string('link')->nullable();
        });
    }
    if (!\Illuminate\Support\Facades\Schema::hasColumn('tasks', 'start_date')) {
        \Illuminate\Support\Facades\Schema::table('tasks', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->date('start_date')->nullable();
        });
    }
    if (!\Illuminate\Support\Facades\Schema::hasColumn('tasks', 'end_date')) {
        \Illuminate\Support\Facades\Schema::table('tasks', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->date('end_date')->nullable();
        });
    }
    if (!\Illuminate\Support\Facades\Schema::hasColumn('tasks', 'added_by')) {
        \Illuminate\Support\Facades\Schema::table('tasks', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->string('added_by')->default('Admin');
        });
    }
    if (!\Illuminate\Support\Facades\Schema::hasColumn('tasks', 'is_verified')) {
        \Illuminate\Support\Facades\Schema::table('tasks', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->boolean('is_verified')->default(false);
        });
    }
    if (!\Illuminate\Support\Facades\Schema::hasColumn('tasks', 'type')) {
        \Illuminate\Support\Facades\Schema::table('tasks', function (\Illuminate\Database\Schema\Blueprint $table) {
            $table->string('type')->nullable();
        });
    }
} catch (\Exception $e) {
    // Ignore database exceptions during startup
}

// Route::get('/link-storage', function () {
//     \Illuminate\Support\Facades\Artisan::call('storage:link');
//     return 'Storage Link Created!';
// });

// Auth Routes

Route::get('/', [AuthController::class, 'root']);
Route::get('/login', [AuthController::class, 'loginView'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::any('/logout', [AuthController::class, 'logout'])->name('logout');

//  ADMIN ROUTES

Route::middleware(['admin'])->group(function () {
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');

    // Progress Tracker
    Route::get('/progress', [App\Http\Controllers\admin\ProgressController::class, 'index'])->name('progress.index');

    // Tasks
    Route::get('/tasks', [AdminTaskController::class, 'index'])->name('tasks.index');
    Route::get('/tasks/create', [AdminTaskController::class, 'create'])->name('tasks.create');
    Route::post('/tasks/store', [AdminTaskController::class, 'store'])->name('tasks.store');
    Route::get('/tasks/{id}/edit', [AdminTaskController::class, 'edit'])->name('tasks.edit');
    Route::post('/tasks/{id}/update', [AdminTaskController::class, 'update'])->name('tasks.update');
    Route::any('/tasks/{id}/delete', [AdminTaskController::class, 'destroy'])->name('tasks.delete');
    Route::post('/tasks/{id}/verify', [AdminTaskController::class, 'verifyTask'])->name('tasks.verify');
    Route::post('/tasks/{id}/remove-photo', [AdminTaskController::class, 'removePhoto'])->name('tasks.removePhoto');

    // Users
    Route::get('/users', [AdminUserController::class, 'index'])->name('users.index');
    Route::get('/users/{id}/viewusers', [AdminUserController::class, 'viewusers'])->name('users.viewusers');
    Route::get('/users/create', [AdminUserController::class, 'create'])->name('users.create');
    Route::post('/users/store', [AdminUserController::class, 'store'])->name('users.store');
    Route::any('/users/{id}/delete', [AdminUserController::class, 'destroy'])->name('users.delete');
    Route::get('/users/{id}/login-as', [AdminUserController::class, 'loginAs'])->name('users.loginAs');

    // Projects
    Route::get('/projects', [AdminProjectController::class, 'index'])->name('projects.index');
    Route::get('/projects/{id}/viewproject', [AdminProjectController::class, 'viewproject'])->name('projects.viewproject');
    Route::get('/projects/create', [AdminProjectController::class, 'create'])->name('projects.create');
    Route::post('/projects/store', [AdminProjectController::class, 'store'])->name('projects.store');
    Route::any('/projects/{name}/delete', [AdminProjectController::class, 'destroy'])->name('projects.delete');
    Route::post('/projects/{id}/update-status', [AdminProjectController::class, 'updateStatus'])->name('projects.update-status');

    // Profile
    Route::get('/profile', [AdminProfileController::class, 'index'])->name('profile.index');
    Route::post('/profile/update', [AdminProfileController::class, 'update'])->name('profile.update');
    Route::post('/profile/password/update', [AdminProfileController::class, 'updatePassword'])->name('profile.password.update');

    // Domains & Logins
    Route::get('/domains', [AdminDomainController::class, 'index'])->name('domains.index');
    Route::post('/domains/store', [AdminDomainController::class, 'store'])->name('domains.store');
    Route::post('/domains/{id}/update', [AdminDomainController::class, 'update'])->name('domains.update');
    Route::any('/domains/{id}/delete', [AdminDomainController::class, 'destroy'])->name('domains.delete');
    
    Route::post('/domains/logins/store', [AdminDomainController::class, 'storeLogin'])->name('domains.logins.store');
    Route::post('/domains/logins/{id}/update', [AdminDomainController::class, 'updateLogin'])->name('domains.logins.update');
    Route::any('/domains/logins/{id}/delete', [AdminDomainController::class, 'destroyLogin'])->name('domains.logins.delete');
});

// USER ROUTES

Route::middleware(['user'])->group(function () {
    Route::get('/user/dashboard', [UserDashboardController::class, 'index'])->name('user.dashboard');

    // Tasks
    Route::get('/user/tasks', [UserTaskController::class, 'index'])->name('user.tasks.index');
    Route::get('/user/tasks/create', [UserTaskController::class, 'create'])->name('user.tasks.create');
    Route::post('/user/tasks/store', [UserTaskController::class, 'store'])->name('user.tasks.store');
    Route::get('/user/tasks/{id}/edit', [UserTaskController::class, 'edit'])->name('user.tasks.edit');
    Route::post('/user/tasks/{id}/update', [UserTaskController::class, 'update'])->name('user.tasks.update');
    Route::any('/user/tasks/{id}/delete', [UserTaskController::class, 'destroy'])->name('user.tasks.delete');
     Route::post('/user/tasks/{id}/remove-photo', [UserTaskController::class, 'removePhoto'])->name('user.tasks.removePhoto');

    Route::post('/tasks/{id}/update-status', [UserTaskController::class, 'updateStatus'])->name('tasks.update-status');

    // Profile
    Route::get('/user/profile', [UserProfileController::class, 'index'])->name('user.profile.index');
    Route::post('/user/profile/update', [UserProfileController::class, 'update'])->name('user.profile.update');
    Route::get('/user/change-password', [UserProfileController::class, 'changePassword'])->name('user.change-password.index');
    Route::post('/user/change-password/update', [UserProfileController::class, 'updatePassword'])->name('user.change-password.update');

    // Domains & Logins
    Route::get('/user/domains', [UserDomainController::class, 'index'])->name('user.domains.index');
    Route::post('/user/domains/store', [UserDomainController::class, 'store'])->name('user.domains.store');
    Route::post('/user/domains/{id}/update', [UserDomainController::class, 'update'])->name('user.domains.update');
    Route::any('/user/domains/{id}/delete', [UserDomainController::class, 'destroy'])->name('user.domains.delete');
    
    Route::post('/user/domains/logins/store', [UserDomainController::class, 'storeLogin'])->name('user.domains.logins.store');
    Route::post('/user/domains/logins/{id}/update', [UserDomainController::class, 'updateLogin'])->name('user.domains.logins.update');
    Route::any('/user/domains/logins/{id}/delete', [UserDomainController::class, 'destroyLogin'])->name('user.domains.logins.delete');
});
