from django.db import models
from django.db.models.base import Model
from django.urls import reverse

from ckeditor.fields import RichTextField


# Create your models here.
class Post(models.Model):
    content = RichTextField()
    title = models.CharField(max_length=255)
    body = models.TextField(null=True)
    slug = models.SlugField(null=False, unique=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        # return reverse('post_detail', args=[str(self.id)])
        return reverse('post_detail', kwargs={'slug': self.slug})


#################################################################
# one-to-one link
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=100, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)


@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()


#################################################################
# Create superuser

# Using Django Shell
# echo "from django.contrib.auth import get_user_model;
#       User =  get_user_model(); 
#       User.objects.create_superuser('userUsername','userEmail','userPassword')"
#       | python manage.py shell
def createSuperUser(username, password, email="", firstName="", lastName=""):
    invalidInputs = ["", None]

    if username.strip() in invalidInputs or password.strip() in invalidInputs:
        return None

    user = User(
        username = username,
        email = email,
        first_name = firstName,
        last_name = lastName
    )
    user.set_password(password)
    user.is_superuser = True
    user.is_staff = True
    user.save()

    return user


#################################################################

class PegawaiAktif(models.Model):
    nipbaru = models.CharField(primary_key=True, db_column='NIPBARU', null=False, max_length=20)
    nip = models.CharField(db_column='NIP', max_length=20)
    niplama = models.CharField(db_column='NIPLAMA', null=True, max_length=20)
    nama = models.CharField(db_column='NAMA', max_length=200)
    gelardepan = models.CharField(db_column='GELARDEPAN', blank=True, null=True, max_length=50)
    gelarbelakang = models.CharField(db_column='GELARBELAKANG', blank=True, null=True, max_length=50)
    tempatlahir = models.CharField(db_column='TEMPATLAHIR', max_length=50)
    tanggallahir = models.CharField(db_column='TANGGALLAHIR', max_length=50)
    usia = models.IntegerField(db_column='USIA')
    kawin = models.CharField(db_column='KAWIN', blank=True, null=True, max_length=50)
    kodekelamin = models.CharField(db_column='KODEKELAMIN', max_length=50)
    kodeagama = models.IntegerField(db_column='KODEAGAMA')
    nomorkartupegawai = models.CharField(db_column='NOMORKARTUPEGAWAI', blank=True, null=True, max_length=50)
    npwp = models.CharField(db_column='NPWP', blank=True, null=True, max_length=50)
    tanggalmulaijabatan = models.CharField(db_column='TANGGALMULAIJABATAN', blank=True, null=True, max_length=50)
    kodejabatan = models.CharField(db_column='KODEJABATAN', blank=True, null=True, max_length=50)
    esl1 = models.CharField(db_column='ESL1', max_length=50)
    esl2 = models.CharField(db_column='ESL2', max_length=50)
    esl3 = models.CharField(db_column='ESL3', max_length=50)
    esl4 = models.CharField(db_column='ESL4', max_length=50)
    esl5 = models.CharField(db_column='ESL5', max_length=50)
    tanggalskjabatan = models.CharField(db_column='TANGGALSKJABATAN', blank=True, null=True, max_length=50)
    tanggalpelantikan = models.CharField(db_column='TANGGALPELANTIKAN', blank=True, null=True, max_length=50)
    noskjabatan = models.CharField(db_column='NOSKJABATAN', blank=True, null=True, max_length=50)
    namajabatan = models.CharField(db_column='NAMAJABATAN', blank=True, null=True, max_length=50)
    kode_eselonisasi = models.FloatField(db_column='KODE ESELONISASI', blank=True, null=True)
    peringkat = models.IntegerField(db_column='PERINGKAT', blank=True, null=True)
    tanggalmulaipangkat = models.CharField(db_column='TANGGALMULAIPANGKAT', blank=True, null=True, max_length=50)
    pangkatgolongan = models.CharField(db_column='PANGKATGOLONGAN', blank=True, null=True, max_length=50)
    noskpangkat = models.CharField(db_column='NOSKPANGKAT', blank=True, null=True, max_length=50)
    tanggalskpangkat = models.CharField(db_column='TANGGALSKPANGKAT', blank=True, null=True, max_length=50)
    masakerjatahun = models.IntegerField(db_column='MASAKERJATAHUN', blank=True, null=True)
    masakerjabulan = models.IntegerField(db_column='MASAKERJABULAN', blank=True, null=True)
    angka_kredit = models.IntegerField(db_column='ANGKA KREDIT', blank=True, null=True)
    tanggalijazah = models.CharField(db_column='TANGGALIJAZAH', blank=True, null=True, max_length=50)
    nomorijazah = models.CharField(db_column='NOMORIJAZAH', blank=True, null=True, max_length=50)
    jenjang = models.CharField(db_column='JENJANG', blank=True, null=True, max_length=50)
    uraianpendidikan = models.CharField(db_column='URAIANPENDIDIKAN', blank=True, null=True, max_length=50)
    namalembagapendidikan = models.CharField(db_column='NAMALEMBAGAPENDIDIKAN', blank=True, null=True, max_length=50)

    class Meta:
        managed = False
        db_table = 'pegawai_aktif'



#################################################################
# STRUKTUR ORGANISASI
class Organisasi(models.Model):
    esl1 = models.CharField(db_column='ESL1', max_length=50)
    esl2 = models.CharField(db_column='ESL2', max_length=100)
    esl3 = models.CharField(db_column='ESL3', max_length=100)
    is_aktif = models.BooleanField()

    def __str__(self):
        return self.esl3

    def save(self, *args, **kwargs): 
        return super().save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'Organisasi'
        db_table = 'layanan_str_org'


# STRUKTUR ORGANISASI
class Tusi(models.Model):
    esl3 = models.ForeignKey("organisasi", on_delete=models.CASCADE)
    esl4 = models.CharField(db_column='ESL4', max_length=100)
    tugas = models.CharField(db_column='tugas', max_length=1000)
    urjab = models.CharField(db_column='urjab', max_length=1000)

    def __str__(self):
        return self.esl4

    def save(self, *args, **kwargs): 
        return super().save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'Tusi'
        db_table = 'layanan_str_tusi'


#################################################################
# PRODUK GRUP
# class ProdukGrup(models.Model):
#     nama = models.CharField(max_length=100)
#     deskripsi = models.CharField(max_length=100)

#     class Meta:
#         verbose_name_plural = 'ProdukGrup'
#         db_table = 'layanan_produk_grup'

#     def __str__(self):
#         return self.jenis

#     def save(self, *args, **kwargs): 
#         return super().save(*args, **kwargs)

# PRODUK
class Produk(models.Model):
    esl3 = models.ForeignKey("organisasi", on_delete=models.CASCADE)
    esl4 = models.ForeignKey("tusi", on_delete=models.CASCADE)
    judul = models.CharField(db_column='judul', max_length=200)
    deskripsi = RichTextField(null=True, blank=True)
    link = models.CharField(db_column='link', max_length=100, null=True, blank=True)

    import uuid
    slug = models.SlugField(null=True, unique=False, blank=True)


    def __str__(self):
        return self.judul

    # def get_absolute_url(self):
    #     return reverse('post_detail', kwargs={'slug': self.slug})

    def save(self, *args, **kwargs): 
        return super().save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'Produk'
        db_table = 'layanan_produk'


#################################################################
# DOKUMEN PERJANJIAN
class Dokumen(models.Model):
    stakeholders = models.ForeignKey("stakeholder", on_delete=models.CASCADE)
    jns_dok = models.CharField(db_column='jns_dok', max_length=100)
    no_dok = models.CharField(db_column='no_dok', max_length=100)
    output = models.CharField(db_column='output', max_length=100)
    tgl_mulai = models.DateField(db_column='tgl_mulai')
    tgl_selesai = models.DateField(db_column='tgl_selesai')

    def __str__(self):
        return self.stakeholders

    def save(self, *args, **kwargs): 
        return super().save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'Dokumen'
        db_table = 'layanan_dok_perjanjian'

# STAKEHOLDERS
class Stakeholder(models.Model):
    stakeholders = models.CharField(db_column='stakeholders', max_length=50)
    ttg_dok = models.CharField(db_column='ttg_dok', max_length=100)
    output = models.CharField(db_column='output', max_length=100)

    def __str__(self):
        return self.stakeholders

    def save(self, *args, **kwargs): 
        return super().save(*args, **kwargs)

    class Meta:
        verbose_name_plural = 'Stakeholder'
        db_table = 'layanan_stakeholder'
