<?php

namespace Modules\PaymentLog\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;

use Modules\PaymentLog\Enums\PaymentLogAccStatusEnum;
use Modules\PaymentLog\Enums\PaymentLogStatusEnum;
use Modules\PaymentLog\Requests\Api\PaymentLogRequest;
use Modules\PaymentLog\Resources\Api\PaymentLogResource;
use Modules\PaymentLog\Services\PaymentLogService;
use Modules\User\Enums\UserRoleEnum;

class PaymentLogController extends Controller
{

    public function __construct(
        private PaymentLogService $paymentLogService,
    ) {
    }
    /**
     * Get Packages.
     *
     * @param Request $request
     * @return void
     */
    public function index(Request $request): void
    {
        $user=auth()->user();
        $credentials['api'] = true;
        $credentials['orderBy']['key']="created_at";
        $credentials['orderBy']['value']=$request->sort ??"asc";
        if($user->role===UserRoleEnum::PARENT->value){
          $credentials['user_id'] = $user->id;
          $credentials['acc_status'] = PaymentLogAccStatusEnum::PARENT->value;
        }
        if($user->role===UserRoleEnum::CANTEEN->value){
          $credentials['canteen_id'] = $user->canteen->id;
            $credentials['acc_status'] = PaymentLogAccStatusEnum::CANTEEN->value;
        }

        $query = $this->paymentLogService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => PaymentLogResource::class,
            "returnName" => "logs"
        ];
        ResponseService::fetchPainateDate($response);
    }

    public function create(PaymentLogRequest $request): void
    {
        $data = $request->validated();
        $data['status'] = PaymentLogStatusEnum::PLUS->value;
        if (auth()->user()->role == UserRoleEnum::CANTEEN->value)
        {
            $data['acc_status'] = PaymentLogAccStatusEnum::CANTEEN->value;
            $data['canteen_id'] = auth()->user()->canteen_id;
        }
        else
        {
            $data['acc_status'] = PaymentLogAccStatusEnum::PARENT->value;
            $data['user_id'] = auth()->user()->id;
        }
        $this->paymentLogService->create($data);

        $user=auth()->user();
        $credentials['api'] = true;
        $credentials['orderBy']['key']="created_at";
        $credentials['orderBy']['value']=$request->sort ??"asc";
        if($user->role===UserRoleEnum::PARENT->value){
            $credentials['user_id'] = $user->id;
            $credentials['acc_status'] = PaymentLogAccStatusEnum::PARENT->value;
        }
        if($user->role===UserRoleEnum::CANTEEN->value){
            $credentials['canteen_id'] = $user->canteen->id;
            $credentials['acc_status'] = PaymentLogAccStatusEnum::CANTEEN->value;
        }

        $query = $this->paymentLogService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => PaymentLogResource::class,
            "returnName" => "logs"
        ];
        ResponseService::fetchPainateDate($response);
    }


}
