from django import forms
from .models import Supplier, Faktur, Medicine, Batch, StockTransaction, Category, AppSetting

class AppSettingForm(forms.ModelForm):
    class Meta:
        model = AppSetting
        fields = [
            'app_name', 'app_description', 'app_address', 'app_phone', 'app_email',
            'theme_color', 'font_family', 'font_color', 'sidebar_theme'
        ]
        widgets = {
            'app_name': forms.TextInput(attrs={'class': 'form-control'}),
            'app_description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'app_address': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'app_phone': forms.TextInput(attrs={'class': 'form-control'}),
            'app_email': forms.EmailInput(attrs={'class': 'form-control'}),
            'theme_color': forms.Select(attrs={'class': 'form-select'}),
            'font_family': forms.Select(attrs={'class': 'form-select'}),
            'font_color': forms.TextInput(attrs={'class': 'form-control', 'type': 'color'}),
            'sidebar_theme': forms.Select(attrs={'class': 'form-select'}),
        }

class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        fields = ['name', 'description']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }

class SupplierForm(forms.ModelForm):
    class Meta:
        model = Supplier
        fields = ['name', 'contact_person', 'phone', 'address']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'contact_person': forms.TextInput(attrs={'class': 'form-control'}),
            'phone': forms.TextInput(attrs={'class': 'form-control'}),
            'address': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }

class MedicineForm(forms.ModelForm):
    class Meta:
        model = Medicine
        fields = ['name', 'category', 'sku', 'unit', 'min_stock', 'main_supplier', 'alternative_supplier']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'category': forms.Select(attrs={'class': 'form-control'}),
            'sku': forms.TextInput(attrs={'class': 'form-control'}),
            'unit': forms.Select(attrs={'class': 'form-control'}),
            'min_stock': forms.NumberInput(attrs={'class': 'form-control'}),
            'main_supplier': forms.Select(attrs={'class': 'form-control'}),
            'alternative_supplier': forms.Select(attrs={'class': 'form-control'}),
        }

class FakturForm(forms.ModelForm):
    class Meta:
        model = Faktur
        fields = ['faktur_number', 'supplier', 'date', 'faktur_type', 'notes']
        widgets = {
            'faktur_number': forms.TextInput(attrs={'class': 'form-control'}),
            'supplier': forms.Select(attrs={'class': 'form-control'}),
            'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
            'faktur_type': forms.Select(attrs={'class': 'form-control'}),
            'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
        }

class StockInForm(forms.Form):
    medicine = forms.ModelChoiceField(
        queryset=Medicine.objects.all(),
        label="Nama Barang",
        widget=forms.Select(attrs={'class': 'form-control select2'})
    )
    batch_number = forms.CharField(
        label="Nomor Batch/Lot",
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    expiry_date = forms.DateField(
        label="Tanggal Kadaluarsa",
        widget=forms.DateInput(attrs={'class': 'form-control', 'type': 'date'})
    )
    quantity = forms.IntegerField(
        label="Jumlah",
        widget=forms.NumberInput(attrs={'class': 'form-control'})
    )
    buying_price = forms.DecimalField(
        label="Harga Beli",
        widget=forms.NumberInput(attrs={'class': 'form-control'})
    )
    selling_price = forms.DecimalField(
        label="Harga Jual",
        widget=forms.NumberInput(attrs={'class': 'form-control'})
    )

class StockOutForm(forms.Form):
    medicine = forms.ModelChoiceField(
        queryset=Medicine.objects.all(),
        label="Pilih Barang",
        widget=forms.Select(attrs={'class': 'form-control select2', 'id': 'id_medicine_out'})
    )
    batch = forms.ModelChoiceField(
        queryset=Batch.objects.none(),
        label="Pilih Batch",
        widget=forms.Select(attrs={'class': 'form-control', 'id': 'id_batch_out'})
    )
    quantity = forms.IntegerField(
        label="Jumlah Keluar",
        widget=forms.NumberInput(attrs={'class': 'form-control'})
    )
    note = forms.CharField(
        label="Keterangan",
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if 'medicine' in self.data:
            try:
                medicine_id = int(self.data.get('medicine'))
                self.fields['batch'].queryset = Batch.objects.filter(medicine_id=medicine_id, quantity__gt=0)
            except (ValueError, TypeError):
                pass
        elif self.initial.get('medicine'):
             self.fields['batch'].queryset = Batch.objects.filter(medicine_id=self.initial.get('medicine'), quantity__gt=0)