<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Route;

class DashboardController extends Controller
{
    private function departmentDashboard(string $view, string $title): View
    {
        $user = auth()->user();

        return view($view, [
            'title' => $title,
            'user' => $user,
            'dealer' => $user->dealer ?? null,
        ]);
    }

    public function index(): View
    {
        $user = auth()->user();
        $isSystemAdmin = $user->hasSystemAdminRole();

        $dealerId = (int) ($user?->dealer_id ?? 0);
        if (!$isSystemAdmin && $dealerId === 0) {
            abort(403);
        }

        // Personal, per-user dashboard placeholder.
        // All users with access to /dashboard see the same widget layout.
        return view('dashboard.index');
    }

    public function executive(): View
    {
        return $this->departmentDashboard('dashboard.departments.executive', 'Executive Dashboard');
    }

    public function sales(): View
    {
        return $this->departmentDashboard('dashboard.departments.sales', 'Sales Dashboard');
    }

    public function buyers(): View
    {
        return $this->departmentDashboard('dashboard.departments.buyers', 'Buyers Dashboard');
    }

    public function financial(): View
    {
        return $this->departmentDashboard('dashboard.departments.financial', 'Financial Dashboard');
    }

    public function reconDepartment(): View
    {
        return $this->departmentDashboard('dashboard.departments.recon', 'Recon Dashboard');
    }

    public function marketingDepartment(): View
    {
        return $this->departmentDashboard('dashboard.departments.marketing', 'Marketing Dashboard');
    }

    public function fiDepartment(): View
    {
        return $this->departmentDashboard('dashboard.departments.fi', 'F&I Dashboard');
    }

    public function inventory(): View
    {
        return $this->departmentDashboard('dashboard.departments.inventory', 'Inventory Dashboard');
    }

    public function operations(): View
    {
        return $this->departmentDashboard('dashboard.departments.operations', 'Operations Dashboard');
    }
}
