<?php

namespace App\Http\Controllers\Api\V1\App;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Jobs\Transaction\ProcessTransaction;
use App\Jobs\Transaction\ProcessInvoice;
use App\Jobs\Transaction\ProcessDispenseRecord;

class TransactionController extends Controller
{
    /**
     * B600: Record Transaction (Asynchronous)
     */
    public function store(Request $request)
    {
        $machine = $request->get('machine');
        $data = $request->except(['machine', 'key']); // 排除 Middleware 注入物件
        $data['serial_no'] = $machine->serial_no;

        ProcessTransaction::dispatch($data);

        return response()->json([
            'success' => true,
            'code' => 200,
            'message' => 'Accepted'
        ], 202);
    }

    /**
     * B601: Record Invoice (Asynchronous)
     */
    public function recordInvoice(Request $request)
    {
        $machine = $request->get('machine');
        $data = $request->except(['machine', 'key']); // 排除 Middleware 注入物件
        $data['serial_no'] = $machine->serial_no;

        ProcessInvoice::dispatch($data);

        return response()->json([
            'success' => true,
            'code' => 200,
            'message' => 'Accepted'
        ], 202);
    }

    /**
     * B602: Record Dispense Result (Asynchronous)
     */
    public function recordDispense(Request $request)
    {
        $machine = $request->get('machine');
        $data = $request->all();
        $data['serial_no'] = $machine->serial_no;

        ProcessDispenseRecord::dispatch($data);

        return response()->json([
            'success' => true,
            'code' => 200,
            'message' => 'Accepted'
        ], 202);
    }
}
