<?php

namespace App\Models;

use App\Models\ParentProfile;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class StudentProfile extends Model
{
    protected $fillable = [
        'user_id',
        'parent_id',
        'date_of_birth',
        'notes',
        'sen_notes',
        'year_group',
        'has_dyslexia',
        'has_dyspraxia',
        'has_autism',
        'has_adhd',
        'has_speech_needs',
        'student_type_id',
		'billing_account_id',		// will exist after next migration
    ];
	
		
	protected $casts = [
		'date_of_birth' => 'date',
	];

    /**
     * The user account for this student
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Parent/guardian of this student
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(User::class, 'parent_id');
    }
	
	public function parents(): BelongsToMany
	{
		return $this->belongsToMany(
				ParentProfile::class,
				'parent_student',
				'student_profile_id',  // FK column pointing to THIS model
				'parent_profile_id'    // FK column pointing to ParentProfile
			)
			->withPivot('relationship', 'notes')
			->withTimestamps();
	}

    /**
     * Subjects (with teachers) that this student studies
     */
    public function subjects(): HasMany
    {
        return $this->hasMany(StudentTeacherSubject::class, 'student_id');
    }

    /**
     * Attendance records for this student
     */
    public function attendance(): HasMany
    {
        return $this->hasMany(LessonAttendance::class, 'student_teacher_subject_id');
    }

    /**
     * The type of student (online, centre, hybrid, etc)
     */
    public function type(): BelongsTo
    {
        return $this->belongsTo(StudentType::class, 'student_type_id');
    }
	
	public function billingAccount()
	{
		return $this->belongsTo(BillingAccount::class);
	}
	
	public function students()
	{
		return $this->belongsToMany(StudentProfile::class, 'parent_student')
					->withPivot('relationship', 'notes')
					->withTimestamps();
	}

}
