<?php

namespace Database\Seeders;

use App\Enums\EmploymentStatus;
use App\Enums\Gender;
use App\Enums\Role;
use App\Models\Employee;
use App\Models\User;
use App\Models\UserProfile;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class EmployeeSeeder extends Seeder
{
    public function run(): void
    {
        DB::transaction(function () {
            $employees = [
                Role::ADMIN_TOKO->value => [
                    'username' => 'admintoko',
                    'email' => 'admintoko@gmail.com',
                    'full_name' => 'Admin Toko',
                ],
                Role::ADMIN_BAHAN_BAKU->value => [
                    'username' => 'adminbahanbaku',
                    'email' => 'adminbahanbaku@gmail.com',
                    'full_name' => 'Admin Bahan Baku',
                ],
                Role::DIREKTUR->value => [
                    'username' => 'direktur',
                    'email' => 'direktur@gmail.com',
                    'full_name' => 'Direktur',
                ],
                Role::MARKETING_OFFLINE->value => [
                    'username' => 'marketing-offline',
                    'email' => 'marketing.offline@gmail.com',
                    'full_name' => 'Marketing Offline',
                ],
                Role::MARKETING_ONLINE->value => [
                    'username' => 'marketing-online',
                    'email' => 'marketing.online@gmail.com',
                    'full_name' => 'Marketing Online',
                ],
                Role::CASHIER->value => [
                    'username' => 'cashier',
                    'email' => 'cashier@gmail.com',
                    'full_name' => 'Cashier',
                ],
                Role::NON_OPERATOR->value => [
                    'username' => 'nonoperator',
                    'email' => 'nonoperator@gmail.com',
                    'full_name' => 'Non Operator',
                ],
            ];

            foreach ($employees as $roleName => $data) {
                $user = User::firstOrCreate(
                    ['email' => $data['email']],
                    [
                        'username' => $data['username'],
                        'password' => Hash::make(config('auth.password_default')),
                        'is_active' => true,
                    ]
                );

                UserProfile::firstOrCreate(
                    ['user_id' => $user->id],
                    [
                        'full_name' => $data['full_name'],
                        'phone_number' => '08'.fake()->numerify('##########'),
                        'gender' => fake()->randomElement(Gender::values()),
                        'birth_date' => '1995-01-01',
                        'address' => 'Jl. Jenderal Sudirman No. 123',
                    ]
                );

                Employee::firstOrCreate(
                    ['user_id' => $user->id],
                    [
                        'join_date' => now()->format('Y-m-d'),
                        'employment_status' => EmploymentStatus::FULL_TIME->value,
                        'base_salary' => 3000000,
                    ]
                );

                $user->syncRoles([$roleName]);
            }
        });
    }
}
