<?php

namespace Database\Seeders;

use App\Models\Company;
use App\Models\User;
use App\Services\CompanySetupService;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        foreach (['admin', 'manager', 'sales_agent'] as $role) {
            Role::findOrCreate($role, 'web');
        }

        $company = Company::firstOrCreate(
            ['name' => 'Admin Company'],
            ['plan' => 'free']
        );

        if ($company->pipelineStages()->count() === 0) {
            app(CompanySetupService::class)->seedDefaultPipeline($company);
        }

        $admin = User::firstOrNew(['email' => 'admin@admin.com']);
        $admin->company_id = $company->id;
        $admin->name = 'Administrator';
        $admin->password = 'password';
        $admin->save();

        $admin->syncRoles(['admin']);
    }
}
