<?php

namespace App\Http\Controllers;

use App\Models\Item;
use App\Models\ProductionQuantity;
use App\Models\StickerMaster;
use Illuminate\Http\Request;
use Mpdf\Mpdf;
use Mpdf\QrCode\Output;
use Mpdf\QrCode\QrCode;

class ProductionStickerReprintController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    // public function downloadQrPdf($palletNo)
    // {
    public function downloadQrPdf(Request $request)
    {
        // $palletNo = urldecode($palletNo);
        $palletNo = urldecode($request->query('palletNo'));

        if (!$palletNo) {
            abort(400, 'palletNo is required');
        }

        $parts = explode('|', $palletNo);
        $itemCode = trim($parts[0]);
        $serialNumberRaw = isset($parts[1]) ? trim($parts[1]) : null;

        // dd($serialNumberRaw);

        if ($serialNumberRaw != null) {
            $serial = preg_replace('/\/.*/', '', $serialNumberRaw);
            $serial = trim($serial);
        } else {
            $serial = null;
        }

        $item = Item::where('code', $itemCode)->first();
        $itemId= $item->id;

        if (!$item) {
            abort(404, "Item with code {$itemCode} not found.");
        }

        $production = ProductionQuantity::where('item_id', $item->id)
            ->where('serial_number', $serial)
            ->first();

        if (!$production) {
            abort(404, "Production data for item code '{$itemCode}' with serial '{$serial}' not found.");
        }

        $productionOrder = $production->production_order ?? '';

        if(!preg_match('/\//', $palletNo)){
            if ($item->category == 'Submersible Motor')
            {
                $copies = 1;
            }
            elseif ($item->category == 'Submersible Pump')
            {
                $copies = 2;
            }
            else
            {
                $copies = 1;
            }
        }
        else
        {
            $copies = 1;

            if ($serialNumberRaw) {
                // Check if serial ends with /m or /M, optionally followed by |
                if (preg_match('/\/[mM](\|)?$/', $serialNumberRaw)) {
                    $copies = 1;
                }
                // Check if serial ends with /p or /P, optionally followed by |
                elseif (preg_match('/\/[pP](\|)?$/', $serialNumberRaw)) {
                    $copies = 1;
                }
            }
        }

        // $palletNo = preg_replace('/\/.*/', '', $palletNo);

        // 5. Generate QR Code (base64)
        $qrCode = new QrCode($palletNo);
        $output = new Output\Png();
        $qrBinary = $output->output($qrCode, 100); // 100 = size
        $qrBase64 = base64_encode($qrBinary);

        // 6. Return view
        return view('print-qr', [
            'qrBase64' => $qrBase64,
            'serial' => $serial,
            'productionOrder' => $productionOrder,
            'item' => $item,
            'copies'=> $copies,
        ]);
    }


    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
