<?php

namespace App\Http\Controllers;

use App\Services\Loggers\OnlineTransactionLoggerService;
use App\Transaction;
use App\TransactionOnlineLog;
use App\TransactionOnlineStatus;
use Illuminate\Http\Request;

class TransactionOnlineLogController extends Controller
{
    public function create(Request $request)
    {
        $log = OnlineTransactionLoggerService::logs($request);

        return response()->json($log, 201);
    }

    public function index(Request $request)
    {
        $transactionOnlineLogs = TransactionOnlineLog::query()
            ->when($request->filled('date'), function ($query) use ($request) {
                return $query->whereDate('created_at', $request->date);
            })
            ->get()
            ->map(function ($x) {
                $x->status_label = TransactionOnlineStatus::statusLabel($x->status);
                return $x;
            });

        return response()->json($transactionOnlineLogs, 200);
    }
    
    public function show($bill_id)
    {
        $transactionOnlineLogs = TransactionOnlineLog::query()
            ->where('bill_id', $bill_id)
            ->get()
            ->map(function ($x) {
                $x->status_label = TransactionOnlineStatus::statusLabel($x->status);
                return $x;
            });

        return response()->json($transactionOnlineLogs, 200);
    }
}
