<?php

namespace App\Api\Controllers\Data;

use App\Api\Controllers\Controller;
use App\Api\Resources\Order\OrderDataCollection;
use App\Api\Resources\User\UserDataCollection;
use App\Api\Resources\User\UserDataResource;
use Illuminate\Http\Request;
use Modules\Coupon\Http\Resources\User\UserBaseResource;
use Modules\Mall\Models\Order;
use Modules\User\Models\User;

class IndexController extends Controller
{

    public function index()
    {
        $all    = 1000;
        $sold   = Order::query()
            ->whereIn('state', [
                Order::STATUS_PAID,
                Order::STATUS_DELIVERED,
                Order::STATUS_SIGNED,
                Order::STATUS_COMPLETED,
            ])
            ->count();
        $donate = (new User())->getALlJzCount();

        $orderQuery = Order::query()->where('type', Order::TYPE_NORMAL);

        /**
         *
         */
        $amount = (clone $orderQuery)->whereIn('state', [
            Order::STATUS_PAID,
            Order::STATUS_DELIVERED,
            Order::STATUS_SIGNED,
            Order::STATUS_COMPLETED,
        ])->sum('amount');

        $data = [
            'activities' => [
                'all'     => $all,
                'donate'  => $donate,
                'residue' => bcsub($all, $donate),
            ],
            'orders'     => [
                'users' => User::query()->whereHas('orders', function ($q) {
                    $q->whereIn('state', [
                        Order::STATUS_PAID,
                        Order::STATUS_DELIVERED,
                        Order::STATUS_SIGNED,
                        Order::STATUS_COMPLETED,
                    ])->where('type', Order::TYPE_NORMAL);
                })->count(),

                'all'       => (clone $orderQuery)
                    ->whereIn('state', [
                        Order::STATUS_PAID,
                        Order::STATUS_DELIVERED,
                        Order::STATUS_SIGNED,
                        Order::STATUS_COMPLETED,
                    ])->count(),
                'paid'      => (clone $orderQuery)->where('state', Order::STATUS_PAID)->count(),
                'delivered' => (clone $orderQuery)->where('state', Order::STATUS_DELIVERED)->count(),
                'signed'    => (clone $orderQuery)->where('state', Order::STATUS_SIGNED)->count(),
                'completed' => (clone $orderQuery)->where('state', Order::STATUS_COMPLETED)->count(),
                'amount'    => floatval($amount),
            ],
            'users'      => [
                'all' => User::count(),
                'yk'  => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', 2);
                    })->count(),
                'jk'  => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', 3);
                    })->count(),
                'nk'  => User::query()
                    ->whereHas('identities', function ($q) {
                        $q->where('id', 4);
                    })->count(),
            ]
        ];

        return $this->success($data);
    }

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date: 2023/1/5 13:50
     */
    public function orders(Request $request)
    {
        $status = $request->status ?? 'all';

        $orders = Order::query()
            ->where('type', Order::TYPE_NORMAL)
            ->when($status !== 'all', function ($q) use ($status) {
                $q->where('state', $status);
            }, function ($q) {
                $q->whereIn('state', [
                    Order::STATUS_PAID,
                    Order::STATUS_DELIVERED,
                    Order::STATUS_SIGNED,
                    Order::STATUS_COMPLETED,
                ]);
            })
            ->paginate();

        return $this->success(new OrderDataCollection($orders));
    }

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date: 2023/1/5 14:13
     */
    public function users()
    {
        $users = User::query()
            ->withCount([
                'orders' => function ($q) {
                    $q->whereIn('state', [
                        Order::STATUS_PAID,
                        Order::STATUS_DELIVERED,
                        Order::STATUS_SIGNED,
                        Order::STATUS_COMPLETED,
                    ]);
                }
            ])
            ->withSum([
                'orders' => function ($q) {
                    $q->whereIn('state', [
                        Order::STATUS_PAID,
                        Order::STATUS_DELIVERED,
                        Order::STATUS_SIGNED,
                        Order::STATUS_COMPLETED,
                    ]);
                }
            ], 'amount')
            ->whereHas('orders', function ($q) {
                $q->whereIn('state', [
                    Order::STATUS_PAID,
                    Order::STATUS_DELIVERED,
                    Order::STATUS_SIGNED,
                    Order::STATUS_COMPLETED,
                ])->where('type', Order::TYPE_NORMAL);
            })
            ->paginate();
        return $this->success(new UserDataCollection($users));

    }
}
