<?php

namespace App\Http\Controllers;


use App\Models\IgnoredMissingLivery;
use App\Models\User;
use App\Models\UserFlight;
use App\Services\AdminService;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Inertia\Inertia;

class AdminController extends Controller
{

    public function __construct(private readonly AdminService $adminService){}

    function staticAdminData(){
        return [
            'missingLiveryCount' => $this->adminService->getMissingLiveries()->count(),
            'missingAirlineCount' => $this->adminService->getMissingAirlines()->count(),
        ];
    }

    function dashboard(){
        return inertia('Admin/Dashboard', [
            'title' => 'Admin Control Panel',
            'userCount' => User::count(),
            'oneWeekUserGrowth' => User::where('created_at', '>=', Carbon::now()->subWeek())->count(),
            'flightCount' => UserFlight::count(),
            'oneWeekFlightGrowth' => UserFlight::where('created_at', '>=', Carbon::now()->subWeek())->count(),
            'latestUser' => User::latest()->first()->name,
            ...$this->staticAdminData(),
        ]);
    }

    function reconcileMissingLiveries(){
        $missingLiveries = $this->adminService->getMissingLiveries();

        return Inertia::render('Admin/MissingLiveries', [
            'title' => $missingLiveries->count() . ' Missing Liveries',
            'missingLiveries' => $missingLiveries,
            ...$this->staticAdminData(),
        ]);
    }

    function reconcileMissingAirlines(){
        $missingAirlines = $this->adminService->getMissingAirlines()->values();

        return Inertia::render('Admin/MissingAirlines', [
            'title' => $missingAirlines->count() . ' Missing Airlines',
            'missingAirlines' => $missingAirlines,
            ...$this->staticAdminData(),
        ]);
    }

    public function ignoreMissingLivery(Request $request)
    {
        $validated = $request->validate([
            'filename' => 'required|string',
        ]);

        IgnoredMissingLivery::createOrFirst($validated);

        return back();
    }

}
