<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Vehicle;
use App\Models\AuctionVehicle;
use App\Models\Order;
use App\Services\Calculator\BangladeshLandedCostCalculator;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function getVehicles(Request $request)
    {
        $query = Vehicle::available();

        if ($request->filled('make')) {
            $query->where('make', $request->make);
        }
        if ($request->filled('body_type')) {
            $query->where('body_type', $request->body_type);
        }

        $vehicles = $query->paginate($request->get('limit', 15));

        return response()->json([
            'success' => true,
            'data' => $vehicles,
            'message' => 'Ready stock vehicles retrieved successfully.',
        ]);
    }

    public function getVehicle(string $id)
    {
        $vehicle = Vehicle::where('id', $id)->orWhere('slug', $id)->first();

        if (!$vehicle) {
            return response()->json(['success' => false, 'message' => 'Vehicle not found.'], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $vehicle,
            'message' => 'Vehicle details retrieved successfully.',
        ]);
    }

    public function getAuctions(Request $request)
    {
        $query = AuctionVehicle::with('auctionHouse')->active();

        if ($request->filled('make')) {
            $query->where('make', $request->make);
        }

        $auctions = $query->paginate($request->get('limit', 15));

        return response()->json([
            'success' => true,
            'data' => $auctions,
            'message' => 'Live auctions retrieved successfully.',
        ]);
    }

    public function getAuction(string $id)
    {
        $auction = AuctionVehicle::with('auctionHouse')->where('id', $id)->orWhere('lot_number', $id)->first();

        if (!$auction) {
            return response()->json(['success' => false, 'message' => 'Auction lot not found.'], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $auction,
            'message' => 'Auction details retrieved successfully.',
        ]);
    }

    public function calculate(Request $request, BangladeshLandedCostCalculator $calculator)
    {
        $validated = $request->validate([
            'price_jpy' => 'required|numeric|min:100000',
            'engine_cc' => 'required|integer',
            'fuel_type' => 'required|string',
            'is_hybrid' => 'nullable|boolean',
            'year' => 'required|integer',
        ]);

        $result = $calculator->calculate($validated);

        return response()->json([
            'success' => true,
            'data' => $result,
            'message' => 'Landed cost calculation computed successfully.',
        ]);
    }

    public function trackOrder(string $orderNumber)
    {
        $order = Order::with('statusHistory', 'vehicle', 'auctionVehicle')
            ->where('order_number', $orderNumber)
            ->first();

        if (!$order) {
            return response()->json(['success' => false, 'message' => 'Order not found.'], 404);
        }

        return response()->json([
            'success' => true,
            'data' => [
                'order_number' => $order->order_number,
                'status' => $order->status,
                'current_step_index' => $order->current_step_index,
                'vessel_name' => $order->vessel_name,
                'bl_number' => $order->bl_number,
                'eta_chittagong' => $order->eta_chittagong,
                'timeline' => $order->statusHistory,
            ],
            'message' => 'Order tracking data retrieved successfully.',
        ]);
    }
}
