<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use App\Services\ActivityRewardService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Exception;
use App\Models\ActivityReward as ActivityRewardModel;

class ActivityReward extends Controller
{
    public function index(): JsonResponse
    {
        try {
            $params = request()->validate([
                'page' => ['required', 'integer', 'min:1'],
                'limit' => ['required', 'integer', 'min:1'],
                'title' => ['nullable', 'string'],
                'type' => ['nullable', 'string'],
            ]);
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 10);
            $where = ActivityRewardService::getWhere($params);
            $query = ActivityRewardModel::where($where);
            $count = $query->count();
            $list = $query->orderByDesc('id')
                ->forPage($page, $limit)->get()->toArray();
            foreach ($list as &$item) {
                if (!empty($item['params'])) {
                    $item['params'] = json_decode($item['params'], true);
                }
            }
            $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);
    }


    public function store(): JsonResponse
    {
        DB::beginTransaction();
        try {
            $params = request()->validate([
                'id' => ['nullable', 'integer', 'min:1'],
                'title' => ['required', 'string', 'min:1', 'max:140'],
                'sub_title' => ['required', 'string', 'min:1', 'max:140'],
                'start_time' => ['required', 'date'],
                'end_time' => ['required', 'date', 'after_or_equal:start_time'],
                'detail_image' => ['required'],
                'pc_image' => ['required'],
                'detail_banner' => ['required'],
                'pc_banner' => ['required'],
                'status' => ['required', 'integer', 'min:0', 'max:1'],
                'type' => ['required'],
                'lang' => ['required', 'string'],
                'content' => ['nullable', 'string'],
                'params' => ['nullable', 'array'],
            ]);
            $params['params'] = empty($params['params']) ? null : json_encode($params['params']);
            ActivityRewardService::submit($params);
            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 destroy(): JsonResponse
    {
        try {
            request()->validate([
                'id' => ['required', 'integer', 'min:1'],
            ]);
            $id = request()->input('id');
            ActivityRewardService::deleteAll(['id' => $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([], '删除成功');
    }
}