-- Migration: Add attachment to inquiry_tests
-- This table might have been created by previous agents or manually

CREATE TABLE IF NOT EXISTS inquiry_tests (
    id INT AUTO_INCREMENT PRIMARY KEY,
    inquiry_id INT,
    test_id INT,
    result VARCHAR(255),
    normal_range VARCHAR(255),
    attachment VARCHAR(255),
    FOREIGN KEY (inquiry_id) REFERENCES laboratory_inquiries(id) ON DELETE CASCADE,
    FOREIGN KEY (test_id) REFERENCES laboratory_tests(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Ensure attachment column exists (in case inquiry_tests already existed without it)
SET @dbname = DATABASE();
SET @tablename = "inquiry_tests";
SET @columnname = "attachment";
SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
   WHERE TABLE_SCHEMA = @dbname
     AND TABLE_NAME = @tablename
     AND COLUMN_NAME = @columnname
  ) > 0,
  "SELECT 1",
  "ALTER TABLE inquiry_tests ADD COLUMN attachment VARCHAR(255) AFTER normal_range"
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
