<?php

require_once __DIR__ . '/../helpers/Env.php';
require_once __DIR__ . '/../config/app.php';
require_once __DIR__ . '/../helpers/FileUpload.php';

/**
 * Script to create thumbnails for all existing images
 * This should be run once after implementing the thumbnail functionality
 */

echo "Starting thumbnail migration...\n";

// Create thumbnail directory if it doesn't exist
if (!is_dir(UPLOAD_ITEMS_THUMBS_PATH)) {
    mkdir(UPLOAD_ITEMS_THUMBS_PATH, 0755, true);
    echo "Created thumbnail directory: " . UPLOAD_ITEMS_THUMBS_PATH . "\n";
}

// Get database connection
require_once __DIR__ . '/../config/database.php';
$db = Database::getInstance();

$processedImages = 0;
$failedImages = 0;

// Process items (banknotes)
echo "Processing items (banknotes)...\n";
$stmt = $db->query("SELECT image_obverse, image_reverse FROM items WHERE image_obverse IS NOT NULL OR image_reverse IS NOT NULL");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process obverse image
    if ($row['image_obverse']) {
        $sourcePath = UPLOAD_ITEMS_PATH . '/' . $row['image_obverse'];
        if (file_exists($sourcePath)) {
            $thumbPath = FileUpload::getThumbnailPath($sourcePath, UPLOAD_ITEMS_THUMBS_PATH);
            if (!file_exists($thumbPath)) {
                $result = FileUpload::createThumbnail($sourcePath, $thumbPath, 170, 80, 70);
                if ($result['success']) {
                    $processedImages++;
                    echo "Created thumbnail: " . basename($thumbPath) . "\n";
                } else {
                    $failedImages++;
                    echo "Failed to create thumbnail for " . $row['image_obverse'] . ": " . $result['error'] . "\n";
                }
            }
        }
    }
    
    // Process reverse image
    if ($row['image_reverse']) {
        $sourcePath = UPLOAD_ITEMS_PATH . '/' . $row['image_reverse'];
        if (file_exists($sourcePath)) {
            $thumbPath = FileUpload::getThumbnailPath($sourcePath, UPLOAD_ITEMS_THUMBS_PATH);
            if (!file_exists($thumbPath)) {
                $result = FileUpload::createThumbnail($sourcePath, $thumbPath, 170, 80, 70);
                if ($result['success']) {
                    $processedImages++;
                    echo "Created thumbnail: " . basename($thumbPath) . "\n";
                } else {
                    $failedImages++;
                    echo "Failed to create thumbnail for " . $row['image_reverse'] . ": " . $result['error'] . "\n";
                }
            }
        }
    }
}

// Process coins
echo "\nProcessing coins...\n";
$stmt = $db->query("SELECT image_obverse, image_reverse FROM coins WHERE image_obverse IS NOT NULL OR image_reverse IS NOT NULL");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process obverse image
    if ($row['image_obverse']) {
        $sourcePath = UPLOAD_ITEMS_PATH . '/' . $row['image_obverse'];
        if (file_exists($sourcePath)) {
            $thumbPath = FileUpload::getThumbnailPath($sourcePath, UPLOAD_ITEMS_THUMBS_PATH);
            if (!file_exists($thumbPath)) {
                $result = FileUpload::createThumbnail($sourcePath, $thumbPath, 160, 160, 70);
                if ($result['success']) {
                    $processedImages++;
                    echo "Created thumbnail: " . basename($thumbPath) . "\n";
                } else {
                    $failedImages++;
                    echo "Failed to create thumbnail for " . $row['image_obverse'] . ": " . $result['error'] . "\n";
                }
            }
        }
    }
    
    // Process reverse image
    if ($row['image_reverse']) {
        $sourcePath = UPLOAD_ITEMS_PATH . '/' . $row['image_reverse'];
        if (file_exists($sourcePath)) {
            $thumbPath = FileUpload::getThumbnailPath($sourcePath, UPLOAD_ITEMS_THUMBS_PATH);
            if (!file_exists($thumbPath)) {
                $result = FileUpload::createThumbnail($sourcePath, $thumbPath, 160, 160, 70);
                if ($result['success']) {
                    $processedImages++;
                    echo "Created thumbnail: " . basename($thumbPath) . "\n";
                } else {
                    $failedImages++;
                    echo "Failed to create thumbnail for " . $row['image_reverse'] . ": " . $result['error'] . "\n";
                }
            }
        }
    }
}

// Process additional item images
echo "\nProcessing additional item images...\n";
$stmt = $db->query("SELECT filename FROM item_images WHERE filename IS NOT NULL");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $sourcePath = UPLOAD_ITEMS_PATH . '/' . $row['filename'];
    if (file_exists($sourcePath)) {
        $thumbPath = FileUpload::getThumbnailPath($sourcePath, UPLOAD_ITEMS_THUMBS_PATH);
        if (!file_exists($thumbPath)) {
            $result = FileUpload::createThumbnail($sourcePath, $thumbPath, 170, 80, 70);
            if ($result['success']) {
                $processedImages++;
                echo "Created thumbnail: " . basename($thumbPath) . "\n";
            } else {
                $failedImages++;
                echo "Failed to create thumbnail for " . $row['filename'] . ": " . $result['error'] . "\n";
            }
        }
    }
}

// Process additional coin images
echo "\nProcessing additional coin images...\n";
$stmt = $db->query("SELECT filename FROM coin_images WHERE filename IS NOT NULL");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $sourcePath = UPLOAD_ITEMS_PATH . '/' . $row['filename'];
    if (file_exists($sourcePath)) {
        $thumbPath = FileUpload::getThumbnailPath($sourcePath, UPLOAD_ITEMS_THUMBS_PATH);
        if (!file_exists($thumbPath)) {
            $result = FileUpload::createThumbnail($sourcePath, $thumbPath, 160, 160, 70);
            if ($result['success']) {
                $processedImages++;
                echo "Created thumbnail: " . basename($thumbPath) . "\n";
            } else {
                $failedImages++;
                echo "Failed to create thumbnail for " . $row['filename'] . ": " . $result['error'] . "\n";
            }
        }
    }
}

echo "\nThumbnail migration completed!\n";
echo "Processed images: $processedImages\n";
echo "Failed images: $failedImages\n";

if ($failedImages > 0) {
    echo "\nSome images failed to process. Please check the error messages above.\n";
} else {
    echo "\nAll images processed successfully!\n";
}
