<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\ComplaintResource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User\UserModel;
use App\Models\ComplaintModel;
use App\Models\PartnerModel;
use App\Models\Deed\InvitationCardModel;
use App\Exceptions\AlertException;
use App\Exceptions\NoticeException;
use App\Services\Complain\ComplaintHandle;
use App\Services\Complain\NoticeService;

class ComplainController extends Controller
{


    /**
     * 举报列表
     * @param  Request  $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(Request $request)
    {
        $build = ComplaintModel::when($request->filled('state'), function ($query) use ($request) {
            return $query->where('state', $request->state);
        })->when($request->filled('search'), function ($query) use ($request) {
            return $query->where('search', 'like', "%" . $request->search . "%");
        })->when($request->filled('reply'), function ($query) use ($request) {
            if ($request->reply) {
                return $query->where('reply_at', '>', 0);
            } else {
                return $query->where('reply_at', '=', 0);
            }
        });
        $complaints = $build->orderBy(
            $request->input('sort_by', 'id'),
            $request->input('order', 'asc')
        )->paginate($request->input("per_page", 10));

        foreach ($complaints as &$complaint) {
            switch ($complaint->type) {
                case "partner":
                    $partner = PartnerModel::find($complaint->value);
                    $complaint->be_data = $partner;
                    break;
                case "contact":
                    $contact = InvitationCardModel::find($complaint->value);
                    $complaint->be_data = $contact;
                    break;
                case "pair":
                    $pair = \App\Models\Fpdx\PairModel::find($complaint->value);
                    $complaint->be_data = $pair;
                    break;
                case "game":
                    $game = \App\Models\Gteam\EnterModel::find($complaint->value);
                    $complaint->be_data = $game;
                    break;
                case "goodnight":
                    $goodnight = \App\Models\Goodnight\EnterModel::find($complaint->value);
                    $complaint->be_data = $goodnight;
                    break;
                case "question":
                    $question = \App\Models\User\LikeInviteQuestionModel::find($complaint->value);
                    $question->question = json_decode($question->question, true);
                    $complaint->be_data = $question;
            }
            $complaint->user = UserModel::find($complaint->uid);
            $complaint->be_user = UserModel::find($complaint->be_uid);
        }
        return ComplaintResource::collection($complaints);
    }


    /**
     * 删除某个字段
     * @param  Request  $request
     * @return array
     */
    public function erasing(Request $request, $id)
    {
        $this->validate($request, [
            'fields' => 'required|array',
        ], [
            'field.required' => '请选择一个字段'
        ]);
        $fields = $request->fields;
        try {
            $complaint = ComplaintHandle::make($id);
            foreach ($fields as $field) {
                $complaint->setField($field);
            }
            $complaint->erasing();
        } catch (\Exception $e) {
            throw new AlertException($e);
        }

        try {
            $notice = new NoticeService();
            $notice->erasingNotice($complaint->complaint);
        } catch (NoticeException $e) {
            app('sentry')->captureException($e);
        }
        return response()->json([
            'code' => 200,
            'message' => 'OK'
        ]);
    }


    /**
     * 回复
     * @param  Request  $request
     * @return array
     * @throws \Exception
     */
    public function reply(Request $request, $id)
    {
        $this->validate($request, [
            'state' => 'required|in:1,2,3,4,5',
            'content' => 'required|max:200',
        ], [
            'state' => '请选择一种处理方式',
            'content.required' => '请写回复内容',
            'content.max' => '回复内容字数不得超出100字',
        ]);
        try {
            $complaint = ComplaintHandle::make($id);
            $complaint->state = $request->input('state');
            $complaint->reply_content = $request->input('content');
            $complaint->updateState();
            $complaint->reply();
        } catch (\Exception $e) {
            throw new AlertException($e);
        }

        $notice = new NoticeService();
        $notice->reply($complaint->complaint);
        return [
            'code' => 200,
            'message' => 'OK'
        ];
    }


    /**
     * 拉入黑名单
     * @param  Request  $request
     * @param  int  $id
     * @return AlertException|\Illuminate\Http\JsonResponse
     */
    public function defriend(Request $request, int $id)
    {
        $this->validate($request, [
            'state' => 'required|in:1,2,3',
        ], [
            'state.required' => '类型',
        ]);
        try {
            $state = $request->input('state');
            $complaint = ComplaintHandle::make($id);
            $complaint->setDefriend($state);
            $complaint->defriend();
        } catch (\Exception $e) {
            throw new AlertException($e);
        }

        $notice = new NoticeService();
        $notice->defriendNotice($complaint->complaint);
        $notice->defriendNoticeBeuser($complaint->complaint);

        return response([
            "code" => 200,
            "message" => "OK"
        ]);
    }
}
