<?php

namespace Database\Seeders;

use App\Domains\Subscription\Models\Plan;
use App\Domains\Workspace\Models\Workspace;
use App\Domains\Workspace\Models\WorkspaceMember;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DevUserSeeder extends Seeder
{
    public function run(): void
    {
        $user = User::updateOrCreate(
            ['email' => 'nexxo@nimuli.com'],
            [
                'name' => 'Nexxo Admin',
                'password' => Hash::make('NimuliDev2026!'),
                'email_verified_at' => now(),
                'locale' => 'de',
                'theme' => 'dark',
            ]
        );

        $plan = Plan::firstWhere('name', 'free') ?? Plan::first();

        $workspace = Workspace::firstOrCreate(
            ['owner_id' => $user->id, 'slug' => 'nexxo'],
            [
                'name' => 'Nexxo Workspace',
                'plan_id' => $plan?->id,
            ]
        );

        WorkspaceMember::firstOrCreate(
            ['workspace_id' => $workspace->id, 'user_id' => $user->id],
            ['role' => 'owner', 'joined_at' => now()]
        );

        if ($user->default_workspace_id !== $workspace->id) {
            $user->updateQuietly(['default_workspace_id' => $workspace->id]);
        }

        $this->command->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
        $this->command->info(' Dev-User bereit:');
        $this->command->info('  Email:     nexxo@nimuli.com');
        $this->command->info('  Password:  NimuliDev2026!');
        $this->command->info('  Workspace: '.$workspace->ulid);
        $this->command->info('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
    }
}
