<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('layouts.app');
});

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

Route::get('/invite/accept/{token}', [\App\Http\Controllers\InvitationController::class, 'showAcceptForm'])->name('invite.accept');
Route::post('/invite/accept/{token}', [\App\Http\Controllers\InvitationController::class, 'accept'])->name('invite.accept.store');

Route::get('/freelancers/{id}', [\App\Http\Controllers\FreelancerProfileController::class, 'show'])->name('freelancer.public');

Route::get('/jobs', [\App\Http\Controllers\PublicJobController::class, 'index'])->name('jobs.index');
Route::get('/jobs/{id}', [\App\Http\Controllers\PublicJobController::class, 'show'])->name('jobs.show');

Route::middleware(['auth', 'verified', 'role:super_admin'])->prefix('admin')->group(function () {
    Route::get('/dashboard', function () {
        return view('admin.dashboard');
    });
});

Route::middleware(['auth', 'verified', 'role:company_admin|planner'])->prefix('company')->group(function () {
    Route::get('/dashboard', function () {
        return view('company.dashboard');
    });

    Route::resource('jobs', \App\Http\Controllers\JobController::class, ['as' => 'company']);
    Route::get('/jobs/{job}/applications', [\App\Http\Controllers\ApplicationController::class, 'jobApplications'])->name('company.jobs.applications');
    Route::post('/applications/{application}/respond', [\App\Http\Controllers\ApplicationController::class, 'respond'])->name('company.applications.respond');

    Route::middleware('role:company_admin')->group(function () {
        Route::get('/settings', [\App\Http\Controllers\CompanyController::class, 'settings'])->name('company.settings');
        Route::put('/settings', [\App\Http\Controllers\CompanyController::class, 'updateSettings'])->name('company.settings.update');

        Route::get('/team', [\App\Http\Controllers\TeamController::class, 'index'])->name('company.team');
        Route::post('/team/invite', [\App\Http\Controllers\TeamController::class, 'invite'])->name('company.team.invite');
        Route::post('/team/toggle-status/{id}', [\App\Http\Controllers\TeamController::class, 'toggleStatus'])->name('company.team.toggle');
        Route::post('/team/invite-resend/{id}', [\App\Http\Controllers\TeamController::class, 'resendInvite'])->name('company.team.invite.resend');
        Route::delete('/team/invite-cancel/{id}', [\App\Http\Controllers\TeamController::class, 'cancelInvite'])->name('company.team.invite.cancel');
    });
});

Route::middleware(['auth', 'verified', 'role:executor'])->prefix('freelancer')->group(function () {
    Route::get('/dashboard', function () {
        return view('freelancer.dashboard');
    });

    Route::get('/profile-setup', [\App\Http\Controllers\FreelancerProfileController::class, 'index'])->name('freelancer.profile');
    Route::post('/profile/bio', [\App\Http\Controllers\FreelancerProfileController::class, 'updateBio'])->name('freelancer.profile.bio');
    Route::post('/profile/skills', [\App\Http\Controllers\FreelancerProfileController::class, 'updateSkills'])->name('freelancer.profile.skills');
    Route::post('/profile/experience', [\App\Http\Controllers\FreelancerProfileController::class, 'updateExperience'])->name('freelancer.profile.experience');
    Route::post('/profile/portfolio', [\App\Http\Controllers\FreelancerProfileController::class, 'addPortfolio'])->name('freelancer.profile.portfolio');
    Route::delete('/profile/portfolio/{id}', [\App\Http\Controllers\FreelancerProfileController::class, 'deletePortfolio'])->name('freelancer.profile.portfolio.delete');

    Route::get('/applications', [\App\Http\Controllers\ApplicationController::class, 'myApplications'])->name('freelancer.applications.index');
    Route::post('/applications/{job_id}', [\App\Http\Controllers\ApplicationController::class, 'store'])->name('freelancer.applications.store');
    Route::delete('/applications/{id}', [\App\Http\Controllers\ApplicationController::class, 'destroy'])->name('freelancer.applications.destroy');
});

require __DIR__.'/auth.php';
