CREATE TABLE `play_media` (
  `id`           varchar(32)   NOT NULL,
  `tenant_id`    varchar(32)   NOT NULL,
  `owner_type`   varchar(32)   NOT NULL COMMENT 'clerk/article/...',
  `owner_id`     varchar(32)   NOT NULL,
  `kind`         varchar(16)   NOT NULL COMMENT 'image | video',
  `status`       varchar(16)   NOT NULL COMMENT 'uploaded|processing|ready|approved|rejected',
  `url`          varchar(1024) NOT NULL,
  `cover_url`    varchar(1024) DEFAULT NULL,
  `duration_ms`  bigint        DEFAULT NULL,
  `width`        int           DEFAULT NULL,
  `height`       int           DEFAULT NULL,
  `size_bytes`   bigint        DEFAULT NULL,
  `order_index`  int           NOT NULL DEFAULT 0,
  `metadata`     json          DEFAULT NULL,
  `created_time` datetime      DEFAULT CURRENT_TIMESTAMP,
  `updated_time` datetime      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_media_owner` (`owner_type`,`owner_id`),
  KEY `idx_media_order` (`tenant_id`,`owner_type`,`owner_id`,`order_index`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `play_clerk_media_asset` (
  `id`               varchar(32)   NOT NULL,
  `clerk_id`         varchar(32)   NOT NULL,
  `tenant_id`        varchar(32)   NOT NULL,
  `media_id`         varchar(32)   NOT NULL,
  `usage`            varchar(32)   NOT NULL,
  `review_state`     varchar(16)   NOT NULL,
  `order_index`      int           NOT NULL DEFAULT 0,
  `submitted_time`   datetime      DEFAULT NULL,
  `review_record_id` varchar(32)   DEFAULT NULL,
  `note`             varchar(255)  DEFAULT NULL,
  `deleted`          tinyint(1)    NOT NULL DEFAULT 0,
  `created_by`       varchar(32)   DEFAULT NULL,
  `created_time`     datetime      DEFAULT CURRENT_TIMESTAMP,
  `updated_time`     datetime      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_by`       varchar(32)   DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_clerk_usage_media` (`clerk_id`,`usage`,`media_id`),
  KEY `idx_clerk_usage_state` (`clerk_id`,`usage`,`review_state`,`deleted`),
  KEY `idx_clerk_media_asset_media` (`media_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
