<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Group;
use App\Models\GroupRegistrationPackage;
use App\Models\GroupUser;
use App\Models\UserCommission;
use App\User;
use Illuminate\Http\Request;

class GroupController extends Controller
{
    public function index(Request $request)
    {
        $this->authorize('admin_group_list');

        $groups = Group::query();
        $filters = $request->input('filters');

        if (isset($filters['group_name'])) {
            $groups = $groups->where('name', 'like', '%' . $filters['group_name'] . '%');
        }

        $data = [
            'pageTitle' => trans('admin/pages/groups.group_list_page_title'),
            'groups' => $groups->paginate(10),
            'group_name' => $filters['group_name'] ?? '',
        ];

        return view('admin.users.groups.lists', $data);
    }

    public function create()
    {
        $this->authorize('admin_group_create');

        $data = [
            'pageTitle' => trans('admin/main.group_new_page_title'),
        ];

        return view('admin.users.groups.new', $data);
    }

    public function store(Request $request)
    {
        $this->authorize('admin_group_create');

        $this->validate($request, [
            'users' => 'nullable|array',
            'name' => 'required',
        ]);

        $data = $request->all();
        $data['created_at'] = time();
        $data['creator_id'] = auth()->user()->id;
        unset($data['_token']);

        $group = Group::create($data);

        $users = $request->get('users');

        if (!empty($users)) {
            foreach ($users as $userId) {
                if (GroupUser::where('user_id', $userId)->first()) {
                    continue;
                }

                GroupUser::create([
                    'group_id' => $group->id,
                    'user_id' => $userId,
                    'created_at' => time(),
                ]);

                $notifyOptions = [
                    '[u.g.title]' => $group->name,
                ];
                sendNotification('change_user_group', $notifyOptions, $userId);
                sendNotification('add_to_user_group', $notifyOptions, $userId);
            }
        }

        $toastData = [
            'title' => trans('public.request_success'),
            'msg' => trans('update.user_group_created_successful'),
            'status' => 'success'
        ];
        return redirect(getAdminPanelUrl("/users/groups/{$group->id}/edit"))->with(['toast' => $toastData]);
    }

    public function edit($id)
    {
        $this->authorize('admin_group_edit');

        $group = Group::findOrFail($id);

        $userGroups = GroupUser::where('group_id', $id)
            ->with(['user' => function ($query) {
                $query->select('id', 'full_name');
            }])
            ->get();

        $data = [
            'pageTitle' => trans('admin/pages/groups.edit_page_title'),
            'group' => $group,
            'userGroups' => $userGroups,
            'groupRegistrationPackage' => $group->groupRegistrationPackage
        ];

        return view('admin.users.groups.new', $data);
    }

    public function update(Request $request, $id)
    {
        $this->authorize('admin_group_edit');

        $this->validate($request, [
            'users' => 'nullable|array',
            'percent' => 'nullable',
            'name' => 'required',
        ]);

        $group = Group::findOrFail($id);
        $data = $request->all();

        // Store Commissions
        $this->storeUserCommissions($group, $data);

        unset($data['_token'], $data['commissions']);

        $group->update($data);

        $users = $request->get('users');

        $group->groupUsers()->delete();

        if (!empty($users)) {
            foreach ($users as $userId) {
                GroupUser::create([
                    'group_id' => $group->id,
                    'user_id' => $userId,
                    'created_at' => time(),
                ]);

                $notifyOptions = [
                    '[u.g.title]' => $group->name,
                ];
                sendNotification('change_user_group', $notifyOptions, $userId);
                sendNotification('add_to_user_group', $notifyOptions, $userId);
            }
        }

        $toastData = [
            'title' => trans('public.request_success'),
            'msg' => trans('update.user_group_updated_successful'),
            'status' => 'success'
        ];
        return redirect(getAdminPanelUrl("/users/groups/{$group->id}/edit"))->with(['toast' => $toastData]);
    }

    private function storeUserCommissions($group, $data)
    {
        $group->commissions()->delete();

        if (!empty($data['commissions'])) {
            $insert = [];

            foreach ($data['commissions'] as $source => $commission) {
                if (!empty($commission['type']) and !empty($commission['value'])) {
                    $value = $commission['value'];

                    if ($commission['type'] == "fixed_amount") {
                        $value = convertPriceToDefaultCurrency($value);
                    }

                    $insert[] = [
                        'user_id' => null,
                        'user_group_id' => $group->id,
                        'source' => $source,
                        'type' => $commission['type'],
                        'value' => $value,
                    ];
                }
            }

            if (!empty($insert)) {
                UserCommission::query()->insert($insert);
            }
        }
    }

    public function destroy(Request $request, $id)
    {
        $this->authorize('admin_group_delete');

        Group::find($id)->delete();

        $toastData = [
            'title' => trans('public.request_success'),
            'msg' => trans('update.user_group_deleted_successful'),
            'status' => 'success'
        ];
        return redirect(getAdminPanelUrl("/users/groups"))->with(['toast' => $toastData]);
    }

    public function groupRegistrationPackage(Request $request, $id)
    {
        $this->validate($request, [
            'instructors_count' => 'nullable|numeric',
            'students_count' => 'nullable|numeric',
            'courses_capacity' => 'nullable|numeric',
            'courses_count' => 'nullable|numeric',
            'meeting_count' => 'nullable|numeric',
            'product_count' => 'nullable|numeric',
            'events_count' => 'nullable|numeric',
            'meeting_packages_count' => 'nullable|numeric',
            'jobs_count' => 'nullable|numeric',
        ]);

        $group = Group::findOrFail($id);

        $data = $request->all();

        GroupRegistrationPackage::updateOrCreate([
            'group_id' => $group->id,
        ], [
            'instructors_count' => $data['instructors_count'] ?? null,
            'students_count' => $data['students_count'] ?? null,
            'courses_capacity' => $data['courses_capacity'] ?? null,
            'courses_count' => $data['courses_count'] ?? null,
            'meeting_count' => $data['meeting_count'] ?? null,
            'product_count' => $data['product_count'] ?? null,
            'events_count' => $data['events_count'] ?? null,
            'meeting_packages_count' => $data['meeting_packages_count'] ?? null,
            'jobs_count' => $data['jobs_count'] ?? null,
            'status' => $data['status'],
            'created_at' => time(),
        ]);

        return redirect()->back();
    }
}
