<?php

namespace Database\Seeders\Admin;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
use App\Helpers\CoreApp\Traits\PermissionTrait;

class UserSeeder extends Seeder
{
    use PermissionTrait;

    public function run()
    {
        try {
            foreach ($this->userData() ?? [] as $user) {
                User::firstOrCreate([
                    'email'              => $user['email'],
                ], [
                    'name'               => $user['name'],
                    'is_admin'           => @$user['is_admin'],
                    'is_hr'              => @$user['is_hr'],
                    'role_id'            => @$user['role_id'],
                    'company_id'         => @$user['company_id'],
                    'department_id'      => @$user['department_id'],
                    'designation_id'     => @$user['designation_id'],
                    'phone'              => @$user['phone'],
                    'permissions'        => @$user['permissions'],
                    'duty_schedule_id'   => 1,
                    'weekends'           => ["saturday", "sunday"],
                    'joining_date'       => now()->format('Y-m-d'),
                    'password'           => Hash::make('12345678'),
                    'attendance_method'  => ['normal_attendance' => 1],
                    "is_free_ip"         => 1,
                    "is_free_location"   => 1
                ]);
            }

            $input = session()->get('input');
            if ($input) {
                $input['user_id'] = 1;
                session()->put('input', $input);
            }
        } catch (\Throwable $th) {
            Log::error($th);
        }
    }

    protected function userData()
    {
        $input = session()->get('input');

        return [
            [
                'name'             => $input['name'] ?? 'Mr Super Admin',
                'email'            => $input['email'] ?? 'admin@onesttech.com',
                'phone'            => $input['phone'] ?? '0XXXXXXXXXX0',
                'is_admin'         => 1,
                'is_hr'            => 0,
                'role_id'          => 1,
                'company_id'       => @$input['company_id'] ?? 1,
                'branch_id'        => @$input['branch_id'] ?? 1,
                'department_id'    => 1,
                'designation_id'   => 1,
                'permissions'      => $this->adminPermissions(),
            ],
            [
                'name'             => $input['name'] ?? 'HR',
                'email'            => $input['email'] ?? 'hr@onesttech.com',
                'phone'            => $input['phone'] ?? '0XXXXXXXXXX1',
                'is_admin'         => 0,
                'is_hr'            => 1,
                'role_id'          => 3,
                'company_id'       => @$input['company_id'] ?? 1,
                'branch_id'        => @$input['branch_id'] ?? 1,
                'duty_schedule_id' => @$input['duty_schedule_id'] ?? 1,
                'department_id'    => 1,
                'designation_id'   => 1,
                'permissions'      => $this->hrPermissions(),
            ],
            [
                'name'             => $input['name'] ?? 'Staff',
                'email'            => $input['email'] ?? 'staff@onesttech.com',
                'phone'            => $input['phone'] ?? '0XXXXXXXXXX2',
                'is_admin'         => 0,
                'is_hr'            => 0,
                'role_id'          => 4,
                'company_id'       => @$input['company_id'] ?? 1,
                'branch_id'        => @$input['branch_id'] ?? 1,
                'duty_schedule_id' => @$input['duty_schedule_id'] ?? 1,
                'department_id'    => 1,
                'designation_id'   => 1,
                'permissions'      => $this->staffPermissions(),
            ]
        ];
    }
}
