<?php
/**
 * One-time migration: Change jobs.repair to LONGTEXT (effectively unlimited)
 * so receipts with many items are never truncated.
 *
 * IMPORTANT:
 * - Editing database.sql does NOT change your existing database. It only affects
 *   fresh installs (when someone imports that file).
 * - To update your LIVE database, do ONE of these:
 *   1. Open this file in the browser: https://yoursite.com/migrate_repair_to_text.php
 *   2. Or run this in phpMyAdmin (SQL tab):
 *      ALTER TABLE jobs MODIFY COLUMN repair LONGTEXT DEFAULT NULL;
 */
require_once __DIR__ . '/db.php';

header('Content-Type: text/html; charset=utf-8');

$pdo = $pdo ?? null;
if (!$pdo) {
    echo "<p>Database connection not available.</p>";
    exit;
}

try {
    $stmt = $pdo->query("SHOW COLUMNS FROM jobs WHERE Field = 'repair'");
    $col = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!$col) {
        echo "<p>Column jobs.repair not found.</p>";
        exit;
    }
    $typeBefore = $col['Type'];

    // Always run ALTER so we're sure the column is LONGTEXT (idempotent; ~4GB max = effectively unlimited)
    $pdo->exec("ALTER TABLE jobs MODIFY COLUMN repair LONGTEXT DEFAULT NULL");
    $stmt = $pdo->query("SHOW COLUMNS FROM jobs WHERE Field = 'repair'");
    $typeAfter = $stmt->fetch(PDO::FETCH_ASSOC)['Type'];

    echo "<p><strong>Done.</strong> Column jobs.repair is now: <code>" . htmlspecialchars($typeAfter) . "</code> (effectively unlimited).</p>";
    echo "<p><strong>New</strong> sales will store and show all items on the receipt.</p>";
    echo "<p><strong>Existing</strong> receipts (e.g. IV-SG00125, IV-SG00126) that were saved when the column was VARCHAR(255) still have truncated data in the database—changing the column does not restore it. To fix those, use <a href=\"fix_receipt_repair.php\">fix_receipt_repair.php</a> and re-enter or paste the full item list.</p>";
} catch (PDOException $e) {
    echo "<p>Error: " . htmlspecialchars($e->getMessage()) . "</p>";
}
