<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use App\Models\Wallet as WalletModel;
use App\Services\ActivityUserService;
use App\Services\BalanceLogService;
use App\Services\TopUpService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Exception;
use App\Models\ActivityUser as ActivityUserModel;

class ActivityUser extends Controller
{
    public function finish(): JsonResponse
    {
        DB::beginTransaction();
        try {
            $params = request()->validate([
                'id' => ['required', 'integer', 'min:1'],
            ]);
            ActivityUserService::finish($params['id']);
            DB::commit();
        } catch (ValidationException $e) {
            DB::rollBack();
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
        } catch (Exception $e) {
            DB::rollBack();
            return $this->error($e->getCode(), $e->getMessage());
        }
        return $this->success();
    }


    //赠送金额（充值）
    public function gift()
    {
        DB::beginTransaction();
        try {
            $params = request()->validate([
                'id' => ['required', 'integer', 'min:1'],
                'amount' => ['required', 'numeric', 'min:0.01'],
                'betting_amount' => ['required', 'integer', 'min:0', 'max:9999999'],
            ]);
            ActivityUserService::gift($params['id'], $params['amount'], $params['betting_amount']);
            DB::commit();
        } catch (ValidationException $e) {
            DB::rollBack();
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
        } catch (Exception $e) {
            DB::rollBack();
            return $this->error($e->getCode(), $e->getMessage());
        }
        return $this->success();
    }

    public function index(): JsonResponse
    {
        try {
            $params = request()->validate([
                'page' => ['required', 'integer', 'min:1'],
                'limit' => ['required', 'integer', 'min:1'],
                'title' => ['nullable', 'string'],
                'member_id' => ['nullable', 'string'],
            ]);
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 10);
            $where = ActivityUserService::getWhere($params);
            $query = ActivityUserModel::where($where);
            $count = $query->count();
            $list = $query->orderBy('status')
                ->with(['member'])
                ->orderByDesc('id')
                ->forPage($page, $limit)->get()->toArray();
            foreach ($list as &$item)
            {
                $item['gift_amount'] = floatval($item['gift_amount']);
                $item['effective_betting_amount'] = floatval($item['effective_betting_amount']);
                $item['betting_amount'] = floatval($item['betting_amount']);
            }

            $result = ['total' => $count, 'data' => $list];
        } catch (ValidationException $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
        } catch (Exception $e) {
            return $this->error($e->getCode(), $e->getMessage());
        }
        return $this->success($result);
    }
}