<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\Admin\Contracts\AdminServiceInterface;
use Carbon\Carbon;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function __construct(
        private AdminServiceInterface $adminService
    ) {}

    public function index(Request $request)
    {
        $selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
        $search        = $request->input('search');
        $flagSend      = $request->input('flag_send');

        ['users' => $users, 'topReceivedUser' => $topReceivedUser, 'topSentUser' => $topSentUser]
            = $this->adminService->getUserListWithStats($selectedMonth, $search, $flagSend);

        if ($request->ajax() || $request->wantsJson()) {
            return response()->json([
                'title' => view('admin.users.partials.title', compact('selectedMonth'))->render(),
                'stats' => view('admin.users.partials.stats', compact('topReceivedUser', 'topSentUser'))->render(),
                'table' => view('admin.users.partials.table', compact('users'))->render(),
            ]);
        }

        return view('admin.users.index', compact('users', 'selectedMonth', 'topReceivedUser', 'topSentUser', 'search', 'flagSend'));
    }

    public function create()
    {
        return view('admin.users.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'msnv'        => 'required|integer|unique:user,msnv',
            'name'        => 'required|string|max:255',
            'mail'        => 'required|email|unique:user,mail',
            'departments' => 'required|integer|min:1|max:5',
            'password'    => 'required|string',
            'role'        => 'required|in:' . User::ROLE_MEMBER . ',' . User::ROLE_ADMIN,
        ]);

        $this->adminService->createUser($request->only('msnv', 'name', 'mail', 'departments', 'password', 'role'));

        return redirect()->route('admin.users.index')->with('success', __('messages.user_create_success'));
    }

    public function edit(Request $request, $msnv)
    {
        $user          = User::where('msnv', $msnv)->firstOrFail();
        $selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));

        $administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth);

        return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth'));
    }

    public function update(Request $request, $msnv)
    {
        $request->validate([
            'num_card'  => 'nullable|integer|min:1',
            'flag_send' => 'nullable|boolean',
        ]);

        $this->adminService->updateUser($msnv, $request->only('num_card', 'flag_send'));

        return redirect()->back()->with('success', __('messages.user_update_success'));
    }

    public function destroy($msnv)
    {
        $this->adminService->deactivateUser($msnv);

        return redirect()->route('admin.users.index')->with('success', __('messages.user_deactivate_success'));
    }

    public function resetCards()
    {
        $this->adminService->resetAllCards();

        return redirect()->back()->with('success', __('messages.cards_reset_success'));
    }
}
