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 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/env python3
"""
🇸🇦 VortexCommerce Product Quality Checker
Validates product images, calculates quality scores, and manages soft-delete workflow.
"""
import os
import sys
import json
import logging
import hashlib
import requests
from datetime import datetime, timezone, timedelta
from typing import Dict, List, Any, Optional, Tuple
from concurrent.futures import ThreadPoolExecutor, as_completed
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

if __name__ == "__main__":
    import os
    import sys
    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

try:
    from app.db.base import engine, SessionLocal, Base
    from app.models.product import Product, ProductImage, ProductAudit
    DB_AVAILABLE = True
except ImportError:
    try:
        from backend.app.db.base import engine, SessionLocal, Base
        from backend.app.models.product import Product, ProductImage, ProductAudit
        DB_AVAILABLE = True
    except ImportError:
        # Standalone mode - we'll define mock models if needed or just skip DB parts
        engine = None
        SessionLocal = None
        Base = None
        Product = None
        DB_AVAILABLE = False


try:
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler(f'product_quality_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log'),
            logging.StreamHandler()
        ]
    )
except Exception:
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s',
        handlers=[logging.StreamHandler()]
    )
logger = logging.getLogger(__name__)


class QualityReport:
    """Quality check report container."""
    
    def __init__(self):
        self.timestamp = datetime.now(timezone.utc)
        self.total_checked = 0
        self.total_valid = 0
        self.total_invalid = 0
        self.deleted_products: List[Dict[str, Any]] = []
        self.updated_scores: List[Dict[str, Any]] = []
        self.errors: List[str] = []
        
    def to_dict(self) -> Dict[str, Any]:
        return {
            "timestamp": self.timestamp.isoformat(),
            "total_checked": self.total_checked,
            "total_valid": self.total_valid,
            "total_invalid": self.total_invalid,
            "deleted_products": self.deleted_products,
            "updated_scores": self.updated_scores,
            "errors": self.errors
        }


class ProductQualityChecker:
    """Comprehensive product quality validation system."""
    
    def __init__(self, db_session=None):
        self.db = db_session
        self.report = QualityReport()
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'VortexCommerce-QualityChecker/1.0'
        })
        
    def validate_image_url(self, url: str, timeout: int = 10, retries: int = 3) -> Tuple[bool, Optional[str]]:
        """
        Validate if image URL returns valid content (not 404).
        Returns (is_valid, error_message).
        """
        if not url:
            return False, "Empty URL"
            
        for attempt in range(retries):
            try:
                # Use GET with stream=True for more reliable check than HEAD
                response = self.session.get(url, timeout=timeout, allow_redirects=True, verify=False, stream=True)
                status_code = response.status_code
                content_type = response.headers.get('Content-Type', '')
                response.close()
                
                if status_code == 200 and content_type.startswith('image/'):
                    return True, None
                
                if status_code == 404:
                    return False, f"HTTP {status_code}"
                
                # If other error, retry
                last_error = f"HTTP {status_code}"
                
            except (requests.exceptions.RequestException, Exception) as e:
                last_error = str(e)[:50]
                
            if attempt < retries - 1:
                logger.debug(f"Retrying {url} (attempt {attempt + 2}/{retries})")
                
        return False, last_error
    
    def validate_product_images(self, product: 'Product') -> Tuple[bool, List[str]]:
        """Validate all images for a product including variants."""
        errors = []
        
        # Check primary images
        if not product.images:
            errors.append("No primary images attached to product")
        else:
            for img in product.images:
                is_valid, error = self.validate_image_url(img.image_url)
                if not is_valid:
                    errors.append(f"Primary Image {img.id}: {error}")
        
        # Check variant images if present in specs
        if product.specs and isinstance(product.specs, dict) and 'variants' in product.specs:
            variants = product.specs['variants']
            for i, var in enumerate(variants):
                img_url = var.get('image_url') or var.get('image')
                if img_url:
                    is_valid, error = self.validate_image_url(img_url)
                    if not is_valid:
                        errors.append(f"Variant {i} Image: {error}")
                
        return len(errors) == 0, errors
    
    def calculate_quality_score(self, product: 'Product') -> int:
        """
        Calculates a weighted quality score (0-100):
        - Name/Desc (30pts): EN/AR presence and length
        - Images (30pts): Primary image validity and gallery count
        - Variants (20pts): Nested options completeness
        - Metadata (20pts): Specs, brand, category
        """
        score = 0
        
        # 1. Names & Descriptions (30 pts)
        if product.name_en and len(product.name_en) > 10: score += 7
        if product.name_ar and len(product.name_ar) > 10: score += 8
        if product.description_en and len(product.description_en) > 50: score += 7
        if product.description_ar and len(product.description_ar) > 50: score += 8
        
        # 2. Images (30 pts)
        if product.images and len(product.images) > 0:
            score += 15 # Has at least one image
            if len(product.images) >= 3: score += 15 # Good gallery
            elif len(product.images) == 2: score += 10
        
        # 3. Variants/Options (20 pts)
        has_options = False
        if product.specs and isinstance(product.specs, dict):
            options = product.specs.get('options')
            if options and len(options) > 0:
                has_options = True
                score += 20
        
        # 4. Metadata (20 pts)
        if product.category_id: score += 5
        if product.price > 0: score += 5
        if product.stock > 0: score += 5
        if product.specs and len(product.specs) > 2: score += 5

        return min(100, score)
    
    def perform_soft_delete(self, product: 'Product', reason: str, deleted_by: Optional[int] = None):
        """
        Soft-deletes a product and archives a snapshot for 30-day recovery.
        """
        try:
            # Prepare recovery snapshot
            snapshot = {
                "name_en": product.name_en,
                "name_ar": product.name_ar,
                "price": product.price,
                "stock": product.stock,
                "specs": product.specs,
                "images": [img.image_url for img in product.images],
                "soft_deleted_at": datetime.now(timezone.utc).isoformat()
            }
            
            product.deleted_at = datetime.now(timezone.utc)
            product.is_active = False
            product.deletion_reason = reason
            product.deleted_by = deleted_by
            
            # Log audit trail
            audit = ProductAudit(
                product_id=product.id,
                action="IMAGE_FAILURE_AUTO_DELETE",
                reason=reason,
                snapshot=snapshot,
                performed_by=deleted_by
            )
            self.db.add(audit)
            
            self.report.deleted_products.append({
                "id": product.id,
                "reason": reason
            })
            self.report.total_invalid += 1
            logger.warning(f"🚨 AUTO-DELETED product {product.id} due to {reason}")
            return True
        except Exception as e:
            logger.error(f"Failed to soft-delete {product.id}: {e}")
            return False
    
    def process_product(self, product: 'Product') -> bool:
        """Process quality lifecycle for a single product."""
        self.report.total_checked += 1
        
        # 1. Critical Image Integrity Check (Phase 3 Requirement)
        if not product.images:
            return self.perform_soft_delete(product, "Missing all images")
            
        for img in product.images:
            is_valid, error = self.validate_image_url(img.image_url)
            if not is_valid:
                return self.perform_soft_delete(product, f"Image 404/Error: {error}")
        
        # 2. Score Calculation
        old_score = product.quality_score
        new_score = self.calculate_quality_score(product)
        
        if old_score != new_score:
            product.quality_score = new_score
            self.report.updated_scores.append({
                "id": product.id,
                "old": old_score,
                "new": new_score
            })
            
        self.report.total_valid += 1
        return True
    
    def run_quality_check(self, batch_size: int = 50) -> QualityReport:
        """Run full quality check on all active products."""
        if not self.db:
            logger.error("No database session available")
            return self.report
            
        logger.info("=" * 60)
        logger.info("Starting Product Quality Check")
        logger.info("=" * 60)
        
        try:
            products = self.db.query(Product).filter(
                Product.is_active == True,
                Product.deleted_at == None
            ).all()
            
            logger.info(f"Found {len(products)} active products to check")
            
            for idx, product in enumerate(products, 1):
                self.process_product(product)
                
                if idx % batch_size == 0:
                    self.db.commit()
                    logger.info(f"Processed {idx}/{len(products)} products...")
                    
            self.db.commit()
            
        except Exception as e:
            logger.error(f"Quality check failed: {str(e)}")
            self.db.rollback()
            self.report.errors.append(f"Quality check failed: {str(e)}")
            
        logger.info("=" * 60)
        logger.info(f"Quality Check Complete")
        logger.info(f"  Total Checked: {self.report.total_checked}")
        logger.info(f"  Valid Products: {self.report.total_valid}")
        logger.info(f"  Invalid (Deleted): {self.report.total_invalid}")
        logger.info(f"  Score Updates: {len(self.report.updated_scores)}")
        logger.info("=" * 60)
        
        return self.report


class StandaloneQualityChecker:
    """Run quality check on JSON file without database."""
    
    def __init__(self, json_path: str):
        self.json_path = json_path
        self.report = QualityReport()
        self.session = requests.Session()
        
    def validate_image_url(self, url: str, timeout: int = 10) -> Tuple[bool, Optional[str]]:
        """Validate single image URL."""
        if not url:
            return False, "Empty URL"
            
        try:
            response = self.session.head(url, timeout=timeout, allow_redirects=True, verify=False)
            
            if response.status_code >= 400:
                response = self.session.get(url, stream=True, timeout=timeout, verify=False)
                response.close()
                
            if response.status_code >= 400:
                return False, f"HTTP {response.status_code}"
                
            content_type = response.headers.get('Content-Type', '')
            if not content_type.startswith('image/'):
                return False, f"Invalid content type: {content_type}"
                
            return True, None
            
        except Exception as e:
            return False, str(e)[:50]
    
    def check_json_products(self) -> Dict[str, Any]:
        """Check products from JSON file."""
        logger.info(f"Loading products from {self.json_path}")
        
        with open(self.json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
            
        products = data.get('products', [])
        logger.info(f"Found {len(products)} products to validate")
        
        results = {
            "valid_products": [],
            "invalid_products": [],
            "summary": {
                "total": len(products),
                "valid": 0,
                "invalid": 0
            }
        }
        
        for idx, product in enumerate(products, 1):
            product_id = product.get('id', f'unknown_{idx}')
            name_en = product.get('name_en', 'Unknown')
            images = product.get('images', [])
            
            invalid_images = []
            valid_images = []
            
            for img_url in images:
                is_valid, error = self.validate_image_url(img_url)
                if is_valid:
                    valid_images.append(img_url)
                else:
                    invalid_images.append({"url": img_url, "error": error})
            
            product_result = {
                "id": product_id,
                "name_en": name_en,
                "images": {
                    "total": len(images),
                    "valid": len(valid_images),
                    "invalid": len(invalid_images),
                    "invalid_details": invalid_images[:2]
                }
            }
            
            if invalid_images:
                results["invalid_products"].append(product_result)
                results["summary"]["invalid"] += 1
                logger.warning(f"Product {product_id}: {len(invalid_images)} invalid images")
            else:
                results["valid_products"].append(product_result)
                results["summary"]["valid"] += 1
                
            if idx % 50 == 0:
                logger.info(f"Processed {idx}/{len(products)} products...")
        
        logger.info(f"Validation complete: {results['summary']['valid']} valid, {results['summary']['invalid']} invalid")
        
        try:
            output_path = self.json_path.replace('.json', '_validation.json')
            with open(output_path, 'w', encoding='utf-8') as f:
                json.dump(results, f, ensure_ascii=False, indent=2)
            logger.info(f"Results saved to {output_path}")
        except Exception as e:
            logger.warning(f"Could not save validation results to disk: {e}")
        
        return results


def create_backup(db_session) -> Optional[str]:
    """Create a backup of current products before quality operations."""
    try:
        products = db_session.query(Product).all()
        
        backup_data = {
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "total_products": len(products),
            "products": []
        }
        
        for p in products:
            product_dict = {
                "id": p.id,
                "name_en": p.name_en,
                "name_ar": p.name_ar,
                "price": p.price,
                "stock": p.stock,
                "is_active": p.is_active,
                "deleted_at": p.deleted_at.isoformat() if p.deleted_at else None,
                "quality_score": p.quality_score,
                "images": [{"id": img.id, "image_url": img.image_url} for img in p.images]
            }
            backup_data["products"].append(product_dict)
        
        backup_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "backups")
        os.makedirs(backup_dir, exist_ok=True)
        
        backup_filename = f"products_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        backup_path = os.path.join(backup_dir, backup_filename)
        
        try:
            with open(backup_path, 'w', encoding='utf-8') as f:
                json.dump(backup_data, f, ensure_ascii=False, indent=2)
            logger.info(f"Backup created: {backup_path}")
        except Exception as e:
            logger.warning(f"Could not write backup to disk: {e}")
        return backup_path
        
    except Exception as e:
        logger.error(f"Backup creation failed: {str(e)}")
        return None


def main():
    """Main entry point."""
    import argparse
    
    parser = argparse.ArgumentParser(description='VortexCommerce Product Quality Checker')
    parser.add_argument('--mode', choices=['db', 'json'], default='db',
                        help='Run against database or JSON file')
    parser.add_argument('--json-path', 
                        default=os.path.join(os.path.dirname(os.path.dirname(__file__)), 
                                            'backend', 'real_products_200.json'),
                        help='Path to JSON file for standalone mode')
    parser.add_argument('--backup', action='store_true',
                        help='Create backup before quality operations')
    parser.add_argument('--batch-size', type=int, default=50,
                        help='Batch size for database commits')
    
    args = parser.parse_args()
    
    logger.info("=" * 60)
    logger.info("VortexCommerce Product Quality Checker v2.0")
    logger.info("=" * 60)
    
    if args.mode == 'json':
        logger.info(f"Running in standalone JSON mode: {args.json_path}")
        checker = StandaloneQualityChecker(args.json_path)
        results = checker.check_json_products()
        logger.info(f"Summary: {results['summary']}")
        return 0
    
    if not DB_AVAILABLE:
        logger.error("Database modules not available. Use --mode json for standalone mode.")
        return 1
    
    db = SessionLocal()
    
    try:
        if args.backup:
            backup_path = create_backup(db)
            if not backup_path:
                logger.warning("Continuing without backup...")
        
        checker = ProductQualityChecker(db)
        report = checker.run_quality_check(args.batch_size)
        
        try:
            report_path = os.path.join(os.path.dirname(__file__), f"quality_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
            with open(report_path, 'w', encoding='utf-8') as f:
                json.dump(report.to_dict(), f, ensure_ascii=False, indent=2)
            logger.info(f"Quality report saved: {report_path}")
        except Exception as e:
            logger.warning(f"Could not save quality report to disk: {e}")
        
    finally:
        db.close()
    
    return 0


if __name__ == "__main__":
    sys.exit(main())