from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods, require_GET, require_POST
from django.core.paginator import Paginator
from django.db.models import Q
from django.contrib import messages
from django import forms
from .models import Well, WaterMeterManufacturer
from .forms import WellForm, WaterMeterManufacturerForm


def well_list(request):
    """نمایش لیست چاه‌ها"""
    wells = Well.objects.select_related(
        'representative', 
        'water_meter_manufacturer',
        'affairs',
        'county', 
        'broker'
    ).filter(is_deleted=False)
    
    # فرم برای افزودن چاه جدید
    form = WellForm()
    
    context = {
        'wells': wells,
        'form': form,
    }
    
    return render(request, 'wells/well_list.html', context)


@require_POST
def add_well_ajax(request):
    """AJAX endpoint for adding wells"""
    try:
        print(f"POST data: {request.POST}")  # Debug log
        print(f"FILES data: {request.FILES}")  # Debug log
        
        form = WellForm(request.POST, request.FILES)
        print(f"Form is valid: {form.is_valid()}")  # Debug log
        
        if form.is_valid():
            well = form.save(commit=False)
            
            # تنظیم فیلدهای affairs, county, broker از profile کاربر
            if hasattr(request.user, 'profile'):
                well.affairs = request.user.profile.affairs
                well.county = request.user.profile.county
                well.broker = request.user.profile.broker
            
            well.save()
            
            return JsonResponse({
                'success': True,
                'message': 'چاه با موفقیت ایجاد شد',
                'well': {
                    'id': well.id,
                    'water_subscription_number': well.water_subscription_number,
                    'representative': str(well.representative) if well.representative else '',
                    'electricity_subscription_number': well.electricity_subscription_number or '',
                    'water_meter_manufacturer': str(well.water_meter_manufacturer) if well.water_meter_manufacturer else '',
                    'affairs': str(well.affairs) if well.affairs else '',
                    'county': str(well.county) if well.county else '',
                    'broker': str(well.broker) if well.broker else '',
                }
            })
        else:
            print(f"Form errors: {form.errors}")  # Debug log
            return JsonResponse({
                'success': False,
                'message': 'خطا در اعتبارسنجی فرم',
                'errors': form.errors
            })
    except forms.ValidationError as e:
        return JsonResponse({
            'success': False,
            'message': str(e)
        })
    except Exception as e:
        print(f"Exception in add_well_ajax: {str(e)}")  # Debug log
        return JsonResponse({
            'success': False,
            'message': f'خطا در ذخیره چاه: {str(e)}',
            'errors': {}
        })


@require_POST
def edit_well_ajax(request, well_id):
    """AJAX endpoint for editing wells"""
    well = get_object_or_404(Well, id=well_id)
    form = WellForm(request.POST, request.FILES, instance=well)
    
    if form.is_valid():
        try:
            well = form.save(commit=False)
            
            # تنظیم فیلدهای affairs, county, broker از profile کاربر (در ویرایش هم می‌تواند به‌روزرسانی شود)
            if hasattr(request.user, 'profile'):
                well.affairs = request.user.profile.affairs
                well.county = request.user.profile.county
                well.broker = request.user.profile.broker
            
            # حذف فایل اگر درخواست شده باشد
            if request.POST.get('remove_file') == 'true':
                well.representative_letter_file.delete(save=False)
            
            well.save()
            
            return JsonResponse({
                'success': True,
                'message': 'چاه با موفقیت ویرایش شد',
                'well': {
                    'id': well.id,
                    'water_subscription_number': well.water_subscription_number,
                    'representative': str(well.representative) if well.representative else '',
                    'electricity_subscription_number': well.electricity_subscription_number or '',
                    'water_meter_manufacturer': str(well.water_meter_manufacturer) if well.water_meter_manufacturer else '',
                    'affairs': str(well.affairs) if well.affairs else '',
                    'county': str(well.county) if well.county else '',
                    'broker': str(well.broker) if well.broker else '',
                }
            })
        except forms.ValidationError as e:
            return JsonResponse({
                'success': False,
                'message': str(e)
            })
        except Exception as e:
            return JsonResponse({
                'success': False,
                'message': f'خطا در ویرایش چاه: {str(e)}'
            })
    else:
        return JsonResponse({
            'success': False,
            'message': 'خطا در اعتبارسنجی فرم',
            'errors': form.errors
        })


@require_POST
def delete_well(request, well_id):
    """حذف چاه"""
    well = get_object_or_404(Well, id=well_id)
    water_subscription_number = well.water_subscription_number
    well.delete()
    print(f"Well deleted: {well_id}")
    return JsonResponse({
        'success': True,
        'message': f'چاه {water_subscription_number} با موفقیت حذف شد'
    })


@require_GET
def get_well_data(request, well_id):
    """دریافت اطلاعات چاه برای ویرایش"""
    well = get_object_or_404(Well, id=well_id)
    
    return JsonResponse({
        'success': True,
        'well': {
            'id': well.id,
            'representative': well.representative.id if well.representative else '',
            'water_subscription_number': well.water_subscription_number,
            'electricity_subscription_number': well.electricity_subscription_number or '',
            'water_meter_serial_number': well.water_meter_serial_number or '',
            'water_meter_old_serial_number': well.water_meter_old_serial_number or '',
            'water_meter_manufacturer': well.water_meter_manufacturer.id if well.water_meter_manufacturer else '',
            'utm_x': str(well.utm_x) if well.utm_x else '',
            'utm_y': str(well.utm_y) if well.utm_y else '',
            'utm_zone': well.utm_zone or '',
            'utm_hemisphere': well.utm_hemisphere or '',
            'well_power': well.well_power or '',
            'reference_letter_number': well.reference_letter_number or '',
            'reference_letter_date': well.reference_letter_date.strftime('%Y-%m-%d') if well.reference_letter_date else '',
            'representative_letter_file_url': well.representative_letter_file.url if well.representative_letter_file else '',
            'representative_letter_file_name': well.representative_letter_file.name.split('/')[-1] if well.representative_letter_file else '',
            # affairs, county, broker are auto-filled from user profile, so not needed in edit form
        }
    })


@require_POST
def create_water_meter_manufacturer(request):
    """ایجاد شرکت سازنده کنتور آب جدید"""
    form = WaterMeterManufacturerForm(request.POST)
    
    if form.is_valid():
        manufacturer = form.save()
        return JsonResponse({
            'success': True,
            'message': 'شرکت سازنده با موفقیت ایجاد شد',
            'manufacturer': {
                'id': manufacturer.id,
                'name': manufacturer.name
            }
        })
    else:
        return JsonResponse({
            'success': False,
            'message': 'خطا در اعتبارسنجی فرم',
            'errors': form.errors
        })

# Create your views here.
