"""
Parts forms module for lara_django_material.

:PROJECT: LARAsuite

:details: Django forms for Part and PartShape models
         optimized for Python 3.13+ with ClassVar pattern.

:authors: mark doerr <mark.doerr@uni-greifswald.de>

.. note:: Forms for parts management
.. todo:: -
"""

from typing import ClassVar

from django import forms

from ..models import Part, PartShape
from .base import CustomFormRenderer, SearchableSelectMultiple

## -------------- PartShape -------------- ##


class PartShapeCreateForm(forms.ModelForm):
    """Form for creating new PartShape entries."""

    default_renderer: ClassVar = CustomFormRenderer()

    class Meta:
        """Meta configuration for PartShapeCreateForm."""

        model = PartShape
        fields = (
            "name",
            "name_standard",
            "iri",
            "width",
            "length",
            "height_unlidded",
            "hight_lidded",
            "hight_stacked",
            "hight_stacked_lidded",
            "model_2d",
            "model_3d",
            "model_json",
            "data_extra",
            "description",
        )

        text_fields_required: ClassVar = ("name",)
        text_fields: ClassVar = ("name_standard",)
        textarea_fields: ClassVar = ("model_2d", "model_3d", "description")
        number_fields: ClassVar = (
            "width",
            "length",
            "height_unlidded",
            "hight_lidded",
            "hight_stacked",
            "hight_stacked_lidded",
        )
        url_fields: ClassVar = ("iri",)

        widgets: ClassVar = {
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full", "required": True})
                for field in text_fields_required
            },
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in text_fields
            },
            **{
                field: forms.Textarea(attrs={"class": "textarea textarea-sm textarea-bordered w-full", "rows": 3})
                for field in textarea_fields
            },
            **{
                field: forms.NumberInput(attrs={"class": "input input-sm input-bordered w-full"})
                for field in number_fields
            },
            **{field: forms.URLInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in url_fields},
            "model_json": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 6}
            ),
            "data_extra": SearchableSelectMultiple(),
        }


class PartShapeUpdateForm(forms.ModelForm):
    """Form for updating existing PartShape entries."""

    default_renderer: ClassVar = CustomFormRenderer()

    class Meta:
        """Meta configuration for PartShapeUpdateForm."""

        model = PartShape
        fields = (
            "name",
            "name_standard",
            "iri",
            "width",
            "length",
            "height_unlidded",
            "hight_lidded",
            "hight_stacked",
            "hight_stacked_lidded",
            "model_2d",
            "model_3d",
            "model_json",
            "data_extra",
            "description",
        )

        text_fields_required: ClassVar = ("name",)
        text_fields: ClassVar = ("name_standard",)
        textarea_fields: ClassVar = ("model_2d", "model_3d", "description")
        number_fields: ClassVar = (
            "width",
            "length",
            "height_unlidded",
            "hight_lidded",
            "hight_stacked",
            "hight_stacked_lidded",
        )
        url_fields: ClassVar = ("iri",)

        widgets: ClassVar = {
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full", "required": True})
                for field in text_fields_required
            },
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in text_fields
            },
            **{
                field: forms.Textarea(attrs={"class": "textarea textarea-sm textarea-bordered w-full", "rows": 3})
                for field in textarea_fields
            },
            **{
                field: forms.NumberInput(attrs={"class": "input input-sm input-bordered w-full"})
                for field in number_fields
            },
            **{field: forms.URLInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in url_fields},
            "model_json": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 6}
            ),
            "data_extra": SearchableSelectMultiple(),
        }


## -------------- Part -------------- ##


class PartCreateForm(forms.ModelForm):
    """Form for creating new Part entries."""

    default_renderer: ClassVar = CustomFormRenderer()

    class Meta:
        """Meta configuration for PartCreateForm."""

        model = Part
        fields = (
            "name",
            "name_display",
            "url",
            "pid",
            "pac_id",
            "iri",
            "manufacturer",
            "model_no",
            "product_type",
            "type_barcode",
            "type_barcode_json",
            "product_no",
            "weight",
            "energy_consumption",
            "spec_json",
            "hardware_config",
            "icon",
            "image",
            "resources_external",
            "tags",
            "description",
            "part_class",
            "shape",
            "components",
            "data_extra",
        )

        text_fields_required: ClassVar = ("name",)
        text_fields: ClassVar = (
            "name_display",
            "model_no",
            "product_type",
            "type_barcode",
            "product_no",
        )
        textarea_fields: ClassVar = ("icon", "description")
        number_fields: ClassVar = ("weight", "energy_consumption")
        url_fields: ClassVar = ("url", "pid", "pac_id", "iri")

        widgets: ClassVar = {
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full", "required": True})
                for field in text_fields_required
            },
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in text_fields
            },
            **{
                field: forms.Textarea(attrs={"class": "textarea textarea-sm textarea-bordered w-full", "rows": 3})
                for field in textarea_fields
            },
            **{
                field: forms.NumberInput(attrs={"class": "input input-sm input-bordered w-full"})
                for field in number_fields
            },
            **{field: forms.URLInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in url_fields},
            "manufacturer": forms.Select(attrs={"class": "select select-sm select-bordered w-full"}),
            "part_class": forms.Select(attrs={"class": "select select-sm select-bordered w-full"}),
            "shape": forms.Select(attrs={"class": "select select-sm select-bordered w-full"}),
            "spec_json": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 6}
            ),
            "hardware_config": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 6}
            ),
            "type_barcode_json": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 3}
            ),
            "resources_external": SearchableSelectMultiple(),
            "tags": SearchableSelectMultiple(),
            "components": SearchableSelectMultiple(),
            "data_extra": SearchableSelectMultiple(),
            "image": forms.ClearableFileInput(attrs={"class": "file-input file-input-sm file-input-bordered w-full"}),
        }


class PartUpdateForm(forms.ModelForm):
    """Form for updating existing Part entries."""

    default_renderer: ClassVar = CustomFormRenderer()

    class Meta:
        """Meta configuration for PartUpdateForm."""

        model = Part
        fields = (
            "name",
            "name_display",
            "url",
            "pid",
            "pac_id",
            "iri",
            "manufacturer",
            "model_no",
            "product_type",
            "type_barcode",
            "type_barcode_json",
            "product_no",
            "weight",
            "energy_consumption",
            "spec_json",
            "hardware_config",
            "icon",
            "image",
            "resources_external",
            "tags",
            "description",
            "part_class",
            "shape",
            "components",
            "data_extra",
        )

        text_fields_required: ClassVar = ("name",)
        text_fields: ClassVar = (
            "name_display",
            "model_no",
            "product_type",
            "type_barcode",
            "product_no",
        )
        textarea_fields: ClassVar = ("icon", "description")
        number_fields: ClassVar = ("weight", "energy_consumption")
        url_fields: ClassVar = ("url", "pid", "pac_id", "iri")

        widgets: ClassVar = {
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full", "required": True})
                for field in text_fields_required
            },
            **{
                field: forms.TextInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in text_fields
            },
            **{
                field: forms.Textarea(attrs={"class": "textarea textarea-sm textarea-bordered w-full", "rows": 3})
                for field in textarea_fields
            },
            **{
                field: forms.NumberInput(attrs={"class": "input input-sm input-bordered w-full"})
                for field in number_fields
            },
            **{field: forms.URLInput(attrs={"class": "input input-sm input-bordered w-full"}) for field in url_fields},
            "manufacturer": forms.Select(attrs={"class": "select select-sm select-bordered w-full"}),
            "part_class": forms.Select(attrs={"class": "select select-sm select-bordered w-full"}),
            "shape": forms.Select(attrs={"class": "select select-sm select-bordered w-full"}),
            "spec_json": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 6}
            ),
            "hardware_config": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 6}
            ),
            "type_barcode_json": forms.Textarea(
                attrs={"class": "textarea textarea-sm textarea-bordered w-full font-mono", "rows": 3}
            ),
            "resources_external": SearchableSelectMultiple(),
            "tags": SearchableSelectMultiple(),
            "components": SearchableSelectMultiple(),
            "data_extra": SearchableSelectMultiple(),
            "image": forms.ClearableFileInput(attrs={"class": "file-input file-input-sm file-input-bordered w-full"}),
        }
