import os

file_path = 'core/views.py'

with open(file_path, 'r') as f:
    content = f.read()

# Replacement 1: sale_return_create
old_sale_create = "def sale_return_create(request): return render(request, 'core/sale_return_create.html')"
new_sale_create = """def sale_return_create(request):
    customers = Customer.objects.all()
    products = Product.objects.filter(is_active=True)
    return render(request, 'core/sale_return_create.html', {
        'customers': customers, 
        'products': products
    })"""

# Replacement 2: create_sale_return_api
old_sale_api = "@csrf_exempt\ndef create_sale_return_api(request): return JsonResponse({'success': True})"
new_sale_api = """@csrf_exempt
@login_required
def create_sale_return_api(request):
    if request.method != 'POST':
        return JsonResponse({'success': False, 'error': 'Invalid method'})
    try:
        data = json.loads(request.body)
        customer_id = data.get('customer_id')
        items = data.get('items', [])
        
        customer = None
        if customer_id:
            customer = get_object_or_404(Customer, pk=customer_id)
            
        with transaction.atomic():
            sale_return = SaleReturn.objects.create(
                customer=customer,
                created_by=request.user,
                total_amount=0,
                return_number=f"SR-{{int(timezone.now().timestamp())}}",
                notes=data.get('notes', '')
            )
            
            total = decimal.Decimal(0)
            for item in items:
                qty = decimal.Decimal(str(item.get('quantity', 0)))
                price = decimal.Decimal(str(item.get('price', 0)))
                line_total = qty * price
                
                SaleReturnItem.objects.create(
                    sale_return=sale_return,
                    product_id=item['id'],
                    quantity=qty,
                    unit_price=price,
                    line_total=line_total
                )
                
                # Update stock: Returns from customer mean stock comes IN
                product = Product.objects.get(pk=item['id'])
                product.stock_quantity += qty
                product.save()
                
                total += line_total
            
            sale_return.total_amount = total
            sale_return.save()
            
        return JsonResponse({'success': True, 'id': sale_return.id})
    except Exception as e:
        logger.exception("Error creating sale return")
        return JsonResponse({'success': False, 'error': str(e)})"""

# Replacement 3: purchase_return_create
old_purchase_create = "def purchase_return_create(request): return render(request, 'core/purchase_return_create.html')"
new_purchase_create = """def purchase_return_create(request):
    suppliers = Supplier.objects.filter(is_active=True)
    products = Product.objects.filter(is_active=True)
    return render(request, 'core/purchase_return_create.html', {
        'suppliers': suppliers, 
        'products': products
    })"""

# Replacement 4: create_purchase_return_api
old_purchase_api = "@csrf_exempt\ndef create_purchase_return_api(request): return JsonResponse({'success': True})"
new_purchase_api = """@csrf_exempt
@login_required
def create_purchase_return_api(request):
    if request.method != 'POST':
        return JsonResponse({'success': False, 'error': 'Invalid method'})
    try:
        data = json.loads(request.body)
        supplier_id = data.get('supplier_id')
        items = data.get('items', [])
        
        supplier = get_object_or_404(Supplier, pk=supplier_id)
        
        with transaction.atomic():
            purchase_return = PurchaseReturn.objects.create(
                supplier=supplier,
                created_by=request.user,
                total_amount=0,
                return_number=f"PR-{{int(timezone.now().timestamp())}}",
                notes=data.get('notes', '')
            )
            
            total = decimal.Decimal(0)
            for item in items:
                qty = decimal.Decimal(str(item.get('quantity', 0)))
                cost = decimal.Decimal(str(item.get('price', 0)))
                line_total = qty * cost
                
                PurchaseReturnItem.objects.create(
                    purchase_return=purchase_return,
                    product_id=item['id'],
                    quantity=qty,
                    cost_price=cost,
                    line_total=line_total
                )
                
                # Update stock: Returns to supplier mean stock goes OUT
                product = Product.objects.get(pk=item['id'])
                product.stock_quantity -= qty
                product.save()
                
                total += line_total
            
            purchase_return.total_amount = total
            purchase_return.save()
            
        return JsonResponse({'success': True, 'id': purchase_return.id})
    except Exception as e:
        logger.exception("Error creating purchase return")
        return JsonResponse({'success': False, 'error': str(e)})"""

if old_sale_create in content:
    content = content.replace(old_sale_create, new_sale_create)
    print("Patched sale_return_create")
else:
    print("Could not find sale_return_create stub")

if old_sale_api in content:
    content = content.replace(old_sale_api, new_sale_api)
    print("Patched create_sale_return_api")
else:
    print("Could not find create_sale_return_api stub")

if old_purchase_create in content:
    content = content.replace(old_purchase_create, new_purchase_create)
    print("Patched purchase_return_create")
else:
    print("Could not find purchase_return_create stub")

if old_purchase_api in content:
    content = content.replace(old_purchase_api, new_purchase_api)
    print("Patched create_purchase_return_api")
else:
    print("Could not find create_purchase_return_api stub")

with open(file_path, 'w') as f:
    f.write(content)
