<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\CountryCode;

class CurrencyRates extends Model
{
  use HasFactory;
      /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'v_currency';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'currency';
    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    use HasFactory;

    protected function actual(String $status)
    {
      return $this->get();
    }

    protected function rates2EUR(String $status)
    {
      $rates = $this->actual("Y")->mapWithKeys(function ($item) {
        return [$item['currency'] => $item['rate']];
      });

      return CountryCode::active("Y")->mapWithKeys(function ($item) use ($rates) {
        if ($item['currency_code'] == "EUR") return [$item["country_code"] => 1.0];
        return [$item["country_code"] => $rates[$item["currency_code"]]];
      });
    }

    protected function currencyCode() {
      return CountryCode::active("Y")->mapWithKeys(function ($item) {
        return [$item['currency_code'] => $item['country_code']];
      });
    }

    protected function countryCurrency() {
      return CountryCode::active("Y")->mapWithKeys(function ($item) {
        return [$item["country_code"] => $item["currency_code"]];
      });
    }

}
