<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Users\LoginActivityRequest;
use App\Http\Requests\Users\UserListRequest;
use App\Http\Requests\Users\UserStoreRequest;
use App\Http\Requests\Users\UserUpdateRequest;
use App\Http\Resources\Users\LoginActivityResource;
use App\Http\Resources\Users\UserWithRolesResource;
use App\Services\Users\LoginActivityService;
use App\Services\Users\UserListService;
use App\Services\Users\UserManagementService;
use Illuminate\Http\JsonResponse;

class UserController extends BaseApiController
{
    public function __construct(
        private UserListService $listService,
        private UserManagementService $managementService,
        private LoginActivityService $loginActivityService
    ) {
        parent::__construct();
    }

    public function index(UserListRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $users = $this->listService->list($payload['sort'] ?? null, $payload['order'] ?? null);

        return response()->json([
            'ok' => true,
            'users' => UserWithRolesResource::collection($users),
        ]);
    }

    public function store(UserStoreRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $result = $this->managementService->create($payload, (int) (auth()->id() ?? 0));

        if (empty($result['ok'])) {
            return response()->json([
                'ok' => false,
                'message' => $result['message'] ?? 'Failed to create user.',
            ], 422);
        }

        return response()->json([
            'ok' => true,
            'user_id' => $result['user']->id ?? null,
        ], 201);
    }

    public function update(UserUpdateRequest $request, int $userId): JsonResponse
    {
        $payload = $request->validated();
        $result = $this->managementService->update($userId, $payload);

        if (empty($result['ok'])) {
            return response()->json([
                'ok' => false,
                'message' => $result['message'] ?? 'Failed to update user.',
            ], 422);
        }

        return response()->json([
            'ok' => true,
        ]);
    }

    public function destroy(int $userId): JsonResponse
    {
        $result = $this->managementService->delete($userId);

        if (empty($result['ok'])) {
            return response()->json([
                'ok' => false,
                'message' => $result['message'] ?? 'Failed to delete user.',
            ], 404);
        }

        return response()->json([
            'ok' => true,
        ]);
    }

    public function loginActivity(LoginActivityRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $perPage = $payload['per_page'] ?? 25;
        $page = $payload['page'] ?? 1;

        $result = $this->loginActivityService->list(
            (int) $perPage,
            (int) $page
        );

        return response()->json([
            'ok' => true,
            'activities' => LoginActivityResource::collection($result['activities']),
            'pagination' => $result['pagination'],
        ]);
    }
}
