<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\SalesController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\NotificationController;

// Halaman Utama: Logic Redirect berdasarkan Role
Route::get('/', function () {
    if (!auth()->check()) return redirect('/login');
    
    $role = auth()->user()->role;
    if ($role == 'gudang') return redirect()->route('gudang.index');
    if ($role == 'admin') return redirect()->route('admin.dashboard');
    
    return redirect()->route('sales.index');
});

// Route Login & Logout
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// Wilayah API (public)
Route::get('/api/wilayah/{type}/{id?}', function($type, $id = null) {
    $base = 'https://emsifa.github.io/api-wilayah-indonesia/api';
    $url  = $id ? "{$base}/{$type}/{$id}.json" : "{$base}/{$type}.json";
    $data = file_get_contents($url);
    return response($data)->header('Content-Type', 'application/json');
});

// Area Terproteksi (Semua User Harus Login)
Route::middleware(['auth'])->group(function () {

    // --- FITUR CS & ADMIN: INPUT & KELOLA DATA ---
    Route::middleware(['checkrole:admin,cs'])->group(function () {
        Route::get('/input', [SalesController::class, 'index'])->name('sales.index');
        Route::post('/simpan', [SalesController::class, 'store'])->name('sales.store');
        Route::get('/edit/{id}', [SalesController::class, 'edit'])->name('sales.edit');
        Route::put('/update/{id}', [SalesController::class, 'update'])->name('sales.update');
        Route::delete('/hapus/{id}', [SalesController::class, 'destroy'])->name('sales.destroy');
        Route::get('/cs/lengkapi/{id}', [SalesController::class, 'csLengkapi'])->name('cs.lengkapi');
        Route::put('/cs/lengkapi/{id}', [SalesController::class, 'csLengkapiUpdate'])->name('cs.lengkapiUpdate');

        // Invoice & Surat Jalan (CS bisa lihat)
        Route::get('/invoice/{id}', [SalesController::class, 'invoice'])->name('invoice.show');
        Route::get('/surat-jalan/{id}', [SalesController::class, 'suratJalan'])->name('invoice.suratjalan');
    });

    // --- FITUR GUDANG & ADMIN: UPDATE STATUS ---
    Route::middleware(['checkrole:admin,gudang'])->group(function () {
        Route::get('/pesanan', [SalesController::class, 'orderList'])->name('gudang.index');
        Route::post('/pesanan/update-status', [SalesController::class, 'updateStatus'])->name('gudang.updateStatus');
        Route::get('/pesanan/lengkapi/{id}', [SalesController::class, 'lengkapi'])->name('gudang.lengkapi');
        Route::put('/pesanan/lengkapi/{id}', [SalesController::class, 'lengkapiUpdate'])->name('gudang.lengkapiUpdate');
    });

    // --- KHUSUS ADMIN: DASHBOARD SUMMARY + KELOLA USER & PRODUK ---
    Route::middleware(['checkrole:admin'])->group(function () {
        Route::get('/dashboard', [SalesController::class, 'dashboard'])->name('admin.dashboard');
        Route::get('/admin/pengguna', [AdminController::class, 'penggunaIndex'])->name('admin.pengguna');
        Route::post('/admin/pengguna', [AdminController::class, 'penggunaStore']);
        Route::put('/admin/pengguna/{id}', [AdminController::class, 'penggunaUpdate']);
        Route::delete('/admin/pengguna/{id}', [AdminController::class, 'penggunaDestroy']);
        Route::get('/admin/products', [AdminController::class, 'productIndex'])->name('admin.products');
        Route::post('/admin/products', [AdminController::class, 'productStore']);
        Route::put('/admin/products/{id}', [AdminController::class, 'productUpdate']);
        Route::delete('/admin/products/{id}', [AdminController::class, 'productDestroy']);
    });

    // --- LAPORAN GMV: Admin & CS bisa lihat ---
    Route::middleware(['checkrole:admin,cs'])->group(function () {
        Route::get('/laporan', [LaporanController::class, 'index'])->name('laporan.index');
        Route::get('/laporan/export', [LaporanController::class, 'exportExcel'])->name('laporan.export');
        Route::get('/laporan/export-csv', [LaporanController::class, 'exportCsv'])->name('laporan.exportCsv');
        Route::get('/laporan/detail', [LaporanController::class, 'detail'])->name('laporan.detail');
        Route::get('/laporan/daily', [LaporanController::class, 'detailDaily'])->name('laporan.daily');
    });

    // --- NOTIFIKASI ---
    Route::get('/notif', [NotificationController::class, 'index'])->name('notif.index');
    Route::post('/notif/read-all', [NotificationController::class, 'readAll'])->name('notif.readAll');
    Route::post('/notif/read/{id}', [NotificationController::class, 'read'])->name('notif.read');
});