import django_filters
from core.models import Report, CustomerTemplateMapping, ProductDrawing, MksCodeMap

class ReportFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(
        field_name='name', 
        lookup_expr='icontains', 
        label='Report Name'
    )
    created_by = django_filters.CharFilter(
        field_name='created_by__username', 
        lookup_expr='icontains', 
        label='Created By'
    )
    created_at = django_filters.DateFromToRangeFilter(
        field_name='created_at', 
        label='Created Date Range',
        widget=django_filters.widgets.RangeWidget(
            attrs={
                'type': 'date',
                'class': 'w-full p-2 border border-gray-300 rounded-md focus:outline-none focus:ring focus:ring-blue-300'
            }
        )
    )

    class Meta:
        model = Report
        fields = ['name', 'created_by', 'created_at']


class CustomerTemplateFilter(django_filters.FilterSet):
    customer_name = django_filters.CharFilter(
        field_name='customer_name',
        lookup_expr='icontains',
        label='Customer Name'
    )
    export_template = django_filters.ChoiceFilter(
        field_name='export_template',
        choices=CustomerTemplateMapping.TEMPLATE_TYPE_CHOICES,
        label='Export Template'
    )

    template_names = django_filters.CharFilter(
        method='filter_template_names',
        label='Template Name Contains'
    )

    class Meta:
        model = CustomerTemplateMapping
        fields = ['customer_name', 'export_template', 'template_names']

    def filter_template_names(self, queryset, name, value):
        return queryset.filter(template_names__icontains=value)

class ProductDrawingFilter(django_filters.FilterSet):
    code_no = django_filters.CharFilter(lookup_expr='icontains', label='Code No')
    code_no_mks = django_filters.CharFilter(lookup_expr='icontains', label='Code No (MKS)')
    lot_no = django_filters.CharFilter(lookup_expr='icontains', label='Lot No')

    class Meta:
        model = ProductDrawing
        fields = ['code_no', 'code_no_mks', 'lot_no',]


class MksCodeMapFilter(django_filters.FilterSet):
    mgt_code = django_filters.CharFilter(lookup_expr='icontains', label='MGT Code')
    mks_code = django_filters.CharFilter(lookup_expr='icontains', label='MKS Code')

    class Meta:
        model = MksCodeMap
        fields = ['mgt_code', 'mks_code']
