# app/utils/fix_images.py

import os
from flask import current_app
from app.extensions import db
from app.models import AdvertisementRequest


def fix_advertisement_images():
    """بررسی و تعمیر مسیرهای تصاویر تبلیغات"""
    
    ads = AdvertisementRequest.query.all()
    fixed_count = 0
    
    for ad in ads:
        if ad.image_path:
            # بررسی وجود فایل
            full_path = os.path.join(current_app.static_folder, ad.image_path)
            
            if not os.path.exists(full_path):
                print(f"⚠️ فایل پیدا نشد: {ad.image_path}")
                print(f"   عنوان: {ad.title}")
                print(f"   ID: {ad.id}")
                
                # تصویر پیش‌فرض
                # ad.image_path = None
                # db.session.commit()
                fixed_count += 1
    
    print(f"\n✅ {fixed_count} تبلیغ نیاز به تعمیر دارند.")
    return fixed_count