CREATE TABLE IF NOT EXISTS media_storages (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    driver ENUM('local','ftp','s3','webdav') NOT NULL DEFAULT 'local',
    config JSON DEFAULT NULL,
    media_url VARCHAR(500) NOT NULL DEFAULT '',
    is_default TINYINT(1) NOT NULL DEFAULT 0,
    file_count INT UNSIGNED NOT NULL DEFAULT 0,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_default (is_default)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE videos ADD COLUMN storage_id INT UNSIGNED DEFAULT NULL AFTER video_folder;
ALTER TABLE videos ADD INDEX idx_storage (storage_id);

INSERT INTO media_storages (name, driver, config, media_url, is_default, sort_order)
SELECT 'Local Server', 'local', '{}', '', 1, 0
FROM dual WHERE NOT EXISTS (SELECT 1 FROM media_storages LIMIT 1);
