<?php

namespace Database\Seeders\Initial;

use App\Models\User;
use App\Enums\UserType;
use App\Models\Category;
use Illuminate\Database\Seeder;

class CategorySeeder extends Seeder
{
  public function run(): void
  {
    $categories = [
      [
        "name" => "Biaya Operasional",
        "period_type" => \App\Enums\PeriodType::Monthly,
        "status" => \App\Enums\OriginStatusType::Active,
        "child_categories" => [
          [
            "name" => "BBM",
            "period_type" => \App\Enums\PeriodType::Monthly,
            "status" => \App\Enums\OriginStatusType::Active,
          ],
          [
            "name" => "Perlengkapan Mess Gn. Sari",
            "period_type" => \App\Enums\PeriodType::Monthly,
            "status" => \App\Enums\OriginStatusType::Active,
          ],
          [
            "name" => "Akomodasi",
            "period_type" => \App\Enums\PeriodType::Monthly,
            "status" => \App\Enums\OriginStatusType::Active,
          ],
          [
            "name" => "Konsumsi",
            "period_type" => \App\Enums\PeriodType::Monthly,
            "status" => \App\Enums\OriginStatusType::Active,
          ],
        ],
      ],
      [
        "name" => "Biaya Lainnya",
        "period_type" => \App\Enums\PeriodType::Other,
        "status" => \App\Enums\OriginStatusType::Active,
      ],
    ];

    $user = User::whereRole(UserType::Director)->first();

    $createdCategories = [];
    foreach ($categories as $category) {
      $level = 1;
      $childs = null;

      if (isset($category["child_categories"])) {
        $childs = $category["child_categories"];
        unset($category["child_categories"]);
      }

      $category['level'] = $level;
      $category = Category::create($category);

      if ($childs) {
        $level++;
        foreach ($childs as $child) {
          $child["parent_id"] = $category->id;
          $child["level"] = $level;
          $child = Category::create($child);
          $createdCategories[] = $child;
        }
      }

      $createdCategories[] = $category;
    }

    $user->categories()->attach($createdCategories);
  }
}
