<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\MobileApiService;
use Illuminate\Http\Request;

class MobileApiController extends Controller
{
    protected MobileApiService $mobileApiService;

    public function __construct(MobileApiService $mobileApiService)
    {
        $this->mobileApiService = $mobileApiService;
    }

    /**
     * Authenticate driver with smart card number
     * POST /api/mobile/auth/smart-card
     */
    public function authenticateBySmartCard(Request $request)
    {
        $request->validate([
            'smart_card_number' => 'required|string',
            'password' => 'required|string',
        ]);

        $result = $this->mobileApiService->authenticateDriverBySmartCard(
            $request->smart_card_number,
            $request->password
        );

        return response()->json($result, $result['success'] ? 200 : 401);
    }

    /**
     * Authenticate driver with national ID
     * POST /api/mobile/auth/national-id
     */
    public function authenticateByNationalId(Request $request)
    {
        $request->validate([
            'national_code' => 'required|string|size:10',
            'password' => 'required|string',
        ]);

        $result = $this->mobileApiService->authenticateDriverByNationalId(
            $request->national_code,
            $request->password
        );

        return response()->json($result, $result['success'] ? 200 : 401);
    }

    /**
     * Calculate shipping rates
     * POST /api/mobile/rates/calculate
     */
    public function calculateRates(Request $request)
    {
        $request->validate([
            'distance_km' => 'required|numeric|min:0.1',
            'cargo_value' => 'sometimes|numeric|min:0',
            'cargo_type_code' => 'sometimes|string',
            'fleet_type_code' => 'sometimes|string',
            'route_type_code' => 'sometimes|string',
            'weight' => 'sometimes|numeric|min:0',
            'is_night_trip' => 'sometimes|boolean',
            'is_holiday_trip' => 'sometimes|boolean',
        ]);

        $context = $request->except(['distance_km']);
        $result = $this->mobileApiService->calculateRates($request->distance_km, $context);

        return response()->json($result);
    }

    /**
     * Register havaleh (dispatch order)
     * POST /api/mobile/havaleh/register
     */
    public function registerHavaleh(Request $request)
    {
        $request->validate([
            'driver_id' => 'required|exists:users,id',
            'origin_location' => 'required|string',
            'destination_location' => 'required|string',
            'distance_km' => 'required|numeric|min:0.1',
            'cargo_type' => 'sometimes|string',
            'cargo_weight' => 'sometimes|numeric|min:0',
            'cargo_value' => 'sometimes|numeric|min:0',
        ]);

        $result = $this->mobileApiService->registerHavaleh($request->all());

        return response()->json($result, $result['success'] ? 201 : 400);
    }

    /**
     * Register bill of lading with RMTO
     * POST /api/mobile/bill/register
     */
    public function registerBill(Request $request)
    {
        $request->validate([
            'load_id' => 'required|exists:loads,id',
            'bill_number' => 'required|string|max:100',
            'serial' => 'required|string|max:50',
            'sender_name' => 'required|string',
            'sender_national_code' => 'sometimes|string|size:10',
            'receiver_name' => 'required|string',
            'receiver_national_code' => 'required|string|size:10',
            'cargo_details' => 'sometimes|string',
            'freight_amount' => 'sometimes|numeric|min:0',
            'tax_amount' => 'sometimes|numeric|min:0',
            'total_amount' => 'sometimes|numeric|min:0',
        ]);

        $result = $this->mobileApiService->registerBillOfLading($request->all());

        return response()->json($result, $result['success'] ? 201 : 400);
    }

    /**
     * Get bill of lading by tracking code
     * GET /api/mobile/bill/{trackingCode}
     */
    public function getBill(string $trackingCode)
    {
        $result = $this->mobileApiService->getBillByTrackingCode($trackingCode);

        return response()->json($result, $result['success'] ? 200 : 404);
    }

    /**
     * Reset load state for new request
     * POST /api/mobile/load/{loadId}/reset
     */
    public function resetLoad(string $loadId)
    {
        $result = $this->mobileApiService->resetLoadState($loadId);

        return response()->json($result, $result['success'] ? 200 : 400);
    }
}
