<?php

namespace App\Models;

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;

class Sprint extends Model
{
    protected $fillable = [
        'title', 'project_id', 'goal', 'capacity_hours', 'start_date', 'end_date',
        'status', 'completed_at', 'completion_summary', 'created_by',
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'capacity_hours' => 'decimal:2',
        'completed_at' => 'datetime',
        'completion_summary' => 'array',
    ];

    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class);
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function tasks(): BelongsToMany
    {
        return $this->belongsToMany(Task::class, 'sprint_task')->withTimestamps();
    }

    public function members(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'sprint_members')->withTimestamps();
    }

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

    public function backlogItems(): HasMany
    {
        return $this->hasMany(BacklogItem::class, 'assigned_sprint_id');
    }

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

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

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

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

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