<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\User;
use Database\Seeders\Support\DemoSeederContext;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

class DemoUsersSeeder extends Seeder
{
    public function run(): void
    {
        $this->ensureRoles();

        $this->seedAdmin();
        $this->seedEmployer();
        $this->seedProducerUser();
        $this->seedExpertUser();
        $this->seedWriter();
    }

    private function ensureRoles(): void
    {
        foreach (['admin', 'writer', 'user', 'employer', 'producer', 'expert'] as $role) {
            Role::findOrCreate($role, 'api');
        }
    }

    private function seedAdmin(): void
    {
        $admin = User::firstOrCreate(
            ['email' => 'info@vianasystem.com'],
            [
                'first_name' => 'Admin',
                'cellphone' => '09123456789',
                'password' => Hash::make(DemoSeederContext::DEMO_PASSWORD),
                'account_type' => User::ACCOUNT_ADMIN,
            ]
        );

        $admin->syncRoles(['admin']);
        $admin->updateQuietly(['account_type' => User::ACCOUNT_ADMIN]);
    }

    private function seedEmployer(): void
    {
        $employer = User::firstOrCreate(
            ['email' => 'morteza@mori.com'],
            [
                'first_name' => 'یوسف',
                'last_name' => 'روشندل',
                'cellphone' => '09210733936',
                'national_code' => '0012345678',
                'password' => Hash::make(DemoSeederContext::DEMO_PASSWORD),
                'wallet' => 15_500_000,
                'account_type' => User::ACCOUNT_EMPLOYER,
            ]
        );

        $employer->syncRoles(['employer', 'user']);
        $employer->updateQuietly([
            'wallet' => 15_500_000,
            'account_type' => User::ACCOUNT_EMPLOYER,
            'first_name' => 'یوسف',
            'last_name' => 'روشندل',
            'national_code' => '0012345678',
        ]);
    }

    private function seedProducerUser(): void
    {
        $producer = User::firstOrCreate(
            ['email' => 'producer@rievo.ir'],
            [
                'first_name' => 'رضا',
                'last_name' => 'دامدار',
                'cellphone' => '09122222222',
                'national_code' => '0023456789',
                'password' => Hash::make(DemoSeederContext::DEMO_PASSWORD),
                'wallet' => 3_000_000,
                'account_type' => User::ACCOUNT_PRODUCER,
            ]
        );

        $producer->syncRoles(['producer', 'user']);
        $producer->updateQuietly(['account_type' => User::ACCOUNT_PRODUCER]);
    }

    private function seedExpertUser(): void
    {
        $expert = User::firstOrCreate(
            ['email' => 'expert@rievo.ir'],
            [
                'first_name' => 'دکتر',
                'last_name' => 'احمدی',
                'cellphone' => '09121111111',
                'national_code' => '0034567890',
                'password' => Hash::make(DemoSeederContext::DEMO_PASSWORD),
                'account_type' => User::ACCOUNT_EXPERT,
            ]
        );

        $expert->syncRoles(['expert', 'user']);
        $expert->updateQuietly(['account_type' => User::ACCOUNT_EXPERT]);
    }

    private function seedWriter(): void
    {
        $writer = User::firstOrCreate(
            ['email' => 'writer@rievo.ir'],
            [
                'first_name' => 'سارا',
                'last_name' => 'نویسنده',
                'cellphone' => '09123333333',
                'password' => Hash::make(DemoSeederContext::DEMO_PASSWORD),
            ]
        );

        if (! $writer->hasRole('writer')) {
            $writer->assignRole('writer');
        }
    }
}
