<?php

use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\TicketController as AdminTicketController;
use App\Http\Controllers\Admin\CustomerController as AdminCustomerController;
use App\Http\Controllers\Admin\InvoiceController as AdminInvoiceController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CourierController;
use App\Http\Controllers\HandoverController;
use App\Http\Controllers\InvoiceVerificationController;
use App\Http\Controllers\NotaController;
use App\Http\Controllers\ServiceTicketController;
use App\Http\Controllers\TechnicianController;
use Illuminate\Support\Facades\Route;

// ─── Public ───────────────────────────────────────────────────
Route::get('/', fn () => redirect()->route('login'));

// ─── Auth ─────────────────────────────────────────────────────
Route::get('/login',  [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login'])->name('login.post');
Route::post('/logout',[AuthController::class, 'logout'])->name('logout')->middleware('auth');

// ─── Service Tickets (customer-facing, public) ────────────────
Route::prefix('tickets')->name('tickets.')->group(function () {
    Route::get('/create',         [ServiceTicketController::class, 'create'])->name('create');
    Route::post('/',              [ServiceTicketController::class, 'store'])->name('store');
    Route::get('/success',        [ServiceTicketController::class, 'success'])->name('success');
    Route::get('/{ticket}',       [ServiceTicketController::class, 'show'])->name('show');
    Route::get('/{ticket}/label', [ServiceTicketController::class, 'label'])->name('label');
});

// ─── Nota Digital (publik) ────────────────────────────────────
Route::get('/nota/{ticketCode}',             [NotaController::class, 'show'])->name('nota.show');
Route::get('/nota/{ticketCode}/pdf',         [NotaController::class, 'pdf'])->name('nota.pdf');
Route::post('/nota/{ticketCode}/send-email', [NotaController::class, 'sendEmail'])
    ->name('nota.send-email')
    ->middleware(['auth', 'role:admin']);
Route::get('/invoice/verify/{code}', [InvoiceVerificationController::class, 'show'])->name('invoices.verify');

// ─── Admin ────────────────────────────────────────────────────
Route::prefix('admin')->name('admin.')->middleware(['auth', 'role:admin'])->group(function () {
    Route::prefix('tickets')->name('tickets.')->group(function () {
        Route::get('/',                  [AdminTicketController::class, 'index'])->name('index');
        Route::patch('/{ticket}/cancel', [AdminTicketController::class, 'cancel'])->name('cancel');
        Route::patch('/{ticket}/status', [AdminTicketController::class, 'updateStatus'])->name('status');
        Route::patch('/{ticket}/approve-repair', [AdminTicketController::class, 'approveRepair'])->name('approve-repair');
    });

    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/',       [ReportController::class, 'index'])->name('index');
        Route::get('/export', [ReportController::class, 'export'])->name('export');
    });

    Route::resource('users', UserController::class)->except(['show']);

    Route::get('settings',  [SettingController::class, 'index'])->name('settings.index');
    Route::patch('settings', [SettingController::class, 'update'])->name('settings.update');
});

Route::prefix('admin')->name('admin.')->middleware(['auth', 'role:admin,cashier'])->group(function () {
    Route::resource('customers', AdminCustomerController::class)->except(['show', 'destroy']);

    Route::prefix('invoices')->name('invoices.')->group(function () {
        Route::get('/', [AdminInvoiceController::class, 'index'])->name('index');
        Route::get('/create', [AdminInvoiceController::class, 'create'])->name('create');
        Route::post('/', [AdminInvoiceController::class, 'store'])->name('store');
        Route::get('/{invoice}', [AdminInvoiceController::class, 'show'])->name('show');
        Route::get('/{invoice}/edit', [AdminInvoiceController::class, 'edit'])->name('edit');
        Route::patch('/{invoice}', [AdminInvoiceController::class, 'update'])->name('update');
        Route::post('/{invoice}/submit-review', [AdminInvoiceController::class, 'submitReview'])->name('submit-review');
        Route::post('/{invoice}/issue', [AdminInvoiceController::class, 'issue'])->name('issue');
        Route::patch('/{invoice}/cancel', [AdminInvoiceController::class, 'cancel'])->name('cancel');
        Route::post('/{invoice}/payments', [AdminInvoiceController::class, 'addPayment'])->name('payments.store');
        Route::post('/{invoice}/credit-notes', [AdminInvoiceController::class, 'createCreditNote'])->name('credit-notes.store');
        Route::get('/{invoice}/pdf', [AdminInvoiceController::class, 'pdf'])->name('pdf');
    });
});

// ─── Teknisi ─────────────────────────────────────────────────
Route::prefix('technician')->name('technician.')->middleware(['auth', 'role:admin,technician'])->group(function () {
    Route::get('/', [TechnicianController::class, 'home'])->name('home');
    Route::get('/tickets',              [TechnicianController::class, 'index'])->name('tickets.index');
    Route::get('/scanner',               [TechnicianController::class, 'scanner'])->name('scanner');
    Route::get('/scanner/manual',        [TechnicianController::class, 'scannerManual'])->name('scanner.manual');
    Route::get('/tickets/{ticket}/book', [TechnicianController::class, 'servicebook'])->name('book');
    Route::get('/sop', fn () => view('technician.sop'))->name('sop');
    Route::post('/tickets/{ticket}/diagnosis', [TechnicianController::class, 'saveDiagnosis'])->name('diagnosis.store');
    Route::post('/tickets/{ticket}/repair', [TechnicianController::class, 'saveRepairExecution'])->name('repair.store');
    Route::post('/tickets/{ticket}/final-check', [TechnicianController::class, 'saveFinalCheck'])->name('final-check.store');
    Route::post('/tickets/{ticket}/log', [TechnicianController::class, 'addLog'])->name('log');
    Route::patch('/tickets/{ticket}/status', [TechnicianController::class, 'updateStatus'])->name('status');
});

// ─── Kurir ───────────────────────────────────────────────────
Route::prefix('courier')->name('courier.')->middleware(['auth', 'role:admin,courier'])->group(function () {
    Route::get('/pickup', [CourierController::class, 'pickup'])->name('pickup');
    Route::get('/sop', fn () => view('courier.sop'))->name('sop');
    Route::post('/pickup/{ticket}', [CourierController::class, 'confirmPickup'])->name('pickup.confirm');
    Route::get('/pickup/{ticket}/fill',  [CourierController::class, 'showFillData'])->name('pickup.fill');
    Route::post('/pickup/{ticket}/fill', [CourierController::class, 'saveFillData'])->name('pickup.fill.save');
    Route::get('/delivery', [CourierController::class, 'delivery'])->name('delivery');
    Route::get('/delivery/sales/{invoice}', [CourierController::class, 'showSaleDelivery'])->name('delivery.sales.show');
    Route::post('/delivery/sales/{invoice}', [CourierController::class, 'storeSaleDelivery'])->name('delivery.sales.store');
    Route::get('/ready', [CourierController::class, 'delivery'])->name('ready');
});

// ─── Serah Terima (Handover) ──────────────────────────────────
Route::prefix('handover')->name('handover.')->middleware(['auth', 'role:admin,courier'])->group(function () {
    Route::get('/{ticket}',  [HandoverController::class, 'show'])->name('show');
    Route::post('/{ticket}', [HandoverController::class, 'store'])->name('store');
});

