<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class MembershipCategory extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'slug',
        'description',
        'benefits',
        'is_active',
        'theme_color',
        'icon',
        'sort_order',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'is_active' => 'boolean',
        'benefits' => 'array',
        'sort_order' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * Get the members for the category.
     */
    public function members(): BelongsToMany
    {
        return $this->belongsToMany(Member::class, 'member_membership_category')
            ->withPivot('assigned_date', 'assigned_by', 'is_primary')
            ->withTimestamps();
    }

    /**
     * Get the subscription fees for the category.
     */
    public function subscriptionFees(): HasMany
    {
        return $this->hasMany(SubscriptionFee::class);
    }

    /**
    * Get the active subscription fee for the category.
     */
    public function activeSubscriptionFee()
    {
        return $this->subscriptionFees()->where('is_active', true)->first();
    }

    /**
     * Get the contribution fees for the category.
     */
    public function contributionFees(): HasMany
    {
        return $this->hasMany(ContributionFee::class);
    }

    /**
     * Get the active contribution fees for the category.
     */
    public function activeContributionFees()
    {
        return $this->contributionFees()->where('is_active', true)->get();
    }

    /**
     * Get the penalties for the category.
     */
    public function penalties(): HasMany
    {
        return $this->hasMany(ContributionPenalty::class);
    }

    /**
     * Get the membership applications for the category.
     */
    public function membershipApplications(): HasMany
    {
        return $this->hasMany(MembershipApplication::class);
    }

    /**
     * Get the contributions for the category.
     */
    public function contributions(): HasMany
    {
        return $this->hasMany(Contribution::class);
    }

    /**
     * Scope a query to only include active categories.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope a query to order by sort order.
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order')->orderBy('name');
    }

    /**
     * Check if category can be deleted.
     */
    public function canBeDeleted(): bool
    {
        return $this->members()->count() === 0;
    }

    /**
     * Get the member count for this category.
     */
    public function getMemberCountAttribute(): int
    {
        return $this->members()->count();
    }

    /**
     * Get the route key for the model.
     */
    public function getRouteKeyName(): string
    {
        return 'slug';
    }
}
