<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\OperatorController;
use App\Http\Controllers\MachineController;

// Redirect root to operator login
Route::get('/', function () {
    return redirect('/login');
});

// Operator auth routes (guest only)
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showOperatorLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'operatorLogin']);
});

// Admin auth routes (guest only)
Route::middleware('guest')->prefix('admin')->group(function () {
    Route::get('/login', [LoginController::class, 'showAdminLoginForm'])->name('admin.login');
    Route::post('/login', [LoginController::class, 'adminLogin']);
});

// Authenticated logout
Route::post('/logout', [LoginController::class, 'logout'])
    ->middleware('auth')
    ->name('logout');

// Admin routes
Route::middleware(['auth', 'role:admin'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
        Route::get('/sessions', [AdminDashboardController::class, 'sessions'])->name('sessions.index');
        Route::get('/sessions/export', [AdminDashboardController::class, 'sessionExport'])->name('sessions.export');
        Route::get('/sessions/export-monthly', [AdminDashboardController::class, 'sessionMonthlyExport'])->name('sessions.export-monthly');
        Route::get('/sessions/{session}', [AdminDashboardController::class, 'sessionDetail'])->name('sessions.show');
        Route::get('/sessions/{session}/live', [AdminDashboardController::class, 'sessionLive'])->name('sessions.live');
        Route::delete('/sessions/{session}', [AdminDashboardController::class, 'sessionDestroy'])->name('sessions.destroy');
        Route::get('/sessions/{session}/export-pdf', [AdminDashboardController::class, 'sessionExportPdf'])->name('sessions.export-pdf');
        Route::post('/sessions/{session}/send-email', [AdminDashboardController::class, 'sessionSendEmail'])->name('sessions.send-email');

        Route::get('/profile', [AdminDashboardController::class, 'profileEdit'])->name('profile.edit');
        Route::put('/profile', [AdminDashboardController::class, 'profileUpdate'])->name('profile.update');

        Route::get('/operators', [OperatorController::class, 'index'])->name('operators.index');
        Route::get('/operators/create', [OperatorController::class, 'create'])->name('operators.create');
        Route::post('/operators', [OperatorController::class, 'store'])->name('operators.store');
        Route::get('/operators/{operator}/edit', [OperatorController::class, 'edit'])->name('operators.edit');
        Route::put('/operators/{operator}', [OperatorController::class, 'update'])->name('operators.update');
        Route::delete('/operators/{operator}', [OperatorController::class, 'destroy'])->name('operators.destroy');
    });

// Operator routes
Route::middleware(['auth', 'role:operator'])
    ->prefix('machine')
    ->name('machine.')
    ->group(function () {
        Route::get('/', [MachineController::class, 'index'])->name('dashboard');
        Route::post('/production', [MachineController::class, 'saveProduction'])->name('production.save');
        Route::post('/case-number', [MachineController::class, 'saveCaseNumber'])->name('case.save');
        Route::post('/run', [MachineController::class, 'run'])->name('run');
        Route::post('/pause', [MachineController::class, 'pause'])->name('pause');
        Route::post('/resume', [MachineController::class, 'resume'])->name('resume');
        Route::post('/stop', [MachineController::class, 'stop'])->name('stop');
        Route::post('/change-product', [MachineController::class, 'changeProduct'])->name('change-product');
        Route::get('/status', [MachineController::class, 'status'])->name('status');
        Route::get('/session/{session}/export-pdf', [MachineController::class, 'exportPdf'])->name('session.export-pdf');
        Route::post('/session/{session}/send-email', [MachineController::class, 'sendEmail'])->name('session.send-email');
    });
