<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Kategori;
use App\Models\User;
use App\Models\Wisata;
use Illuminate\View\View;

class DashboardController extends Controller
{
    public function __invoke(): View
    {
        $wisatas = Wisata::query()
            ->with('kategori')
            ->whereNotNull('latitude')
            ->whereNotNull('longitude')
            ->orderBy('title')
            ->get();

        return view('admin.dashboard.index', [
            'totalWisata' => Wisata::query()->count(),
            'totalUser' => User::query()->where('role', 'user')->count(),
            'totalKategori' => Kategori::query()->count(),
            'mapWisatas' => $wisatas->map(fn (Wisata $wisata): array => [
                'id' => $wisata->id,
                'title' => $wisata->title,
                'location' => $wisata->location,
                'latitude' => (float) $wisata->latitude,
                'longitude' => (float) $wisata->longitude,
                'imageUrl' => $wisata->image_url,
                'rating' => number_format((float) $wisata->rating, 1),
                'category' => $wisata->kategori?->nama ?? 'Tanpa Kategori',
                'mediaType' => $wisata->model_path ? 'ar' : ($wisata->video_path ? 'video' : 'ar'),
                'mediaLabel' => $wisata->model_path ? 'Augmented Reality' : ($wisata->video_path ? 'Video Wisata' : 'Augmented Reality'),
                'editUrl' => route('admin.wisata.edit', $wisata),
            ])->values(),
        ]);
    }
}
