CREATE TABLE IF NOT EXISTS media_files (
    id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    uuid          CHAR(36)        NOT NULL,
    uploader_id   BIGINT UNSIGNED NOT NULL,
    type          ENUM('image','video','audio','document','voice_note','video_note') NOT NULL,
    original_name VARCHAR(255)    NOT NULL,
    stored_name   VARCHAR(64)     NOT NULL,
    extension     VARCHAR(10)     NOT NULL,
    mime_type     VARCHAR(100)    NOT NULL,
    file_size     BIGINT UNSIGNED NOT NULL,
    width         SMALLINT UNSIGNED DEFAULT NULL,
    height        SMALLINT UNSIGNED DEFAULT NULL,
    duration_ms   INT UNSIGNED    DEFAULT NULL,
    thumbnail_name VARCHAR(64)    DEFAULT NULL,
    waveform_data  JSON           DEFAULT NULL,
    cdn_url        VARCHAR(500)   DEFAULT NULL,
    is_processed   TINYINT(1)    DEFAULT 0,
    created_at    DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_uuid (uuid),
    UNIQUE KEY uq_stored (stored_name),
    FOREIGN KEY (uploader_id) REFERENCES users(id) ON DELETE RESTRICT,
    INDEX idx_type (type),
    INDEX idx_uploader (uploader_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
