from django import forms

from property_manager_app.models import (
    PropertyCarePlan,
    PropertyMaintenanceVisit,
    PropertyVisitPhoto,
    PropertyInspectionReport,
    PropertyInspectionReportPhoto,
)


class PropertyCarePlanForm(forms.ModelForm):
    class Meta:
        model = PropertyCarePlan

        fields = [
            'start_date',
            'general_exclusions',
            'plan_notes',
        ]

        widgets = {
            'start_date': forms.DateInput(attrs={
                'class': 'form-control',
                'type': 'date',
            }),

            'general_exclusions': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': (
                    'Example: Major repairs, materials, specialist contractor work, '
                    'renovations, pest control, emergency replacement items, etc.'
                )
            }),

            'plan_notes': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': (
                    'Internal staff plan note or owner-facing final care plan summary.'
                )
            }),
        }

        labels = {
            'start_date': 'Approved Plan Start Date',
            'general_exclusions': 'General Plan Exclusions',
            'plan_notes': 'Final Plan Notes',
        }

class PropertyMaintenanceVisitForm(forms.ModelForm):
    class Meta:
        model = PropertyMaintenanceVisit

        fields = [
            'care_plan',
            'related_service_type',
            'visit_title',
            'visit_type',
            'visit_status',
            'scheduled_date',
            'scheduled_time',
            'completed_at',
            'visited_by',
            'visit_summary',
            'owner_visible_note',
            'internal_note',
            'is_owner_visible',
        ]

        widgets = {
            'care_plan': forms.Select(attrs={
                'class': 'form-select',
            }),

            'related_service_type': forms.Select(attrs={
                'class': 'form-select',
            }),

            'visit_title': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Example: Monthly Inspection Visit - June 2026',
            }),

            'visit_type': forms.Select(attrs={
                'class': 'form-select',
            }),

            'visit_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'scheduled_date': forms.DateInput(attrs={
                'class': 'form-control',
                'type': 'date',
            }),

            'scheduled_time': forms.TimeInput(attrs={
                'class': 'form-control',
                'type': 'time',
            }),

            'completed_at': forms.DateTimeInput(attrs={
                'class': 'form-control',
                'type': 'datetime-local',
            }),

            'visited_by': forms.Select(attrs={
                'class': 'form-select',
            }),

            'visit_summary': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': 'Summarize what was completed during this visit.',
            }),

            'owner_visible_note': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 4,
                'placeholder': 'Note that may later be shown to the property owner.',
            }),

            'internal_note': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 4,
                'placeholder': 'Internal staff-only note.',
            }),

            'is_owner_visible': forms.CheckboxInput(attrs={
                'class': 'form-check-input',
            }),
        }

        labels = {
            'care_plan': 'Related Final Care Plan',
            'related_service_type': 'Related Service Type',
            'visit_title': 'Visit Title',
            'visit_type': 'Visit Type',
            'visit_status': 'Visit Status',
            'scheduled_date': 'Scheduled Date',
            'scheduled_time': 'Scheduled Time',
            'completed_at': 'Completed At',
            'visited_by': 'Visited By',
            'visit_summary': 'Visit Summary',
            'owner_visible_note': 'Owner Visible Note',
            'internal_note': 'Internal Staff Note',
            'is_owner_visible': 'Visible to Owner Later',
        }

    def __init__(self, *args, **kwargs):
        managed_property = kwargs.pop('managed_property', None)
        super().__init__(*args, **kwargs)

        if managed_property:
            self.fields['care_plan'].queryset = PropertyCarePlan.objects.filter(
                property=managed_property
            )

            self.fields['related_service_type'].queryset = (
                managed_property.selected_services.all().order_by('display_order', 'name')
            )

class PropertyVisitPhotoForm(forms.ModelForm):
    class Meta:
        model = PropertyVisitPhoto

        fields = [
            'image',
            'photo_type',
            'caption',
            'is_owner_visible',
        ]

        widgets = {
            'image': forms.ClearableFileInput(attrs={
                'class': 'form-control',
                'accept': 'image/jpeg,image/jpg,image/png,image/webp',
            }),

            'photo_type': forms.Select(attrs={
                'class': 'form-select',
            }),

            'caption': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Optional short caption for this visit photo',
            }),

            'is_owner_visible': forms.CheckboxInput(attrs={
                'class': 'form-check-input',
            }),
        }

        labels = {
            'image': 'Visit Photo',
            'photo_type': 'Photo Type',
            'caption': 'Caption',
            'is_owner_visible': 'Visible to Property Owner',
        }

        help_texts = {
            'image': (
                'Upload a clear visit image. Supported image types are validated '
                'and the file will be compressed automatically after upload.'
            ),
            'photo_type': (
                'Choose whether this is a before, after, issue, general, '
                'completion proof, or other photo.'
            ),
            'caption': (
                'Optional note to explain what this image shows.'
            ),
            'is_owner_visible': (
                'Enable this when the photo may later appear in the owner report center.'
            ),
        }

class PropertyInspectionReportForm(forms.ModelForm):
    class Meta:
        model = PropertyInspectionReport

        fields = [
            'report_title',
            'report_status',
            'inspection_date',

            'overall_condition',
            'cleanliness_status',
            'garden_status',
            'security_status',
            'utility_status',
            'structural_status',

            'issues_found',
            'recommendations',
            'urgent_action_required',
            'owner_visible_summary',
            'internal_staff_note',
        ]

        widgets = {
            'report_title': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Example: Monthly Property Inspection Report - June 2026',
            }),

            'report_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'inspection_date': forms.DateInput(attrs={
                'class': 'form-control',
                'type': 'date',
            }),

            'overall_condition': forms.Select(attrs={
                'class': 'form-select',
            }),

            'cleanliness_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'garden_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'security_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'utility_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'structural_status': forms.Select(attrs={
                'class': 'form-select',
            }),

            'issues_found': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': 'Describe any concerns, defects, unusual conditions, or issues found during inspection.',
            }),

            'recommendations': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': 'Recommend repairs, future checks, cleaning, urgent follow-up, or owner decisions.',
            }),

            'urgent_action_required': forms.CheckboxInput(attrs={
                'class': 'form-check-input',
            }),

            'owner_visible_summary': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': 'Owner-facing summary that may later appear in the property owner report center.',
            }),

            'internal_staff_note': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 5,
                'placeholder': 'Internal Ensolanka note visible only to staff.',
            }),
        }

        labels = {
            'report_title': 'Report Title',
            'report_status': 'Report Status',
            'inspection_date': 'Inspection Date',

            'overall_condition': 'Overall Property Condition',
            'cleanliness_status': 'Cleanliness Status',
            'garden_status': 'Garden / Outdoor Status',
            'security_status': 'Security Status',
            'utility_status': 'Utility Status',
            'structural_status': 'Structural Status',

            'issues_found': 'Issues Found',
            'recommendations': 'Recommendations',
            'urgent_action_required': 'Urgent Action Required',
            'owner_visible_summary': 'Owner Visible Summary',
            'internal_staff_note': 'Internal Staff Note',
        }

        help_texts = {
            'report_status': (
                'Draft reports remain internal. Ready for Review means the report is prepared for management review. '
                'Published will later be handled through a dedicated publish workflow.'
            ),
            'urgent_action_required': (
                'Enable this if the inspection reveals a concern that needs quick owner or staff attention.'
            ),
            'owner_visible_summary': (
                'Write this carefully because it may later be shown directly to the property owner.'
            ),
            'internal_staff_note': (
                'Use this for operational remarks, follow-up planning, or internal-only staff observations.'
            ),
        }

class PropertyInspectionReportPhotoForm(forms.ModelForm):
    class Meta:
        model = PropertyInspectionReportPhoto

        fields = [
            'image',
            'photo_type',
            'caption',
            'is_owner_visible',
        ]

        widgets = {
            'image': forms.ClearableFileInput(attrs={
                'class': 'form-control',
                'accept': 'image/jpeg,image/jpg,image/png,image/webp',
            }),

            'photo_type': forms.Select(attrs={
                'class': 'form-select',
            }),

            'caption': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Optional caption describing what this photo shows',
            }),

            'is_owner_visible': forms.CheckboxInput(attrs={
                'class': 'form-check-input',
            }),
        }

        labels = {
            'image': 'Inspection Report Photo',
            'photo_type': 'Photo Type',
            'caption': 'Caption',
            'is_owner_visible': 'Visible to Property Owner',
        }

        help_texts = {
            'image': (
                'Upload a clear report image. Images are validated and compressed automatically after upload.'
            ),
            'photo_type': (
                'Choose how this photo relates to the inspection report.'
            ),
            'caption': (
                'Optional short description for staff reference or future owner display.'
            ),
            'is_owner_visible': (
                'Enable this when the photo may later be shown in the owner report center.'
            ),
        }