<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\CarMake;
use App\Models\CarModel;
use App\Models\CarYear;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class AdminCarController extends Controller
{
    public function index()
    {
        $makes = CarMake::withCount('models')->latest()->paginate(20);
        return view('admin.cars.index', compact('makes'));
    }

    public function create()
    {
        return view('admin.cars.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255|unique:car_makes',
            'logo' => 'nullable|image|max:2048',
            'is_active' => 'boolean',
        ]);

        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('car-makes', 'public');
        }

        $data['slug'] = Str::slug($data['name']);
        $data['is_active'] = $request->boolean('is_active', true);

        CarMake::create($data);

        return redirect()->route('admin.car-makes.index')
            ->with('success', 'Car make created successfully.');
    }

    public function edit(CarMake $carMake)
    {
        $carMake->load('models.years');
        return view('admin.cars.edit', compact('carMake'));
    }

    public function update(Request $request, CarMake $carMake)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255|unique:car_makes,name,' . $carMake->id,
            'logo' => 'nullable|image|max:2048',
            'is_active' => 'boolean',
        ]);

        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('car-makes', 'public');
        }

        $data['slug'] = Str::slug($data['name']);
        $data['is_active'] = $request->boolean('is_active', true);

        $carMake->update($data);

        return redirect()->route('admin.car-makes.index')
            ->with('success', 'Car make updated.');
    }

    public function destroy(CarMake $carMake)
    {
        $carMake->delete();
        return redirect()->route('admin.car-makes.index')
            ->with('success', 'Car make deleted.');
    }

    public function models(Request $request)
    {
        $makes = CarMake::active()->get();
        $models = CarModel::with(['make', 'years'])
            ->when($request->make_id, fn($q) => $q->where('car_make_id', $request->make_id))
            ->latest()
            ->paginate(20);

        return view('admin.cars.models', compact('makes', 'models'));
    }

    public function storeModel(Request $request)
    {
        $data = $request->validate([
            'car_make_id' => 'required|exists:car_makes,id',
            'name' => 'required|string|max:255',
            'years' => 'required|array',
            'years.*' => 'integer|min:1900|max:2030',
        ]);

        $model = CarModel::create([
            'car_make_id' => $data['car_make_id'],
            'name' => $data['name'],
            'slug' => Str::slug($data['name']) . '-' . Str::random(4),
        ]);

        foreach ($data['years'] as $year) {
            CarYear::create(['car_model_id' => $model->id, 'year' => $year]);
        }

        return back()->with('success', 'Car model added successfully.');
    }

    public function destroyModel($id)
    {
        CarModel::findOrFail($id)->delete();
        return back()->with('success', 'Car model deleted.');
    }
}
