<?php

namespace App\Http\Controllers\Admin;

use App\Models\Bank;
use App\Models\User;
use App\Utils\Helper;
use App\Enums\UserType;
use App\Models\UserBank;
use App\Enums\ActionType;
use Illuminate\Http\Request;
use App\Enums\OriginStatusType;
use App\Enums\PublishStatusType;
use App\Http\Controllers\Core\BaseResourceController;
use App\Models\Department;

class UserBankController extends BaseResourceController
{
  protected $model = UserBank::class;

  protected function indexQuery($query, Request $request)
  {
    $query->when($request->filled("status"), fn($q) => $q->where("status", $request->status));
    $query->with("user:id,name,role");
    $query->with("company:id,name");
    $query->with("bank");

    return $query;
  }

  protected function getPage(Request $request, $id = null): array
  {
    $page = [
      "name" => "user_banks",
      "inertia" => "Admin/UserBank",
      "label" => "Rekening Bank",
      "url" => "/admin/user_banks",
      "fields" => Helper::getFormFields($this->validation($request)),
    ];

    return $page;
  }

  protected function validation(Request $request, $id = null): array
  {
    return [
      "validation" => [
        "type" => "required|string",
        "user_id" => "required_if:type," . \App\Enums\UserBankType::Personal . "|nullable|integer|exists:users,id",
        "company_id" => "required_if:type," . \App\Enums\UserBankType::Company . "|nullable|integer|exists:companies,id",
        "bank_id" => "required|integer|exists:banks,id",
        "account_number" => "required|numeric",
        "account_name" => "required|string",
        "branch" => "nullable|string",
        "status" => "required|string",
      ],
      "default" => [
        "type" => \App\Enums\UserBankType::Personal,
        "status" => \App\Enums\OriginStatusType::Active,
      ],
    ];
  }

  protected function getFormData(Request $request, $model = null): array
  {
    $formData = [];
    $status = \App\Enums\OriginStatusType::getValues();
    $types = \App\Enums\UserBankType::getValues();

    if ($this->isFormPage) {
      $formData["banks"] = \App\Models\Bank::selectOptions()->where('status', \App\Enums\OriginStatusType::Active)->get();
      $formData["users"] = \App\Models\User::selectOptions()->where('status', \App\Enums\OriginStatusType::Active)->get();
      $formData["companies"] = \App\Models\Company::selectOptions()->where('status', \App\Enums\OriginStatusType::Active)->get();
    }

    return [
      "status" => $status,
      "types" => $types,

      ...parent::getFormData($request, $model),
      ...$formData,
    ];
  }

  protected function getAuthorize(Request $request, $data = null, $action = ActionType::Read)
  {
    $user = auth()->user();
    $department = Department::active($request)->first();
    if ($department->is_finance && in_array($user->role, [UserType::Director, UserType::Manager]))
      return true;

    $actionLabels = Helper::getEnumTranslation(ActionType::class, "id");
    return Helper::redirectBack("error", "Anda tidak diperbolehkan {$actionLabels[$action]} {$this->page["label"]} ini");
  }
}
