<?php 

namespace App\Models;
use CodeIgniter\Model;

class FamilyStudentModel extends Model {
    protected $table = 'family_students';
    protected $primaryKey = 'id';
    protected $allowedFields = ['family_id','student_id','is_primary_home','notes'];

    public function getFamiliesForStudent(int $studentId): array {
        $db = \Config\Database::connect();
        $sql = "SELECT f.* , fs.is_primary_home\n                FROM family_students fs\n                JOIN families f ON f.id = fs.family_id\n                WHERE fs.student_id = ? AND f.is_active = 1\n                ORDER BY fs.is_primary_home DESC, f.household_name";
        return $db->query($sql, [$studentId])->getResultArray();
    }
}