<?php

namespace App\Http\Controllers;

use App\Exceptions\AlertException;
use App\Http\Controllers\Core\Auth;
use App\Http\Resources\Pay\CouponCollection;
use App\Models\CouponModel;
use Illuminate\Http\Request;

class CouponController extends Controller
{
    //


    /**
     * 优惠券
     * @param  Request  $request
     * @return CouponCollection
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @deprecated Payment/GetCouponList
     */
    public function index(Request $request)
    {
        $uid = Auth::auth();
        $coupons = CouponModel::existing($uid)
            ->when($request->filled('type'), function ($query) use ($request) {
                return $query->where('type', $request->input('type'));
            })
            ->when($request->filled('goods_scope'), function ($query) use ($request) {
                return $query->where('goods_scope', $request->get('goods_scope'));
            })
            ->orderBy('get_at', 'asc')
            ->paginate($request->get('pre_page', 20));
        CouponModel::whereIn('id', $coupons->pluck('id'))->where('looked_at', 0)->update(['looked_at' => time()]);
        return new CouponCollection($coupons);
    }


    /**
     * 优惠券抵扣信息
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     * @deprecated Payment/GetCouponDetail
     */
    public function show(Request $request, $id)
    {
        $uid = Auth::auth();
        $coupon = CouponModel::find($id);

        if (!$coupon) {
            throw new AlertException("优惠券不存在或已过期");
        }

        if (!$coupon->isOwner($uid)) {
            throw new AlertException("优惠券不存在或已过期");
        }


        // 查看状态
        $coupon->look();

        if ($request->filled('goods_id')) {
            $goodsId = $request->goods_id;
            $settlement_amount = $coupon->getSettlementAmount($goodsId);

            $coupon->total_amount = $settlement_amount['total_amount'];
            $coupon->settlement_coupon_amount = $settlement_amount['settlement_coupon_amount'];
            $coupon->settlement_total_amount = $settlement_amount['settlement_total_amount'];
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $coupon,
        ]);
    }


    /**
     * 兑换优惠券
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function exchange(Request $request)
    {
        $this->validate($request, [
            'code' => 'required',
        ], [
            'code.required' => '兑换码不能为空哦',
        ]);
        $code = $request->input('code');
        $uid = Auth::auth();
        $coupon = CouponModel::where('code', $code)->first();

        if (!$coupon) {
            throw new AlertException('兑换码不正确哦');
        }
        if ($coupon->uid) {
            throw new AlertException('兑换码已被兑换');
        }
        if ($coupon->isOverdue()) {
            throw new AlertException('兑换码已过期');
        }

        $coupon->uid = $uid;
        $coupon->get_at = time();
        $coupon->save();

        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $coupon
        ]);
    }
}
