<?php

namespace App\Models;

use App\Enums\GroupStatus;
use App\Models\Concerns\BelongsToBusiness;
use App\Models\Concerns\WritesAuditLog;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class AjoGroup extends Model
{
    use BelongsToBusiness, HasUuids, SoftDeletes, WritesAuditLog;

    protected $fillable = ['name', 'contribution_type', 'contribution_amount', 'maximum_members', 'start_date', 'end_date', 'status', 'schedule'];

    protected function casts(): array
    {
        return ['start_date' => 'date', 'end_date' => 'date', 'status' => GroupStatus::class, 'schedule' => 'array'];
    }

    public function members(): BelongsToMany
    {
        return $this->belongsToMany(Member::class, 'group_members')->withPivot(['payout_position', 'joined_at', 'status'])->withTimestamps();
    }

    public function contributions(): HasMany
    {
        return $this->hasMany(Contribution::class);
    }

    public function payouts(): HasMany
    {
        return $this->hasMany(Payout::class);
    }
}
