-- =============================================================================
-- Server Avatars
-- =============================================================================
-- Pre-configured avatar configurations available globally across all realms.
-- These reference server.props directly (not inventory items).
--
-- Loaded after 020_auth.sql (server.props must exist)
-- =============================================================================

CREATE TABLE server.avatars (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

    slug public.slug NOT NULL,
    name public.nonempty_text NOT NULL,
    description TEXT,

    is_public BOOLEAN NOT NULL DEFAULT false,
    is_active BOOLEAN NOT NULL DEFAULT true,
    thumbnail_path public.asset_path,

    -- Content layers: 3 layers x 9 positions = 27 slots
    -- All reference server.props(id) directly
    l_skin_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_skin_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    l_clothes_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_clothes_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    l_accessories_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    l_accessories_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    -- Emotion layers: 12 emotions x 9 positions = 108 slots
    e_neutral_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_neutral_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_happy_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_happy_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_sad_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sad_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_angry_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_angry_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_surprised_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_surprised_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_thinking_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_thinking_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_laughing_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_laughing_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_crying_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_crying_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_love_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_love_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_confused_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_confused_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_sleeping_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_sleeping_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    e_wink_0 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_1 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_2 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_3 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_4 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_5 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_6 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_7 UUID REFERENCES server.props(id) ON DELETE SET NULL,
    e_wink_8 UUID REFERENCES server.props(id) ON DELETE SET NULL,

    created_by UUID REFERENCES auth.users(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),

    CONSTRAINT uq_server_avatars_slug UNIQUE (slug)
);

COMMENT ON TABLE server.avatars IS 'Pre-configured avatar configurations available globally (references server.props)';

CREATE INDEX idx_server_avatars_active ON server.avatars (is_active) WHERE is_active = true;
CREATE INDEX idx_server_avatars_public ON server.avatars (is_public) WHERE is_public = true;

COMMENT ON COLUMN server.avatars.is_public IS
    'When true, avatar appears in the public avatar selection. Uses filtered index.';

-- =============================================================================
-- Add Default Avatar Columns to server.config
-- =============================================================================
-- These columns reference server.avatars for gender/age-based default avatars.
-- =============================================================================

ALTER TABLE server.config
    ADD COLUMN default_avatar_neutral_child UUID REFERENCES server.avatars(id) ON DELETE SET NULL,
    ADD COLUMN default_avatar_neutral_adult UUID REFERENCES server.avatars(id) ON DELETE SET NULL,
    ADD COLUMN default_avatar_male_child UUID REFERENCES server.avatars(id) ON DELETE SET NULL,
    ADD COLUMN default_avatar_male_adult UUID REFERENCES server.avatars(id) ON DELETE SET NULL,
    ADD COLUMN default_avatar_female_child UUID REFERENCES server.avatars(id) ON DELETE SET NULL,
    ADD COLUMN default_avatar_female_adult UUID REFERENCES server.avatars(id) ON DELETE SET NULL;

COMMENT ON COLUMN server.config.default_avatar_neutral_child IS
    'Default server avatar for gender-neutral child users';
COMMENT ON COLUMN server.config.default_avatar_neutral_adult IS
    'Default server avatar for gender-neutral adult users';
COMMENT ON COLUMN server.config.default_avatar_male_child IS
    'Default server avatar for male child users';
COMMENT ON COLUMN server.config.default_avatar_male_adult IS
    'Default server avatar for male adult users';
COMMENT ON COLUMN server.config.default_avatar_female_child IS
    'Default server avatar for female child users';
COMMENT ON COLUMN server.config.default_avatar_female_adult IS
    'Default server avatar for female adult users';

-- =============================================================================
-- Add FK for auth.active_avatars.selected_server_avatar_id
-- =============================================================================

ALTER TABLE auth.active_avatars
    ADD CONSTRAINT fk_auth_active_avatars_selected_server_avatar
    FOREIGN KEY (selected_server_avatar_id) REFERENCES server.avatars(id) ON DELETE SET NULL;
