from django.db import models

class ProgramStudi(models.Model):
    nama = models.CharField(max_length=100)

    def __str__(self):
        return self.nama

    class Meta:
        verbose_name_plural = "Program Studi"

class Dosen(models.Model):
    nidn = models.CharField(max_length=20, unique=True)
    nama = models.CharField(max_length=100)

    def __str__(self):
        return self.nama

    class Meta:
        verbose_name_plural = "Dosen"

class TahunAkademik(models.Model):
    nama = models.CharField(max_length=20)  # e.g., 2025/2026
    is_active = models.BooleanField(default=False)

    def __str__(self):
        return self.nama

    class Meta:
        verbose_name_plural = "Tahun Akademik"

class Hari(models.Model):
    nama = models.CharField(max_length=20)

    def __str__(self):
        return self.nama

    class Meta:
        verbose_name_plural = "Hari"

class MataKuliah(models.Model):
    kode = models.CharField(max_length=20)
    nama = models.CharField(max_length=100)
    sks = models.IntegerField()
    semester = models.IntegerField()
    prodi = models.ForeignKey(ProgramStudi, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.kode} - {self.nama}"

    class Meta:
        verbose_name_plural = "Mata Kuliah"
        unique_together = [['kode', 'prodi'], ['nama', 'prodi']]

class Ruangan(models.Model):
    nama = models.CharField(max_length=50)
    kapasitas = models.IntegerField()
    prodi = models.ForeignKey(ProgramStudi, on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.nama

    class Meta:
        verbose_name_plural = "Ruangan"

class Kelas(models.Model):
    nama = models.CharField(max_length=20)  # e.g., A, B, C or 1A, 1B
    prodi = models.ForeignKey(ProgramStudi, on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.nama

    class Meta:
        verbose_name_plural = "Kelas"

class Jam(models.Model):
    range_waktu = models.CharField(max_length=50)  # e.g., 07:00 - 08:40

    def __str__(self):
        return self.range_waktu

    class Meta:
        verbose_name_plural = "Jam"

class DosenPengampu(models.Model):
    dosen = models.ForeignKey(Dosen, on_delete=models.CASCADE)
    matkul = models.ForeignKey(MataKuliah, on_delete=models.CASCADE)
    tahun_akademik = models.ForeignKey(TahunAkademik, on_delete=models.CASCADE)
    prodi = models.ForeignKey(ProgramStudi, on_delete=models.CASCADE, null=True, blank=True)
    kelas = models.ForeignKey(Kelas, on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return f"{self.dosen.nama} - {self.matkul.nama} ({self.kelas.nama if self.kelas else '-'})"

    class Meta:
        verbose_name_plural = "Dosen Pengampu"

class Jadwal(models.Model):
    dosen_pengampu = models.ForeignKey(DosenPengampu, on_delete=models.CASCADE)
    ruangan = models.ForeignKey(Ruangan, on_delete=models.CASCADE)
    hari = models.ForeignKey(Hari, on_delete=models.CASCADE)
    jam = models.ForeignKey(Jam, on_delete=models.CASCADE)
    kelas = models.ForeignKey(Kelas, on_delete=models.CASCADE)
    is_manual = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.dosen_pengampu.matkul.nama} - {self.hari.nama} {self.jam.range_waktu}"

    class Meta:
        verbose_name_plural = "Jadwal"

class DaftarHadir(models.Model):
    jadwal = models.ForeignKey(Jadwal, on_delete=models.CASCADE)
    tanggal = models.DateField()
    pertemuan_ke = models.IntegerField(default=1)
    keterangan = models.TextField(null=True, blank=True)

    def __str__(self):
        return f"Hadir: {self.jadwal} - Tgl: {self.tanggal}"

    class Meta:
        verbose_name_plural = "Daftar Hadir"
