<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\FrontUserService;
use Illuminate\Http\Request;

class FrontUserController extends Controller
{
    protected $frontUserService;

    public function __construct(FrontUserService $frontUserService)
    {
        $this->frontUserService = $frontUserService;
    }

    public function index(Request $request)
    {
        $result = $this->frontUserService->index($request->all());

        return response()->json($result);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'username' => 'required|string|max:50|alpha_dash',
            'email' => 'nullable|string|max:100|email',
            'phone' => 'nullable|string|max:20',
            'password' => 'required|string|min:6|max:20',
            'nickname' => 'nullable|string|max:50',
            'avatar' => 'nullable|string|max:255',
            'gender' => 'nullable|integer|in:0,1,2',
            'birthday' => 'nullable|date',
            'signature' => 'nullable|string|max:255',
            'group_id' => 'nullable|integer|exists:user_groups,id',
            'status' => 'nullable|integer|in:0,1',
            'ext_fields' => 'nullable|array',
        ]);

        $result = $this->frontUserService->store($validated);

        return response()->json($result);
    }

    public function show($id)
    {
        $result = $this->frontUserService->show($id);

        return response()->json($result);
    }

    public function update(Request $request, $id)
    {
        $validated = $request->validate([
            'email' => 'nullable|string|max:100|email',
            'phone' => 'nullable|string|max:20',
            'password' => 'nullable|string|min:6|max:20',
            'nickname' => 'nullable|string|max:50',
            'avatar' => 'nullable|string|max:255',
            'gender' => 'nullable|integer|in:0,1,2',
            'birthday' => 'nullable|date',
            'signature' => 'nullable|string|max:255',
            'group_id' => 'nullable|integer|exists:user_groups,id',
            'status' => 'nullable|integer|in:0,1',
            'ext_fields' => 'nullable|array',
        ]);

        $result = $this->frontUserService->update($id, $validated);

        return response()->json($result);
    }

    public function destroy($id)
    {
        $result = $this->frontUserService->destroy($id);

        return response()->json($result);
    }

    public function updateStatus(Request $request, $id)
    {
        $request->validate([
            'status' => 'required|in:0,1',
        ]);

        $result = $this->frontUserService->updateStatus($id, $request->status);

        return response()->json($result);
    }

    public function resetPassword(Request $request, $id)
    {
        $request->validate([
            'password' => 'required|string|min:6|max:20',
        ]);

        $result = $this->frontUserService->resetPassword($id, $request->password);

        return response()->json($result);
    }

    public function batchDelete(Request $request)
    {
        $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'integer',
        ]);

        $result = $this->frontUserService->batchDelete($request->ids);

        return response()->json($result);
    }
}
