<?php

namespace App\Http\Controllers\Admin;

use App\Models\Goodnight\TopicExampleModel;
use App\Models\Goodnight\TopicModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Gnight\UserModel;

class GnightController extends Controller
{
    /**
     * 获取正在审核的语音
     * @param  Request  $request
     * @return array
     */
    public function getCheckings(Request $request)
    {
        $page = $request->get('page') ?? 1;
        $count = UserModel::where('voice_state', 2)->count();
        $boy = UserModel::where('voice_state', 1)->where('sex', 1)->count();
        $girl = UserModel::where('voice_state', 1)->where('sex', 2)->count();
        $boy_all = UserModel::where('sex', 1)->count();
        $girl_all = UserModel::where('sex', 2)->count();
        $all = UserModel::where('sex', 0)->count();
        $voices = UserModel::where('voice_state', 2)->skip($page - 1)->take(500)->get();
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => [
                'count' => $count,
                'boy' => $boy,
                'girl' => $girl,
                'boy_all' => $boy_all,
                'girl_all' => $girl_all,
                'all' => $all,
                'limit' => 500,
                'voice' => $voices
            ]
        ]);
    }

    public function changeState(int $uid, Request $request)
    {
        $user = UserModel::findOrFail($uid);
        $user->voice_state = $request->input('voice_state');
        $user->save();
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    public function topics(Request $request)
    {
        $topics = TopicModel::when($request->filled("show"), function ($query) use ($request) {
            return $query->where("show", 1);
        })->get();
        foreach ($topics as $topic) {
            $topic->examples = TopicExampleModel::select("id", "content", "show")->where('topic_id', $topic->id)
                ->when($request->filled("show"), function ($query) use ($request) {
                    return $query->where("show", 1);
                })->get();
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $topics
        ]);
    }


    public function createTopic(Request $request)
    {
        $topic = TopicModel::create($request->all());
        return response([
            "code" => 200,
            "message" => "OK",
            "data" => $topic
        ]);
    }

    public function updateTopic(Request $request, int $id)
    {
        $topic = TopicModel::findOrFail($id);
        $topic->update($request->all());
        return response([
            "code" => 200,
            "message" => "OK"
        ]);
    }


    public function createTopicExample(Request $request)
    {
        $topicExample = TopicExampleModel::create($request->all());
        return response([
            "code" => 200,
            "message" => "OK",
            "data" => $topicExample
        ]);
    }


    public function updateTopicExample(Request $request, int $id)
    {
        $topicExample = TopicExampleModel::find($id);
        $topicExample->update($request->all());
        return response([
            "code" => 200,
            "message" => "OK"
        ]);
    }
}
