<?php

use App\Database\Migration;

/**
 * Migration 022 – Video support for iw_media
 *
 * Adds a video_poster column to iw_media so that video files can store
 * a path to their poster/thumbnail image (relative to public/uploads/).
 *
 * Safe re-run: checks information_schema before ALTER TABLE.
 */
class AddVideoPosterToMedia extends Migration
{
    public function up(): void
    {
        $hasColumn = (int) $this->db->fetchColumn(
            "SELECT COUNT(*)
             FROM   information_schema.COLUMNS
             WHERE  TABLE_SCHEMA = DATABASE()
               AND  TABLE_NAME   = 'iw_media'
               AND  COLUMN_NAME  = 'video_poster'"
        );

        if ($hasColumn === 0) {
            $this->db->query("
                ALTER TABLE iw_media
                    ADD COLUMN video_poster VARCHAR(500) DEFAULT NULL
                        COMMENT 'Relative path to poster/thumbnail for video files'
                        AFTER description
            ");
        }
    }

    public function down(): void
    {
        $this->db->query("
            ALTER TABLE iw_media
                DROP COLUMN IF EXISTS video_poster
        ");
    }
}
