<?php

namespace Database\Seeders;

use App\Models\Bank;
use App\Models\User;
use App\Enums\UserType;
use App\Models\Company;
use App\Models\UserBank;
use App\Models\Department;
use Laravolt\Avatar\Avatar;
use App\Enums\OriginStatusType;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;

class UserSeeder extends Seeder
{
  /**
   * Run the database seeds.
   */
  public function run(): void
  {
    $avatar = new Avatar();
    $presidentDirectorName = fake()->name("male");
    $directorName = fake()->name("male");
    $managerName = fake()->name("male");
    $userName = fake()->name("male");

    $presidentDirector = User::firstOrCreate(
      ['email' => 'president_director@hbm'],
      [
        'name' => $presidentDirectorName,
        'username' => str($presidentDirectorName)->slug(),
        'password' => Hash::make('password'),
        'remember_token' => Hash::make('password123'),
        'role' => UserType::PresidentDirector,
        'picture' => $avatar->create($presidentDirectorName)->setBackground('#7842f5')->setForeground('#ffffff')->setDimension(100)->toBase64()
      ]
    );

    $director = User::firstOrCreate(
      ['email' => 'director@hbm'],
      [
        'name' => $directorName,
        'username' => str($directorName)->slug(),
        'password' => Hash::make('password'),
        'remember_token' => Hash::make('password123'),
        'role' => UserType::Director,
        'picture' => $avatar->create($directorName)->setBackground('#7842f5')->setForeground('#ffffff')->setDimension(100)->toBase64()
      ]
    );

    $manager = User::firstOrCreate(
      ['email' => 'manager@hbm'],
      [
        'name' => $managerName,
        'username' => str($managerName)->slug(),
        'password' => Hash::make('password'),
        'remember_token' => Hash::make('password123'),
        'role' => UserType::Manager,
        'picture' => $avatar->create($managerName)->setBackground('#7842f5')->setForeground('#ffffff')->setDimension(100)->toBase64()
      ]
    );

    $user = User::firstOrCreate(
      ['email' => 'user@hbm'],
      [
        'name' => $userName,
        'username' => str($userName)->slug(),
        'password' => Hash::make('password'),
        'remember_token' => Hash::make('password123'),
        'role' => UserType::User,
        'picture' => $avatar->create($userName)->setBackground('#7842f5')->setForeground('#ffffff')->setDimension(100)->toBase64()
      ]
    );

    $company = Company::firstOrCreate(
      ['code' => 'HAI', 'name' => 'PT. HUANGNINDO ASIAPASIFIK INTERNASIONAL', 'address' => "TENGGARONG-KALIMANTAN TIMUR"],
      ['status' => OriginStatusType::Active]
    );

    $department = Department::firstOrCreate(
      ['name' => 'Geologis'],
      ['user_id' => $director->id, 'company_id' => $company->id, 'code' => 'GEO', 'status' => OriginStatusType::Active]
    );

    $manager->owners()->attach($director->id);
    $user->owners()->attach($manager->id);

    $manager->companies()->attach($company->id);
    $user->companies()->attach($company->id);
    $director->companies()->attach($company->id);

    $director->userDepartments()->attach($department->id, ['type' => UserType::Director]);
    $manager->userDepartments()->attach($department->id, ['type' => UserType::Manager]);
    $user->userDepartments()->attach($department->id, ['type' => UserType::User, "manager_id" => $manager->id]);

    // Finance Department Seeding
    $financeDirectorName = fake()->name("male");
    $financeManagerName = fake()->name("male");

    $financeDirector = User::firstOrCreate(
      ['email' => 'finance_director@hbm'],
      [
        'name' => $financeDirectorName,
        'username' => str($financeDirectorName)->slug(),
        'password' => Hash::make('password'),
        'remember_token' => Hash::make('password123'),
        'role' => UserType::Director,
        'picture' => $avatar->create($financeDirectorName)->setBackground('#7842f5')->setForeground('#ffffff')->setDimension(100)->toBase64()
      ]
    );

    $financeManager = User::firstOrCreate(
      ['email' => 'finance_manager@hbm'],
      [
        'name' => $financeManagerName,
        'username' => str($financeManagerName)->slug(),
        'password' => Hash::make('password'),
        'remember_token' => Hash::make('password123'),
        'role' => UserType::Manager,
        'picture' => $avatar->create($financeManagerName)->setBackground('#7842f5')->setForeground('#ffffff')->setDimension(100)->toBase64()
      ]
    );

    $financeDepartment = Department::firstOrCreate(
      ['name' => 'Finance'],
      ['user_id' => $financeDirector->id, 'company_id' => $company->id, 'code' => 'FIN', 'is_finance' => true, 'status' => OriginStatusType::Active]
    );

    $financeManager->owners()->attach($financeDirector->id);

    $financeManager->companies()->attach($company->id);
    $financeDirector->companies()->attach($company->id);

    $financeDirector->userDepartments()->attach($financeDepartment->id, ['type' => UserType::Director]);
    $financeManager->userDepartments()->attach($financeDepartment->id, ['type' => UserType::Manager]);

    $bankBCA = Bank::where('code', '014')->first();
    $bankMandiri = Bank::where('code', '008')->first();

    UserBank::firstOrCreate(
      ['account_number' => '8234567890'],
      [
        'user_id' => $user->id,
        'bank_id' => $bankBCA->id,
        'type' => \App\Enums\UserBankType::Personal,
        'account_name' => $userName,
        'status' => OriginStatusType::Active
      ]
    );

    UserBank::firstOrCreate(
      ['account_number' => '1234567899'],
      [
        'user_id' => $director->id,
        'bank_id' => $bankMandiri->id,
        'type' => \App\Enums\UserBankType::Personal,
        'account_name' => $directorName,
        'status' => OriginStatusType::Active
      ]
    );

    UserBank::firstOrCreate(
      ['account_number' => '9999888877'],
      [
        'user_id' => null,
        'company_id' => $company->id,
        'bank_id' => $bankBCA->id,
        'type' => \App\Enums\UserBankType::Company,
        'account_name' => 'PT. HUANGNINDO ASIAPASIFIK INTERNASIONAL',
        'status' => OriginStatusType::Active
      ]
    );
  }
}
