<?php

namespace App\Http\Controllers;

use App\Models\Maintenance;
use App\Models\Vehicle;
use Illuminate\Http\Request;

class MaintenanceController extends Controller
{
    public function index()
    {
        $maintenances = Maintenance::with('vehicle')->latest()->paginate(10);
        return view('maintenances.index', compact('maintenances'));
    }

    public function create()
    {
        $vehicles = Vehicle::where('status', 'active')->get();
        return view('maintenances.create', compact('vehicles'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'vehicle_id' => 'nullable|exists:vehicles,id',
            'date' => 'nullable|date',
            'description' => 'nullable|string',
            'cost' => 'nullable|numeric',
            'km' => 'nullable|numeric',
            'next_service_km' => 'nullable|numeric',
            'next_service_date' => 'nullable|date',
        ]);

        $validated['date'] = $validated['date'] ?? now()->toDateString();
        $validated['cost'] = $validated['cost'] ?? 0;
        $validated['km'] = $validated['km'] ?? 0;

        $maintenance = Maintenance::create($validated);

        // Update vehicle's service tracking
        if (!empty($validated['vehicle_id'])) {
            $vehicle = Vehicle::find($validated['vehicle_id']);
            if ($vehicle) {
                $vehicle->update([
                    'last_service_km' => $validated['km'],
                    'last_service_date' => $validated['date'],
                    'next_service_km' => $validated['next_service_km'] ?? $vehicle->next_service_km,
                    'next_service_date' => $validated['next_service_date'] ?? $vehicle->next_service_date,
                ]);
                \App\Models\ActivityLog::log('created', 'Maintenance', 'Maintenance record added for ' . $vehicle->vehicle_number);
            }
        }

        return redirect()->route('maintenances.index')->with('success', 'Maintenance record added successfully!');
    }

    public function edit(Maintenance $maintenance)
    {
        $vehicles = Vehicle::where('status', 'active')->get();
        return view('maintenances.edit', compact('maintenance', 'vehicles'));
    }

    public function update(Request $request, Maintenance $maintenance)
    {
        $validated = $request->validate([
            'vehicle_id' => 'nullable|exists:vehicles,id',
            'date' => 'nullable|date',
            'description' => 'nullable|string',
            'cost' => 'nullable|numeric',
            'km' => 'nullable|numeric',
            'next_service_km' => 'nullable|numeric',
            'next_service_date' => 'nullable|date',
        ]);

        $validated['cost'] = $validated['cost'] ?? 0;
        $validated['km'] = $validated['km'] ?? 0;

        $maintenance->update($validated);

        \App\Models\ActivityLog::log('updated', 'Maintenance', 'Maintenance record updated #' . $maintenance->id);

        return redirect()->route('maintenances.index')->with('success', 'Maintenance record updated!');
    }

    public function destroy(Maintenance $maintenance)
    {
        $maintenance->delete();
        \App\Models\ActivityLog::log('deleted', 'Maintenance', 'Maintenance record deleted #' . $maintenance->id);
        return redirect()->route('maintenances.index')->with('success', 'Maintenance record deleted.');
    }
}
