<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\LiveWeight;
use App\Models\Weighment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class WeightController extends Controller
{
    public function receiveWeight(Request $request)
    {
        // পাইথন ব্রিজ থেকে আসা ওজন ক্যাশে সংরক্ষণ করবে
        $request->validate(['weight' => 'required|numeric']);
        Cache::put('live_weight', $request->weight, now()->addMinutes(10));
        return response()->json(['success' => true]);
    }

    public function getLiveWeight()
    {
        // লাইভ ওজন ক্যাশ থেকে রিটার্ন করবে (পাইথন ব্রিজ এখানে ডাটা রাখবে)
        $weight = cache('current_weight', 0); 
    return response()->json(['weight' => $weight]);
    }

    public function saveFirstWeight(Request $request)
    {
        $request->validate([
            'vehicle_no' => 'required|string',
            'sender_name' => 'nullable|string',
            'receiver_name' => 'nullable|string',
            'goods_name' => 'nullable|string',
            'bags' => 'nullable|integer'
        ]);

        $weight = Cache::get('live_weight', 0);
        
        if ($weight == 0) {
            return response()->json(['success' => false, 'message' => 'No weight reading'], 400);
        }

        $weighment = Weighment::create([
            'serial_no' => Weighment::generateSerialNo(),
            'vehicle_no' => $request->vehicle_no,
            'sender_name' => $request->sender_name,
            'receiver_name' => $request->receiver_name,
            'goods_name' => $request->goods_name,
            'bags' => $request->bags ?? 0,
            'gross_weight' => $weight,
            'gross_time' => now(),
            'operator_name' => 'OPERATOR',
            'status' => 'first_done'
        ]);

        return response()->json(['success' => true, 'data' => $weighment]);
    }

    public function saveSecondWeight(Request $request)
    {
        $request->validate(['vehicle_no' => 'required|string']);

        $weighment = Weighment::where('vehicle_no', $request->vehicle_no)
                              ->where('status', 'first_done')
                              ->latest()
                              ->first();

        if (!$weighment) {
            return response()->json(['success' => false, 'message' => 'No first weight found'], 404);
        }

        $tare_weight = Cache::get('live_weight', 0);
        
        if ($tare_weight == 0) {
            return response()->json(['success' => false, 'message' => 'No weight reading'], 400);
        }

        $weighment->tare_weight = $tare_weight;
$weighment->tare_time = now();
$weighment->status = 'completed'; // এই লাইনটি যোগ করুন
$weighment->calculateNet(); // এখানে সম্ভবত সেভও করা আছে

        return response()->json(['success' => true, 'data' => $weighment]);
    }
    
    public function getStats()
{
    return response()->json([
        'todayCount' => Weighment::whereDate('created_at', today())->count(),
        'pendingCount' => Weighment::where('status', 'first_done')->count(),
        'completedCount' => Weighment::where('status', 'completed')->count(),
    ]);
}

public function checkVehicle($vehicle_no)
{
    $weighment = Weighment::where('vehicle_no', $vehicle_no)
                          ->where('status', 'first_done')
                          ->latest()
                          ->first();
    
    return response()->json([
        'exists' => $weighment ? true : false,
        'status' => $weighment ? $weighment->status : null
    ]);
}
}