<?php

namespace Modules\User\Http\Controllers\Api\Account;

use App\Api\Controllers\Controller;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\User\Http\Resources\Account\UserAccountLogCollection;

class LogController extends Controller
{
    /**
     * Notes:水滴
     *
     * @Author: 玄尘
     * @Date: 2022/8/18 13:57
     */
    public function score(): \Illuminate\Http\JsonResponse
    {
        $user = Api::user();
        $logs = $user->account->logs()
            ->where('type', 'score')
            ->paginate();
        $data = [
            'score' => $user->account->score,
            'logs'  => new UserAccountLogCollection($logs)
        ];
        return $this->success($data);
    }

    /**
     * Notes:现金账户
     *
     * @Author: 玄尘
     * @Date: 2022/8/19 8:37
     */
    public function balance(): \Illuminate\Http\JsonResponse
    {
        $user = Api::user();
        $logs = $user->account->logs()->where('type', 'balance')->paginate();

        $yesterday = $user->account->logs()
            ->whereDate('created_at', now()->subDay()->toDateTime())
            ->where('type', 'balance')
            ->where('amount', '>', 0)
            ->sum('amount') ?? 0;

        $withdraw = $user->withdraws()->sum('amount') ?? 0;

        $data = [
            'account' => [
                'balance'   => floatval($user->account->balance),
                'yesterday' => floatval($yesterday),
                'withdraw'  => floatval($withdraw),
            ],
            'logs'    => new UserAccountLogCollection($logs)
        ];
        return $this->success($data);
    }

}
