CREATE TABLE albums (
    album_id         UUID PRIMARY KEY,
    title            TEXT NOT NULL,
    description      TEXT NOT NULL DEFAULT '',
    creator_user_id  UUID NOT NULL REFERENCES users(id),
    cover_asset_id   UUID,
    start_date       TIMESTAMPTZ,
    end_date         TIMESTAMPTZ,
    created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE album_entries (
    album_id         UUID NOT NULL REFERENCES albums(album_id) ON DELETE CASCADE,
    asset_id         UUID NOT NULL REFERENCES assets(asset_id) ON DELETE CASCADE,
    sort_order       INTEGER NOT NULL DEFAULT 0,
    added_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    added_by_user_id UUID NOT NULL REFERENCES users(id),
    PRIMARY KEY (album_id, asset_id)
);

CREATE TABLE tags (
    tag_id     UUID PRIMARY KEY,
    name       TEXT NOT NULL,
    tag_source TEXT NOT NULL DEFAULT 'user_manual'
);

CREATE TABLE asset_tags (
    asset_id           UUID NOT NULL REFERENCES assets(asset_id) ON DELETE CASCADE,
    tag_id             UUID NOT NULL REFERENCES tags(tag_id) ON DELETE CASCADE,
    tagged_by_user_id  UUID,
    confidence         DOUBLE PRECISION NOT NULL DEFAULT 1.0,
    PRIMARY KEY (asset_id, tag_id)
);
