<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class Merchant extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = ['public_id', 'name', 'email', 'password', 'status', 'email_verified_at', 'two_factor_enabled', 'two_factor_secret', 'parent_merchant_id', 'merchant_type', 'external_reference', 'treasury_policy'];

    protected $hidden = ['password', 'remember_token', 'two_factor_secret', 'parent_merchant_id', 'merchant_type', 'external_reference', 'treasury_policy'];

    protected function casts(): array
    {
        return ['password' => 'hashed', 'email_verified_at' => 'datetime', 'two_factor_enabled' => 'boolean', 'two_factor_secret' => \App\Casts\EncryptedEnvelope::class, 'treasury_policy' => 'array'];
    }

    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_merchant_id');
    }

    public function subMerchants()
    {
        return $this->hasMany(self::class, 'parent_merchant_id');
    }

    public function apiKeys()
    {
        return $this->hasMany(ApiKey::class);
    }

    public function invoices()
    {
        return $this->hasMany(Invoice::class);
    }

    public function customers()
    {
        return $this->hasMany(Customer::class);
    }
}
