<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\VariationLocationDetails;

class ResolveStocksController extends Controller
{
    public function resolve()
    {
        $productss = [1,2,32];
        $createdDetails = [];

        $stocksToMove = VariationLocationDetails::where('location_id', 2)
        ->whereNotIn('product_id', $productss)
        ->get();
        
        $chunks = $stocksToMove->chunk(300);

        foreach ($chunks as $chunk) {
            $chunk->each(function ($detail) {
                $qty_available = $detail->qty_available;
                
                $d = VariationLocationDetails::create([
                    'product_id' => $detail->product_id,
                    'product_variation_id' => $detail->product_variation_id,
                    'variation_id' => $detail->variation_id,
                    'location_id' => 4,
                    'qty_available' => $detail->qty_available,
                ]);

                $createdDetails[] = $d;

                // Decrement from the location
                $detail->decrement('qty_available', $qty_available);
            });
        }

        $count = count($createdDetails);
        \Log::info("$count details created");

        return redirect('/home');
    }
}
