<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PanelController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Middleware\AdminMiddleware;
use App\Http\Controllers\Admin\AdminController;

// Public route
Route::get('/', [PanelController::class, 'landing'])->name('landing');
Route::get('/admin', [LoginController::class, 'adminLogin'])->name('login.admin');
Route::get('/admin-login', [LoginController::class, 'adminLogin']);

// Guest auth routes
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
    Route::get('/demo-login', [LoginController::class, 'demoLogin'])->name('login.demo');
    Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('/register', [RegisterController::class, 'register']);
});

// Protected routes (requires login)
Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

    Route::get('/dashboard', [PanelController::class, 'dashboard'])->name('dashboard');
    Route::post('/tasks', [PanelController::class, 'createTask'])->name('tasks.create');
    Route::delete('/tasks/{id}', [PanelController::class, 'deleteTask'])->name('tasks.delete');
    Route::post('/tasks/{id}/restart', [PanelController::class, 'restartTask'])->name('tasks.restart');
    Route::post('/tasks/{id}/simulate', [PanelController::class, 'simulateTask'])->name('tasks.simulate');
    Route::post('/tasks/{id}/stop', [PanelController::class, 'stopTask'])->name('tasks.stop');
    Route::get('/tasks/statuses', [PanelController::class, 'getTaskStatuses'])->name('tasks.statuses');
    Route::post('/tasks/{id}/progress', [PanelController::class, 'updateTaskProgress'])->name('tasks.progress');

    Route::get('/companies', [PanelController::class, 'companies'])->name('companies.index');
    Route::get('/companies/export', [PanelController::class, 'exportCompanies'])->name('companies.export');
    Route::post('/companies/{id}/status', [PanelController::class, 'updateCompanyStatus'])->name('companies.status');

    // Admin-only routes (separate namespace, prefix, and controller)
    Route::middleware(AdminMiddleware::class)->prefix('admin')->name('admin.')->group(function () {
        Route::get('/settings', [AdminController::class, 'settings'])->name('settings');
        Route::get('/users', [AdminController::class, 'users'])->name('users');
        Route::post('/users/{id}/toggle', [AdminController::class, 'toggleRole'])->name('users.toggle');
        Route::delete('/users/{id}', [AdminController::class, 'deleteUser'])->name('users.delete');
        Route::post('/users/{id}/organization', [AdminController::class, 'assignOrganization'])->name('users.assign-organization');

        // Organizations CRUD
        Route::get('/organizations', [AdminController::class, 'organizations'])->name('organizations.index');
        Route::post('/organizations', [AdminController::class, 'createOrganization'])->name('organizations.create');
        Route::delete('/organizations/{id}', [AdminController::class, 'deleteOrganization'])->name('organizations.delete');
    });
});
