<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Helpers\ApiResponse;
use App\Models\DeviceToken;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;

class DeviceTokenController extends Controller
{
    #[OA\Post(
        path: '/v1/devices/register',
        operationId: 'registerDevice',
        tags: ['Push Notifications'],
        summary: 'Register a device token for push notifications',
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(
            required: true,
            content: new OA\JsonContent(
                required: ['token', 'platform'],
                properties: [
                    new OA\Property(property: 'token', type: 'string', description: 'FCM device token'),
                    new OA\Property(property: 'platform', type: 'string', enum: ['ios', 'android', 'web']),
                    new OA\Property(property: 'device_name', type: 'string', nullable: true),
                    new OA\Property(property: 'app_version', type: 'string', nullable: true),
                ]
            )
        ),
        responses: [
            new OA\Response(response: 200, description: 'Device registered'),
            new OA\Response(response: 422, description: 'Validation error'),
        ]
    )]
    public function register(Request $request)
    {
        $request->validate([
            'token' => 'required|string',
            'platform' => 'required|in:ios,android,web',
            'device_name' => 'nullable|string|max:100',
            'app_version' => 'nullable|string|max:20',
        ]);

        $deviceToken = DeviceToken::updateOrCreate(
            ['token' => $request->input('token')],
            [
                'user_id' => $request->user()->id,
                'platform' => $request->input('platform'),
                'device_name' => $request->input('device_name'),
                'app_version' => $request->input('app_version'),
                'last_used_at' => now(),
            ]
        );

        return ApiResponse::success(['device_id' => $deviceToken->id], 'Device registered successfully');
    }

    #[OA\Delete(
        path: '/v1/devices/unregister',
        operationId: 'unregisterDevice',
        tags: ['Push Notifications'],
        summary: 'Remove a device token',
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(
            required: true,
            content: new OA\JsonContent(
                required: ['token'],
                properties: [new OA\Property(property: 'token', type: 'string')]
            )
        ),
        responses: [
            new OA\Response(response: 200, description: 'Device unregistered'),
        ]
    )]
    public function unregister(Request $request)
    {
        $request->validate([
            'token' => 'required|string',
        ]);

        DeviceToken::where('token', $request->input('token'))
            ->where('user_id', $request->user()->id)
            ->delete();

        return ApiResponse::success(null, 'Device unregistered successfully');
    }
}
