<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Agent extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'governorate',
        'phone',
        'whatsapp',
        'is_premium',
        'transaction_count',
        'rating',
    ];

    protected $casts = [
        'is_premium' => 'boolean',
        'rating' => 'decimal:2',
        'transaction_count' => 'integer',
    ];

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

    public function transactions()
    {
        return $this->hasMany(AgentTransaction::class);
    }

    public function reviews()
    {
        return $this->hasMany(AgentReview::class);
    }

    public function isPremium()
    {
        return $this->type === 'premium' && $this->total_transactions >= 500;
    }

    public function getAverageRating()
    {
        return $this->reviews()->avg('rating') ?? 0;
    }

    public function getTotalReviews()
    {
        return $this->reviews()->count();
    }
}
