<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Core\Auth;
use App\Models\ConfessModel;
use App\Models\User\UserModel;
use App\Models\NoticeModel;
use App\Models\PartnerModel;
use App\Services\User\TaskService;
use Illuminate\Http\Request;
use App\Models\OrderModel;
use App\Models\Fpdx\MediaModel;

class Confess extends Controller
{
    /**
     * 计算表白费用
     * @param  int  $uid
     * @param  int  $partner_id
     * @param  int  $send_msg
     * @param  int  $receive_msg
     * @return int
     * @throws \ApiException
     * @deprecated 已弃用
     */
    private function calcFee(int $uid, int $partner_id, int $send_msg = 1, int $receive_msg = 1)
    {
        $fee = 100;
        if (
            ConfessModel::where([
            ['uid', $uid],
            ['partner_id', $partner_id],
            ['receive_state', -1]
            ])->exists()
        ) {
            throw new \ApiException("你被对方拒绝了", 101);
        }
        if (
            ConfessModel::where([
                ['uid', $uid],
                ['state', 1],
                ['partner_id', $partner_id]
            ])->count() > 2
        ) {
            throw new \ApiException('一张卡片只能表白3次', 101);
        }
        if ($send_msg == 1) {
            $fee += 10;
        }
        if ($receive_msg == 1) {
            $fee += 10;
        }
        return $fee;
    }

    /**
     * 发起表白
     * @param  Request  $request
     * @return array
     * @throws \ApiException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @deprecated
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'partner_id' => 'required',
            'nickname' => 'required',
            'media_id' => 'required',
            'content' => 'required',
            'send_self' => 'required',
            'send_msg' => 'required',
            'receive_msg' => 'required'
        ]);
        $partner_id = $request->input('partner_id');
        $uid = Auth::auth();
        $fee = $this->calcFee($uid, $partner_id, $request->input('send_msg'), $request->input('receive_msg'));
        $confessModel = new ConfessModel();
        $confess = $confessModel->fill([
            'nickname' => $request->input('nickname'),
            'media_id' => $request->input('media_id'),
            'content' => $request->input('content'),
            'send_self' => $request->input('send_self'),
            'send_msg' => $request->input('send_msg'),
            'receive_msg' => $request->input('receive_msg'),
            'state' => 0,
            'create_at' => time()
        ]);
        if (!$confess->save()) {
            throw new \ApiException("数据库异常", 505);
        }
        if ($fee > 0) { # 下单
            $orderObj = new Order();
            $order_id = $orderObj->unifyByConfess($fee, $confess->id, $request);
        }
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'order_id' => $order_id,
                'confess_id' => $confess->id
            ],
        );
    }

    /**
     * 表白支付结果处理
     * @param  OrderModel  $order
     * @return bool
     * @throws \Exception
     * @deprecated 已弃用
     */
    public function payment(OrderModel $order)
    {
        \DB::beginTransaction();
        try {
            // 获取表白记录信息
            $confess = ConfessModel::find($order->type_id);
            // 让表白生效
            $confess->state = 1;
            $confess->save();
            // 获取被表白卡片信息
            $partner = PartnerModel::find($confess->partner_id);
            // 获取被表白用户信息
            $user = UserModel::find($partner->uid);
            $ts = new TaskService();
            $ts->task($user->uid, 5);
            \DB::commit();
            return true;
        } catch (\Exception $e) {
            \DB::rollBack();
            return false;
        }
    }
}
