<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Login\LoginRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{

    public function login(LoginRequest $request): JsonResponse
    {
        if (!Auth::attempt($request->validated())) {
            return response()->json([
                'message' => 'Invalid credentials'
            ], 401);
        }

        $user = Auth::user();

        $token = $user->createToken('api-token')->plainTextToken;

        return response()->json([
            'message' => 'Login successful',
            'data' => [
                'user' => [
                    'id' => $user->id,
                    'name' => $user->name,
                    'email' => $user->email,
                    'roles' => $user->getRoleNames(),
                    'permissions' => $user->getAllPermissions()->pluck('name'),
                ],
                'token' => $token,
            ]
        ]);
    }


    public function logout(Request $request): JsonResponse
    {
        $request->user()->currentAccessToken()->delete();

        return response()->json([
            'message' => 'Logout successful'
        ]);
    }


    public function me(Request $request): JsonResponse
    {
        $user = $request->user();

        return response()->json([
            'data' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'roles' => $user->getRoleNames(),
                'permissions' => $user->getAllPermissions()->pluck('name'),
            ]
        ]);
    }

    /**
     * Generate permanent API token for testing
     */
    public function generateTestToken(Request $request): JsonResponse
    {
        // Only allow in development/testing environment
        if (!app()->environment(['local', 'testing'])) {
            return response()->json([
                'message' => 'Token generation only available in development environment'
            ], 403);
        }

        $request->validate([
            'email' => 'required|email|exists:users,email',
            'token_name' => 'string|max:255',
            'expires_in_days' => 'nullable|integer|min:1|max:365'
        ]);

        $user = \App\Models\User::where('email', $request->email)->first();

        $tokenName = $request->token_name ?? 'test-token-' . now()->format('Y-m-d-H-i-s');
        $tokenResult = $user->createToken($tokenName, ['*']);

        // Set expiration if specified
        if ($request->expires_in_days) {
            $expirationDate = now()->addDays($request->expires_in_days);
            $tokenResult->accessToken->update([
                'expires_at' => $expirationDate
            ]);
        }

        return response()->json([
            'message' => 'Test token generated successfully',
            'data' => [
                'user' => [
                    'id' => $user->id,
                    'name' => $user->name,
                    'email' => $user->email,
                    'roles' => $user->getRoleNames(),
                ],
                'token' => $tokenResult->plainTextToken,
                'token_name' => $tokenName,
                'expires_at' => $request->expires_in_days ?
                    now()->addDays($request->expires_in_days)->toISOString() : null,
                'is_permanent' => !$request->expires_in_days
            ]
        ]);
    }
}
