<?php

namespace App\Http\Controllers\Api\Admin;

use App\Domains\Billing\Models\Plan;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UpdatePlanRequest;
use Illuminate\Http\JsonResponse;

class AdminPlanUpdateController extends Controller
{
    public function __invoke(UpdatePlanRequest $request, string $plan): JsonResponse
    {
        $record = Plan::query()->findOrFail($plan);
        $record->update($request->validated());

        return response()->json([
            'data' => [
                'id' => $record->id,
                'name' => $record->name,
                'slug' => $record->slug,
                'monthlyPrice' => '$'.number_format((float) $record->monthly_price, 2),
                'maxUsers' => $record->max_users,
                'maxBranches' => $record->max_branches,
                'maxChannels' => $record->max_channels,
                'active' => $record->is_active,
            ],
        ]);
    }
}
