<?php

namespace App\Http\Controllers\Api\Desktop;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class DesktopController extends Controller
{
    /**
     * Check license key and return user data
     * This endpoint is public (no authentication required)
     */
    public function checkLicense(Request $request): JsonResponse
    {
        $request->validate([
            'license_key' => 'required|string'
        ]);

        $licenseKey = $request->string('license_key')->toString();

        $user = User::where('license_key', $licenseKey)
            ->where('is_active', true)
            ->first();

        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => 'Invalid or inactive license key'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'license_key' => $user->license_key,
                'is_active' => $user->is_active,
            ],
            'message' => 'License key verified successfully'
        ]);
    }

    /**
     * Authenticate user and return API token
     * This endpoint is public (no authentication required)
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function authenticate(Request $request): JsonResponse
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required|string',
            'license_key' => 'required|string',
        ]);

        $user = User::where('email', $request->email)
            ->where('license_key', $request->license_key)
            ->where('is_active', true)
            ->first();

        if (!$user || !Hash::check($request->password, $user->password)) {
            return response()->json([
                'success' => false,
                'message' => 'Invalid credentials or license key'
            ], 401);
        }

        // Delete existing tokens for this user (optional - you can remove this if you want multiple tokens)
        // $user->tokens()->delete();

        // Create a new token
        $token = $user->createToken('api-token')->plainTextToken;

        return response()->json([
            'success' => true,
            'data' => [
                'token' => $token,
                'user' => [
                    'id' => $user->id,
                    'name' => $user->name,
                    'email' => $user->email,
                ]
            ],
            'message' => 'Authentication successful'
        ]);
    }

    /**
     * Test protected endpoint - requires Sanctum authentication
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function test(Request $request): JsonResponse
    {
        return response()->json([
            'success' => true,
            'message' => 'Protected endpoint accessed successfully',
            'data' => [
                'user' => $request->user(),
                'timestamp' => now()->toDateTimeString(),
            ]
        ]);
    }
}

