<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;
class PaymentMethod extends BaseModel
{
    use HasFactory;

    protected $table = 'payment_method';

    public function user(){
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function displayName(){
        if($this->card_type){
            return "<small>{$this->card_type} ****-****-{$this->card_last_four}</small><small class='text-muted d-block'>Expires: {$this->expiration_month} / {$this->expiration_year}</small>";
        }
        $parsed = json_decode($this->stripe_detail_json ?? $this->braintree_detail_json);
        if(@$parsed->tenant){
            return "<small class='text-uppercase'>{$parsed->tenant} - {$parsed->email}</small>";
        }
        if(@$parsed->card) {
          return "<small class='text-uppercase'>{$parsed->card->brand} ****-****-{$parsed->card->last4}</small><small class='text-muted d-block'>Expires: {$parsed->card->exp_month} / {$parsed->card->exp_year}</small>";  
        }      

        return 'Non-card Payment Method';
    }
    public function displayNameShort(){
      if ($this->card_type) {
        return "****-****-{$this->card_last_four}";
      }
        $parsed = json_decode($this->stripe_detail_json);
        if(!@$parsed->card) return 'Non-card Payment Method';
        return "****-****-{$parsed->card->last4}";
    }

    public function brand(){
        if($this->card_type) return $this->card_type;
        $parsed = json_decode($this->stripe_detail_json);
        if(!@$parsed->card) return;
        return $parsed->card->brand;
    }

    public function cardLast4(){
        if($this->card_last_four) return $this->card_last_four;
        $parsed = json_decode($this->stripe_detail_json);
        if(!@$parsed->card) return '---';
        return $parsed->card->last4;
    }

    public function stripeDetails() {
        return json_decode($this->stripe_detail_json);
    }

    public function getWalletType(){
        $data = $this->stripeDetails();
        if(!@$data->card) return;
        if(!@$data->card->wallet) return;
        return toHumanReadable($data->card->wallet->type);
    }

    public function brandToCssClassName(){
        $brand = $this->brand();
        if(!$brand) return '';

        $brand = strtolower($brand);
        return preg_replace('/\s+/', '_', $brand);
    }

    public function financialTransactions(){
        return $this->hasMany(FinancialTransaction::class, 'payment_method_id', 'id');
    }

    public function isExpired() {
        $stripeDetailJson = json_decode($this->stripe_detail_json);
        $expirationMonth = @$stripeDetailJson->card ? @$stripeDetailJson->card->exp_month : null;
        $expirationYear = @$stripeDetailJson->card ? @$stripeDetailJson->card->exp_year : null;

        if((int) $expirationYear >= (int) date('Y')){
            if((int) $expirationMonth >= (int) date('m')){
                return false;
            }
        }
        return true;
    }
}
