<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class GameSession extends Model
{
    protected $fillable = [
        'registration_id',
        'play_date',
        'score',
    ];

    /*
    |---------------------------
    | Relationships
    |---------------------------
    */

    // Session belongs to a student
    public function registration()
    {
        return $this->belongsTo(Registration::class);
    }

    // Session has many shots (1 session = 3 shots)
    public function shots()
    {
        return $this->hasMany(GameShot::class);
    }

    /*
    |---------------------------
    | Helper methods
    |---------------------------
    */

    // Get score as array like [1,0,1]
    public function getShotArray()
    {
        return $this->shots()
            ->orderBy('shot_number')
            ->pluck('result')
            ->toArray();
    }

    // Check if session is complete (3 shots done)
    public function isComplete()
    {
        return $this->shots()->count() >= 3;
    }

    // Calculate score from shots (reliable source)
    public function calculateScore()
    {
        return $this->shots()->where('result', 1)->count();
    }
}
