<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * @property date $start_date
 * @property date $end_date
 * @property Pilgrim[] $pilgrims
 * @property Carbon $created_at
 * @property Carbon $updated_at
 */
class Group extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'leader_teacher_id',
    ];

    /**
     * @var array<string, string>
     */
    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
    ];

    /**
     * Get the name of the group
     *
     * */
    public function name(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $this->start_date->format('d M').' - '.$this->end_date->format('d M'),
        );
    }

    public function leaderTeacher(): BelongsTo
    {
        return $this->belongsTo(Teacher::class, 'leader_teacher_id');
    }

    public function helperTeachers(): BelongsToMany
    {
        return $this->belongsToMany(Teacher::class, 'group_teacher');
    }

    public function teachers(): BelongsToMany
    {
        return $this->belongsToMany(Teacher::class, 'group_teacher');
    }

    public function pilgrims(): HasMany
    {
        return $this->hasMany(Pilgrim::class, 'group_id');
    }

    public function program(): HasOne
    {
        return $this->hasOne(Program::class);
    }
}
