-- 材质管理系统初始化脚本
-- 此脚本由GORM AutoMigrate自动执行，仅作参考

-- 材质表
CREATE TABLE IF NOT EXISTS materials (
    id          BIGSERIAL PRIMARY KEY,
    name        VARCHAR(255) NOT NULL,
    diffuse_r   FLOAT NOT NULL DEFAULT 0,
    diffuse_g   FLOAT NOT NULL DEFAULT 0,
    diffuse_b   FLOAT NOT NULL DEFAULT 0,
    alpha       FLOAT NOT NULL DEFAULT 1,
    shininess   FLOAT NOT NULL DEFAULT 0,
    specular_r  FLOAT NOT NULL DEFAULT 0,
    specular_g  FLOAT NOT NULL DEFAULT 0,
    specular_b  FLOAT NOT NULL DEFAULT 0,
    ambient_r   FLOAT NOT NULL DEFAULT 0,
    ambient_g   FLOAT NOT NULL DEFAULT 0,
    ambient_b   FLOAT NOT NULL DEFAULT 0,
    metallic    FLOAT NOT NULL DEFAULT 0,
    roughness   FLOAT NOT NULL DEFAULT 0.5,
    reflectance FLOAT NOT NULL DEFAULT 0.5,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 材质绑定表
CREATE TABLE IF NOT EXISTS material_bindings (
    id          BIGSERIAL PRIMARY KEY,
    material_id BIGINT NOT NULL REFERENCES materials(id) ON DELETE CASCADE,
    group_id    VARCHAR(255) NOT NULL,
    created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_bindings_material_id ON material_bindings(material_id);
CREATE INDEX IF NOT EXISTS idx_bindings_group_id ON material_bindings(group_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_material_group ON material_bindings(material_id, group_id);
