<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Siswa extends Model
{
    use HasFactory;
    protected $table = 'siswas';
    protected $primaryKey = 'id_siswa';

    protected $fillable = [

        'nama_siswa',
        'nis',
        'tempat_lahir',
        'tanggal',
        'jenis_kelamin',
        'agama',
        'pendik_sebelumnya',
        'jmlh_sodara',
        'alamat',
        'nama_ayah',
        'nama_ibu',
        'pekerjaan_ayah',
        'pekerjaan_ibu',
        'wali_siswa',
        'kelas',
        'jenis_kelamin',
        'tahun_pelajaran',
        'id_user',
        'id_guru'
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'id_user', 'id');
    }

    public function guru(): BelongsTo
    {
        return $this->belongsTo(Guru::class, 'id_guru', 'id_guru');
    }

    public function pelanggarans()
    {
        return $this->hasMany(Pelanggaran::class, 'id_siswa');
    }

    public function pribasos()
    {
        return $this->hasMany(Pribasos::class, 'id_siswa');
    }

    public function pengaduans(): HasMany
    {
        return $this->hasMany(Pengaduan::class, 'id_user', 'id_user');
    }

    public function jawabanKuesioner(): HasMany
    {
        return $this->hasMany(JawabanKuesioner::class, 'siswa_id', 'id_siswa');
    }

    public function riwayat(): HasManyThrough
    {
        return $this->hasManyThrough(
            Riwayat::class,
            Pengaduan::class,
            'id_user',            // Foreign key on Pengaduan table
            'id_pengaduan_bk',    // Foreign key on Riwayat table
            'id_user',            // Local key on Siswa table
            'id_pengaduan_bk'     // Local key on Pengaduan table
        );
    }
}
