from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.urls import reverse
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
from django.contrib.auth.decorators import login_required
from common.decorators import allowed_roles
from common.consts import UserRoles
from processes.utils import scope_wells_queryset
from processes.models import ProcessInstance

@login_required
@allowed_roles([UserRoles.ADMIN, UserRoles.BROKER, UserRoles.MANAGER, UserRoles.ACCOUNTANT, UserRoles.WATER_RESOURCE_MANAGER])
def well_list(request):
    """نمایش لیست چاه‌ها"""
    base = Well.objects.select_related(
        'representative', 
        'water_meter_manufacturer',
        'affairs',
        'county', 
        'broker'
    ).filter(is_deleted=False)
    wells = scope_wells_queryset(request.user, base)
    
    # فرم برای افزودن چاه جدید
    form = WellForm()
    
    context = {
        'wells': wells,
        'form': form,
    }
    
    return render(request, 'wells/well_list.html', context)


@require_POST
@login_required
@allowed_roles([UserRoles.ADMIN, UserRoles.BROKER, UserRoles.MANAGER, UserRoles.ACCOUNTANT, UserRoles.WATER_RESOURCE_MANAGER])
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
@login_required
@allowed_roles([UserRoles.ADMIN, UserRoles.BROKER, UserRoles.MANAGER, UserRoles.ACCOUNTANT, UserRoles.WATER_RESOURCE_MANAGER])
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
@login_required
@allowed_roles([UserRoles.ADMIN, UserRoles.BROKER, UserRoles.MANAGER, UserRoles.ACCOUNTANT, UserRoles.WATER_RESOURCE_MANAGER])
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
@login_required
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_GET
@login_required
@allowed_roles([UserRoles.ADMIN, UserRoles.BROKER, UserRoles.MANAGER, UserRoles.ACCOUNTANT, UserRoles.WATER_RESOURCE_MANAGER])
def get_well_details(request, well_id):
    """جزئیات کامل چاه برای نمایش در مدال"""
    well = get_object_or_404(
        Well.objects.select_related(
            'representative', 'water_meter_manufacturer', 'affairs', 'county', 'broker'
        ),
        id=well_id
    )

    lat_long = None
    try:
        lat_long_val = well.lat_long()
        if lat_long_val:
            # utm.to_latlon returns (lat, lon)
            lat_long = {
                'lat': round(float(lat_long_val[0]), 6),
                'lon': round(float(lat_long_val[1]), 6),
            }
    except Exception:
        lat_long = None

    data = {
        'id': well.id,
        'water_subscription_number': well.water_subscription_number,
        'electricity_subscription_number': well.electricity_subscription_number or '',
        'representative': {
            'id': well.representative.id if well.representative else None,
            'full_name': well.representative.get_full_name() if well.representative else '',
            'username': well.representative.username if well.representative else '',
        },
        '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': str(well.water_meter_manufacturer) if well.water_meter_manufacturer else '',
        'utm': {
            'x': str(well.utm_x) if well.utm_x is not None else '',
            'y': str(well.utm_y) if well.utm_y is not None else '',
            'zone': well.utm_zone or '',
            'hemisphere': well.utm_hemisphere or '',
        },
        'lat_long': lat_long,
        '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 '',
        'affairs': str(well.affairs) if well.affairs else '',
        'county': str(well.county) if well.county else '',
        'broker': str(well.broker) if well.broker else '',
    }

    # تعداد درخواست‌ها برای نمایش سریع
    try:
        total_requests = ProcessInstance.objects.filter(well_id=well.id, is_deleted=False).count()
    except Exception:
        total_requests = 0

    return JsonResponse({'success': True, 'well': data, 'total_requests': total_requests})


@require_GET
@login_required
@allowed_roles([UserRoles.ADMIN, UserRoles.BROKER, UserRoles.MANAGER, UserRoles.ACCOUNTANT, UserRoles.WATER_RESOURCE_MANAGER])
def get_well_requests(request, well_id):
    """سوابق درخواست‌های مرتبط با یک چاه"""
    # Scoped access: reuse base scoping by filtering on ProcessInstance via broker/affairs of current user if needed
    qs = ProcessInstance.objects.select_related(
        'process', 'current_step', 'requester', 'representative'
    ).filter(well_id=well_id, is_deleted=False).order_by('-created')

    items = []
    for inst in qs[:100]:  # محدودسازی برای عملکرد
        try:
            url = reverse('processes:instance_summary', args=[inst.id]) if inst.status == 'completed' else reverse('processes:instance_steps', args=[inst.id])
        except Exception:
            url = ''
        items.append({
            'id': inst.id,
            'code': inst.code,
            'process': inst.process.name if inst.process else '',
            'status': inst.status,
            'status_display': inst.get_status_display(),
            'priority': inst.priority,
            'priority_display': inst.get_priority_display(),
            'current_step': inst.current_step.name if inst.current_step else '',
            'requester': inst.requester.get_full_name() if inst.requester else '',
            'representative': inst.representative.get_full_name() if inst.representative else '',
            'created': inst.jcreated_date() if hasattr(inst, 'created') and inst.created else '',
            'url': url,
        })

    return JsonResponse({'success': True, 'requests': items})


@require_POST
@login_required
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.
