<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class Affiliator extends Model
{
    use HasFactory;
    use LogsActivity;


    public static $STATUS_PENDING = 'pending';
    public static $STATUS_APPROVED = 'approved';
    public static $STATUS_REJECTED = 'rejected';
    public static $STATUS_CANCELLED = 'cancelled';

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults();
    }

    protected $fillable = [
        'name', 
        'code', 
        'email', 
        'phone',
        'user_id', 
        'bank', 
        'account_name', 
        'account_number', 
        'fee_percentage',
        'dob',
        'coaching_status',
        'coaching_area',
        'caddy_area',
        'caddy_los',
        'trainee_count',
        'instagram',
        'youtube',
        'tiktok',
        'type',
        'verified_at',
        'status'
    ];

    protected $casts = [
        'dob' => 'date',
        'verified_at' => 'datetime',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }


    public function withdraws()
    {
        return $this->hasMany(AffiliatorWithdraw::class);
    }

    public function feeLedgers()
    {
        return $this->hasMany(AffiliatorFeeLedger::class);
    }


    public function getBalanceAttribute() : float
    {
        return $this->feeLedgers()->sum('amount');
    }


    public function routeNotificationForEmail()
    {
        $email = $this->email;
        return $email;
    }
}
