<?php

namespace App\Console\Commands;

use App\Domains\Subscription\Models\Plan;
use App\Domains\Workspace\Models\Workspace;
use Illuminate\Console\Command;

class AssignDefaultPlan extends Command
{
    protected $signature = 'workspaces:assign-default-plan';

    protected $description = 'Assign free plan to workspaces without a plan';

    public function handle(): int
    {
        $freePlan = Plan::firstWhere('tier', 'free');

        if (! $freePlan) {
            $this->error('Free plan not found. Run db:seed --class=PlanSeeder first.');

            return self::FAILURE;
        }

        $count = Workspace::whereNull('plan_id')->update(['plan_id' => $freePlan->id]);
        $this->info("✓ {$count} workspace(s) assigned to Free plan");

        return self::SUCCESS;
    }
}
