<?php

namespace App\Models;

use CodeIgniter\Model;

class SemesterScoreModel extends Model
{
    protected $table = 'semester_scores';
    protected $primaryKey = 'id';
    protected $allowedFields = [
        'student_id',
        'school_id',
        'class_section_id',
        'updated_by',
        'homework_avg',
        'quiz_avg',
        'project_avg',
        'midterm_exam_score',
        'final_exam_score',
        'attendance_score',
        'participation_score',
        'ptap_score',
        'test_avg',
        'semester_score',
        'semester',
        'school_year'
    ];

    /**
     * Insert or update a semester score record
     */
// In SemesterScoreModel.php

public function upsert(array $data): bool
{
    // First check if record exists
    $existing = $this->where([
        'student_id' => $data['student_id'],
        'semester' => $data['semester'],
        'school_year' => $data['school_year']
    ])->first();

    // If exists - UPDATE
    if ($existing) {
        return $this->update($existing['id'], $data);
    }
    // If new - INSERT
    else {
        return $this->insert($data) !== false;
    }
}
}