-- v0.12.2: Distribution vers Plateformes (F501-F510)
-- Track distribution to Spotify, Apple Music, Deezer via distributor partner

-- Track distribution submissions
CREATE TABLE IF NOT EXISTS track_distributions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    track_id UUID NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
    creator_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,

    -- Distributor info
    distributor VARCHAR(50) NOT NULL DEFAULT 'distrokid',
    submission_id VARCHAR(255) NOT NULL UNIQUE,
    submission_upc VARCHAR(50),
    submission_isrc VARCHAR(50),

    -- Metadata snapshot at submission time
    metadata JSONB NOT NULL DEFAULT '{}',
    cover_file_path VARCHAR(512),

    -- Overall status
    overall_status VARCHAR(50) NOT NULL DEFAULT 'submitted',

    -- Platform-specific status (JSON)
    -- e.g. {"spotify": {"status":"live","url":"...","live_date":"...","error":null}}
    platform_statuses JSONB NOT NULL DEFAULT '{}',

    -- Timestamps
    submitted_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    first_live_at TIMESTAMPTZ,
    last_status_check_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_track_distributions_track ON track_distributions(track_id);
CREATE INDEX IF NOT EXISTS idx_track_distributions_creator ON track_distributions(creator_id);
CREATE INDEX IF NOT EXISTS idx_track_distributions_status ON track_distributions(overall_status);
CREATE INDEX IF NOT EXISTS idx_track_distributions_submitted ON track_distributions(submitted_at DESC);

-- Distribution status change history (audit trail)
CREATE TABLE IF NOT EXISTS track_distribution_status_history (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    distribution_id UUID NOT NULL REFERENCES track_distributions(id) ON DELETE CASCADE,
    platform VARCHAR(50) NOT NULL,
    old_status VARCHAR(50),
    new_status VARCHAR(50) NOT NULL,
    note TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_dist_status_history_dist ON track_distribution_status_history(distribution_id);

-- External streaming royalties (monthly imports from distributors)
CREATE TABLE IF NOT EXISTS external_streaming_royalties (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    track_id UUID NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
    creator_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,

    -- Reporting period
    reporting_period_start DATE NOT NULL,
    reporting_period_end DATE NOT NULL,

    -- Platform
    platform VARCHAR(50) NOT NULL,

    -- Metrics
    total_streams BIGINT NOT NULL DEFAULT 0,
    total_revenue_cents BIGINT NOT NULL DEFAULT 0,
    currency VARCHAR(3) NOT NULL DEFAULT 'USD',

    -- Breakdown (optional JSON)
    streams_breakdown JSONB,

    -- Distributor info
    distributor VARCHAR(50) NOT NULL DEFAULT 'distrokid',
    distributor_report_id VARCHAR(255),

    -- Import status
    import_status VARCHAR(50) NOT NULL DEFAULT 'pending',
    import_error TEXT,
    imported_at TIMESTAMPTZ,

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT chk_streams_non_negative CHECK (total_streams >= 0),
    CONSTRAINT chk_revenue_non_negative CHECK (total_revenue_cents >= 0),
    CONSTRAINT uq_royalty_period_platform UNIQUE (track_id, platform, reporting_period_start)
);

CREATE INDEX IF NOT EXISTS idx_ext_royalties_creator_period ON external_streaming_royalties(creator_id, reporting_period_start DESC);
CREATE INDEX IF NOT EXISTS idx_ext_royalties_track ON external_streaming_royalties(track_id);
CREATE INDEX IF NOT EXISTS idx_ext_royalties_platform ON external_streaming_royalties(platform);
