<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Admin\{
    DashboardController, TicketController, ClientController,
    VisitController, InvoiceController, WorkOrderController,
    ProjectController, ReportController, SettingController,
    UserController, CsatController, IspController, VisitChecklistController,
    KnowledgeBaseController
};
use App\Http\Controllers\Client\{PortalController, ClientLoginController};

// ── AUTH ─────────────────────────────────────────────
Route::get('/login', [LoginController::class,'showLogin'])->name('login');
Route::post('/login', [LoginController::class,'login'])->name('login.post');
Route::post('/logout', [LoginController::class,'logout'])->name('logout');
Route::get('/2fa', [LoginController::class,'show2fa'])->name('2fa');
Route::post('/2fa', [LoginController::class,'verify2fa'])->name('2fa.verify');

// ── CSAT (public) ────────────────────────────────────
Route::get('/csat/{token}', [CsatController::class,'show'])->name('csat.show');
Route::post('/csat/{token}', [CsatController::class,'submit'])->name('csat.submit');

// ── CLIENT PORTAL ────────────────────────────────────
Route::prefix('portal')->name('client.')->group(function () {
    Route::get('/login', [ClientLoginController::class,'show'])->name('login');
    Route::post('/login', [ClientLoginController::class,'login'])->name('login.post');
    Route::post('/logout', [ClientLoginController::class,'logout'])->name('logout');
    Route::middleware('auth.client')->group(function () {
        Route::get('/', [PortalController::class,'dashboard'])->name('dashboard');
        Route::get('/tickets', [PortalController::class,'tickets'])->name('tickets');
        Route::get('/tickets/create', [PortalController::class,'createTicket'])->name('tickets.create');
        Route::post('/tickets', [PortalController::class,'storeTicket'])->name('tickets.store');
        Route::get('/tickets/{ticket}', [PortalController::class,'showTicket'])->name('tickets.show');
        Route::post('/tickets/{ticket}/comment', [PortalController::class,'addComment'])->name('tickets.comment');
        Route::get('/invoices', [PortalController::class,'invoices'])->name('invoices');
    });
});

// ── ADMIN PANEL ──────────────────────────────────────
Route::middleware('auth')->prefix('admin')->name('admin.')->group(function () {
    Route::get('/', [DashboardController::class,'index'])->name('dashboard');

    Route::middleware('can.do:ticket.*')->group(function () {
        Route::get('/tickets', [TicketController::class,'index'])->name('tickets.index');
        Route::get('/tickets/create', [TicketController::class,'create'])->name('tickets.create');
        Route::post('/tickets', [TicketController::class,'store'])->name('tickets.store');
        Route::get('/tickets/{ticket}', [TicketController::class,'show'])->name('tickets.show');
        Route::get('/tickets/{ticket}/edit', [TicketController::class,'edit'])->name('tickets.edit');
        Route::put('/tickets/{ticket}', [TicketController::class,'update'])->name('tickets.update');
        Route::post('/tickets/{ticket}/comment', [TicketController::class,'addComment'])->name('tickets.comment');
        Route::post('/tickets/{ticket}/status', [TicketController::class,'changeStatus'])->name('tickets.status');
        Route::post('/tickets/{ticket}/assign', [TicketController::class,'assign'])->name('tickets.assign');
        Route::post('/tickets/{ticket}/attach-visit', [TicketController::class,'attachVisit'])->name('tickets.attach_visit');
        Route::post('/tickets/{ticket}/attachment', [TicketController::class,'uploadAttachment'])->name('tickets.attachment');
        Route::post('/tickets/{ticket}/csat/send', [TicketController::class,'sendCsat'])->name('tickets.csat.send');
        Route::delete('/tickets/{ticket}', [TicketController::class,'destroy'])->name('tickets.destroy')->middleware('role:admin');
    });

    Route::middleware('can.do:client.*')->group(function () {
        Route::get('/clients', [ClientController::class,'index'])->name('clients.index');
        Route::get('/clients/create', [ClientController::class,'create'])->name('clients.create');
        Route::post('/clients', [ClientController::class,'store'])->name('clients.store');
        Route::get('/clients/{client}', [ClientController::class,'show'])->name('clients.show');
        Route::get('/clients/{client}/edit', [ClientController::class,'edit'])->name('clients.edit');
        Route::put('/clients/{client}', [ClientController::class,'update'])->name('clients.update');
        Route::delete('/clients/{client}', [ClientController::class,'destroy'])->name('clients.destroy')->middleware('role:admin');
        Route::get('/clients/{client}/users', [ClientController::class,'users'])->name('clients.users');
        Route::post('/clients/{client}/users', [ClientController::class,'storeUser'])->name('clients.users.store');
        Route::delete('/clients/{client}/users/{user}', [ClientController::class,'destroyUser'])->name('clients.users.destroy')->middleware('role:admin');
        Route::get('/clients/{client}/devices', [ClientController::class,'devices'])->name('clients.devices');
        Route::post('/clients/{client}/devices', [ClientController::class,'storeDevice'])->name('clients.devices.store');
        Route::delete('/clients/{client}/devices/{device}', [ClientController::class,'destroyDevice'])->name('clients.devices.destroy')->middleware('role:admin');
        Route::get('/clients/{client}/isp', [IspController::class,'index'])->name('clients.isp');
        Route::post('/clients/{client}/isp', [IspController::class,'store'])->name('clients.isp.store');
        Route::post('/clients/{client}/isp/{isp}/incident', [IspController::class,'incident'])->name('clients.isp.incident');
        // AJAX API endpoints
        Route::get('/clients/{client}/visits', [ClientController::class,'getVisitsJson']);
    });

    Route::get('/knowledgebase', [KnowledgeBaseController::class,'index'])->name('knowledgebase.index');

    Route::middleware('can.do:visit.*')->group(function () {
        Route::get('/visits', [VisitController::class,'index'])->name('visits.index');
        Route::get('/visits/calendar', [VisitController::class,'calendar'])->name('visits.calendar');
        Route::get('/visits/calendar/events', [VisitController::class,'calendarEvents'])->name('visits.calendar.events');
        Route::get('/visits/create', [VisitController::class,'create'])->name('visits.create');
        Route::post('/visits', [VisitController::class,'store'])->name('visits.store');
        Route::get('/visits/dashboard', [\App\Http\Controllers\Admin\VisitDashboardController::class,'index'])->name('visits.dashboard');
        Route::get('/visits/{visit}', [VisitController::class,'show'])->name('visits.show');
        Route::get('/visits/{visit}/prepare-form', [VisitChecklistController::class,'form'])->name('visits.preparation.form');
        Route::get('/visits/{visit}/checkin-form', [VisitChecklistController::class,'showCheckin'])->name('visits.checkin.form');
        Route::post('/visits/{visit}/checkin', [VisitChecklistController::class,'storeCheckin'])->name('visits.checkin');
        Route::get('/visits/{visit}/work-form', [VisitChecklistController::class,'showWork'])->name('visits.work.form');
        Route::post('/visits/{visit}/before-photo', [VisitChecklistController::class,'storeBeforePhoto'])->name('admin.visits.before.photo');
        Route::post('/visits/{visit}/logs', [VisitChecklistController::class,'addWorkLog'])->name('admin.visits.logs');
        Route::post('/visits/{visit}/after-photo', [VisitChecklistController::class,'storeAfterPhoto'])->name('admin.visits.after.photo');
        Route::get('/visits/{visit}/pre-pulang', [VisitChecklistController::class,'showPrePulang'])->name('admin.visits.prepulang.form');
        Route::post('/visits/{visit}/pre-pulang', [VisitChecklistController::class,'submitPrePulang'])->name('admin.visits.prepulang.submit');
        Route::put('/visits/{visit}', [VisitController::class,'update'])->name('visits.update');
        Route::post('/visits/{visit}/status', [VisitController::class,'changeStatus'])->name('visits.status');
        Route::post('/visits/{visit}/remind', [VisitController::class,'sendReminder'])->name('visits.remind');
        Route::post('/visits/{visit}/tickets', [VisitController::class,'attachTicket'])->name('visits.tickets.attach');
        Route::delete('/visits/{visit}', [VisitController::class,'destroy'])->name('visits.destroy')->middleware('role:admin');
        // Visit preparation endpoints (H-1 preparation)
        Route::get('/visits/{visit}/preparation', [VisitChecklistController::class,'showPreparation'])->name('visits.preparation.show');
        Route::post('/visits/{visit}/preparation', [VisitChecklistController::class,'storePreparation'])->name('visits.preparation.store');
    });

    Route::middleware('can.do:invoice.*')->group(function () {
        Route::get('/invoices', [InvoiceController::class,'index'])->name('invoices.index');
        Route::get('/invoices/create', [InvoiceController::class,'create'])->name('invoices.create');
        Route::post('/invoices', [InvoiceController::class,'store'])->name('invoices.store');
        Route::get('/invoices/{invoice}', [InvoiceController::class,'show'])->name('invoices.show');
        Route::get('/invoices/{invoice}/pdf', [InvoiceController::class,'pdf'])->name('invoices.pdf');
        Route::post('/invoices/{invoice}/send', [InvoiceController::class,'send'])->name('invoices.send');
        Route::post('/invoices/{invoice}/payment', [InvoiceController::class,'recordPayment'])->name('invoices.payment');
        Route::delete('/invoices/{invoice}', [InvoiceController::class,'destroy'])->name('invoices.destroy')->middleware('role:admin');
        Route::get('/invoices/{invoice}/edit', [InvoiceController::class,'edit'])->name('invoices.edit');
        Route::put('/invoices/{invoice}', [InvoiceController::class,'update'])->name('invoices.update');
    });

    Route::middleware('can.do:workorder.*')->group(function () {
        Route::get('/workorders', [WorkOrderController::class,'index'])->name('workorders.index');
        Route::get('/workorders/create', [WorkOrderController::class,'create'])->name('workorders.create');
        Route::post('/workorders', [WorkOrderController::class,'store'])->name('workorders.store');
        Route::get('/workorders/{workorder}', [WorkOrderController::class,'show'])->name('workorders.show');
        Route::get('/workorders/{workorder}/edit', [WorkOrderController::class,'edit'])->name('workorders.edit');
        Route::put('/workorders/{workorder}', [WorkOrderController::class,'update'])->name('workorders.update');
        Route::delete('/workorders/{workorder}', [WorkOrderController::class,'destroy'])->name('workorders.destroy')->middleware('role:admin');
        Route::post('/workorders/{workorder}/status', [WorkOrderController::class,'changeStatus'])->name('workorders.status');
        Route::post('/workorders/{workorder}/photo', [WorkOrderController::class,'uploadPhoto'])->name('workorders.photo');
    });

    Route::middleware('can.do:project.*')->group(function () {
        Route::get('/projects', [ProjectController::class,'index'])->name('projects.index');
        Route::get('/projects/create', [ProjectController::class,'create'])->name('projects.create');
        Route::post('/projects', [ProjectController::class,'store'])->name('projects.store');
        Route::get('/projects/{project}', [ProjectController::class,'show'])->name('projects.show');
        Route::get('/projects/{project}/edit', [ProjectController::class,'edit'])->name('projects.edit');
        Route::put('/projects/{project}', [ProjectController::class,'update'])->name('projects.update');
        Route::delete('/projects/{project}', [ProjectController::class,'destroy'])->name('projects.destroy')->middleware('role:admin');
        Route::post('/projects/{project}/status', [ProjectController::class,'changeStatus'])->name('projects.status');
    });

    Route::middleware('can.do:report.*')->group(function () {
        Route::get('/reports', [ReportController::class,'index'])->name('reports.index');
        Route::get('/reports/tickets', [ReportController::class,'tickets'])->name('reports.tickets');
        Route::get('/reports/sla', [ReportController::class,'sla'])->name('reports.sla');
        Route::get('/reports/csat', [ReportController::class,'csat'])->name('reports.csat');
        Route::get('/reports/revenue', [ReportController::class,'revenue'])->name('reports.revenue');
        Route::get('/reports/technician', [ReportController::class,'technician'])->name('reports.technician');
    });

    // Knowledge base: allow admin and technician to manage articles
    Route::middleware('role:admin,technician')->group(function () {
        Route::resource('knowledgebase', KnowledgeBaseController::class)->except(['index']);
    });

    Route::middleware('role:admin')->group(function () {
        Route::post('/users/test-telegram', [UserController::class,'testTelegram'])->name('users.testTelegram');
        Route::post('/users/test-whatsapp', [UserController::class,'testWhatsapp'])->name('users.testWhatsapp');
        Route::resource('users', UserController::class);
        Route::post('/users/{user}/2fa/reset', [UserController::class,'reset2fa'])->name('users.2fa.reset');
    });

    Route::middleware('can.do:settings.*')->group(function () {
        Route::get('/settings', [SettingController::class,'index'])->name('settings.index');
        Route::post('/settings', [SettingController::class,'update'])->name('settings.update');
        Route::get('/settings/sla', [SettingController::class,'sla'])->name('settings.sla');
        Route::post('/settings/sla', [SettingController::class,'updateSla'])->name('settings.sla.update');
        Route::get('/settings/activity-log', [SettingController::class,'activityLog'])->name('settings.activity');
    });
});

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