<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Services\UserService;
use Exception;
use Illuminate\Http\Request;

class UserController extends Controller
{
    protected $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    public function index()
    {
        $users = $this->userService->getAllUsers();
        return response()->json($users);
    }

    public function show(string $username)
    {
        $user = $this->userService->findByUsername($username);

        if (!$user) {
            return response()->json(['message' => 'User tidak ditemukan'], 404);
        }

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

    public function store(Request $request)
    {
        try {
            // Validasi data input
            $validatedData = $request->validate([
                'username' => 'required|string|max:25',
                'password' => 'nullable|string|min:6',
                'name' => 'sometimes|required|string|max:255',
                'email' => [
                    'nullable',
                    'email',
                    'max:255',
                ],
                'role_id' => 'nullable|integer',
            ]);

            $user = $this->userService->createUser($validatedData);
            return response()->json($user, 201);
        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }

    public function update(Request $request, int $id)
    {
        try {
            // Validasi data input
            $validatedData = $request->validate([
                'username' => 'required|string|max:25',
                'password' => 'nullable|string|min:6',
                'name' => 'sometimes|required|string|max:255',
                'email' => [
                    'nullable',
                    'email',
                    'max:255',
                ],
                'role_id' => 'nullable|integer',
            ]);

            $user = $this->userService->updateUser($id, $validatedData);

            if (!$user) {
                return response()->json(['message' => 'User tidak ditemukan'], 404);
            }

            return response()->json($user);
        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }

    public function destroy(string $username)
    {
        try {
            $deleted = $this->userService->deleteUser($username);

            if (!$deleted) {
                return response()->json(['message' => 'User tidak ditemukan atau tidak bisa dihapus'], 404);
            }

            return response()->json(['message' => 'User berhasil dihapus'], 204);
        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }

    public function login(LoginRequest $request)
    {
        $request->authenticate();
        session()->regenerate();
        return redirect()->intended(route('app', absolute: true));
    }

    public function logout(Request $request)
    {
        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return redirect('/login');
    }
}
