-- M8 团队协作与资产库

ALTER TABLE tenant_members
    ADD COLUMN position VARCHAR(128) NULL AFTER role;

CREATE TABLE team_groups (
    id          BIGINT      NOT NULL PRIMARY KEY,
    tenant_id   BIGINT      NOT NULL,
    name        VARCHAR(128) NOT NULL,
    description TEXT,
    created_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    INDEX idx_tenant (tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE group_members (
    group_id    BIGINT NOT NULL,
    user_id     BIGINT NOT NULL,
    PRIMARY KEY (group_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE global_assets (
    id              BIGINT          NOT NULL PRIMARY KEY,
    tenant_id       BIGINT          NOT NULL,
    creator_user_id BIGINT          NOT NULL,
    name            VARCHAR(256)    NOT NULL,
    asset_type      VARCHAR(32)     NOT NULL COMMENT 'character/scene/prop',
    library_type    VARCHAR(16)     NOT NULL DEFAULT 'personal' COMMENT 'personal/team',
    tags            VARCHAR(512),
    image_tos_key   VARCHAR(512),
    image_url       VARCHAR(1024),
    description     TEXT,
    is_favorite     TINYINT         NOT NULL DEFAULT 0,
    source_project_id BIGINT,
    created_at      DATETIME(3)     NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at      DATETIME(3)     NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    INDEX idx_tenant_type (tenant_id, asset_type, library_type),
    INDEX idx_creator (creator_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
