<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Helpers\DashboardHelper;

class DashboardController extends Controller
{    public function index()
    {
        $allStatistics = DashboardHelper::getAllStatistics();

        $allFastestData = DashboardHelper::getAllFastestData();

        $allTerakhirTerbitData = DashboardHelper::getAllTerakhirTerbitData();

        $statuses = DashboardHelper::getStatusDataByType('pertek');
        $statuses = DashboardHelper::addTotalToStatuses($statuses);
        $type = 'pertek';

        return view('dashboard/index', compact('statuses', 'type', 'allStatistics', 'allFastestData', 'allTerakhirTerbitData'));
    }

    public function pertek()
    {
        $statuses = DashboardHelper::getStatusDataByType('pertek');
        $statuses = DashboardHelper::addTotalToStatuses($statuses);
        $type = 'pertek';

        return view('dashboard/dashboard-pertek', compact('statuses', 'type'));
    }

    public function rintek()
    {
        $statuses = DashboardHelper::getStatusDataByType('rintek');
        $statuses = DashboardHelper::addTotalToStatuses($statuses);
        $type = 'rintek';

        return view('dashboard/dashboard-rintek', compact('statuses', 'type'));
    }

    public function amdal()
    {
        $statuses = DashboardHelper::getStatusDataByType('amdal');
        $statuses = DashboardHelper::addTotalToStatuses($statuses);
        $type = 'amdal';

        return view('dashboard/dashboard-amdal', compact('statuses', 'type'));
    }

    public function bengkel()
    {
        $statuses = DashboardHelper::getStatusDataByType('uji_emisi');
        $statuses = DashboardHelper::addTotalToStatuses($statuses);
        $type = 'uji_emisi';

        return view('dashboard/dashboard-bengkel', compact('statuses', 'type'));
    }

    public function izinAngkut()
    {
        $statuses = DashboardHelper::getStatusDataByType('izin_angkut');
        $statuses = DashboardHelper::addTotalToStatuses($statuses);
        $type = 'izin_angkut';

        return view('dashboard/dashboard-izin-angkut', compact('statuses', 'type'));
    }

    public function summaryByStatus(Request $request)
    {
        try {
            $type = $request->get('type', 'pertek');
            $statuses = DashboardHelper::getStatusDataByType($type);

            if (empty($statuses)) {
                return response()->json([
                    'success' => false,
                    'message' => 'Tipe izin tidak ditemukan.',
                ], 404);
            }

            $statuses = DashboardHelper::addTotalToStatuses($statuses);

            return response()->json([
                'success'       => true,
                'message'       => 'Summary by status retrieved successfully.',
                'type'          => $type,
                'type_label'    => DashboardHelper::getTypeLabel($type),
                'data'          => $statuses,
                'chart_data'    => DashboardHelper::getChartData($type),
                'last_updated'  => now()->format('Y-m-d H:i:s'),
            ], 200);

        } catch (\Exception $e) {
            Log::error('Gagal mengambil summary status izin: ' . $e->getMessage());

            return response()->json([
                'success' => false,
                'message' => 'Terjadi kesalahan saat mengambil data.',
                'error'   => $e->getMessage(),
            ], 500);
        }
    }

    public function getAllSummary()
    {
        try {
            $allData = DashboardHelper::getAllStatistics();

            return response()->json([
                'success'       => true,
                'message'       => 'All summary data retrieved successfully.',
                'data'          => $allData,
                'last_updated'  => now()->format('Y-m-d H:i:s'),
            ], 200);

        } catch (\Exception $e) {
            Log::error('Gagal mengambil semua summary: ' . $e->getMessage());

            return response()->json([
                'success' => false,
                'message' => 'Terjadi kesalahan saat mengambil data.',
                'error'   => $e->getMessage(),
            ], 500);
        }
    }
}
