-- Track-level folksonomy tags for taste enrichment (milestone #160, #1490).
--
-- A global (not per-user) cache of style/mood tags fetched from
-- MusicBrainz (keyless) + Last.fm (when a key is configured), keyed by
-- track. Feeds the taste recompute's tag facet alongside raw ID3 genre,
-- lifting resolution from coarse "Rock" to "post-punk / shoegaze /
-- melancholic". The enricher caps to the top-K tags per track at write
-- time so a heavily-tagged track can't dominate the profile. weight is a
-- normalized folksonomy strength in [0,1]. Mirrors the coverart
-- enricher's versioned source-tracking pattern.
CREATE TABLE track_tags (
    track_id uuid             NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
    tag      text             NOT NULL,
    weight   double precision NOT NULL DEFAULT 1,
    PRIMARY KEY (track_id, tag)
);
CREATE INDEX track_tags_track_idx ON track_tags (track_id);

-- Enrichment bookkeeping (mirror artists.artist_art_source):
--   tag_source NULL                            → not yet processed (eligible)
--   tag_source 'lastfm' / 'musicbrainz' / 'mixed' → found, cached
--   tag_source 'none'                          → providers returned nothing
--   tag_sources_version                        → bump to re-process none/stale
ALTER TABLE tracks ADD COLUMN tag_source text;
ALTER TABLE tracks ADD COLUMN tag_sources_version integer NOT NULL DEFAULT 0;
CREATE INDEX tracks_tag_source_idx ON tracks (tag_source, tag_sources_version);
