-- Bestehende benutzerspezifische Mining-Daten werden diesem Keycloak-Sub zugeordnet:
-- adea1766-5d1c-4c2e-98bd-5239861f745f
-- Die Keycloak-Sub ist stabiler als preferred_username und wird fuer alle benutzerspezifischen Mining-Daten genutzt.

BEGIN;

ALTER TABLE miningcheck_settings
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_cost_plans
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_measurements
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_measurement_rates
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_payouts
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_targets
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_dashboard_definitions
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

ALTER TABLE miningcheck_purchased_miners
ADD COLUMN IF NOT EXISTS owner_sub VARCHAR(128);

UPDATE miningcheck_settings
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_cost_plans
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_measurements
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_measurement_rates mr
SET owner_sub = m.owner_sub
FROM miningcheck_measurements m
WHERE mr.measurement_id = m.id
  AND (mr.owner_sub IS NULL OR BTRIM(mr.owner_sub) = '');

UPDATE miningcheck_measurement_rates
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_payouts
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_targets
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_dashboard_definitions
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

UPDATE miningcheck_purchased_miners
SET owner_sub = 'adea1766-5d1c-4c2e-98bd-5239861f745f'
WHERE owner_sub IS NULL OR BTRIM(owner_sub) = '';

ALTER TABLE miningcheck_settings
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_cost_plans
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_measurements
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_measurement_rates
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_payouts
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_targets
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_dashboard_definitions
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_purchased_miners
ALTER COLUMN owner_sub SET NOT NULL;

ALTER TABLE miningcheck_settings
DROP CONSTRAINT IF EXISTS miningcheck_settings_project_key_key;

ALTER TABLE miningcheck_measurements
DROP CONSTRAINT IF EXISTS uq_mining_measurements_unique;

ALTER TABLE miningcheck_targets
DROP CONSTRAINT IF EXISTS uq_mining_targets_project_label;

ALTER TABLE miningcheck_dashboard_definitions
DROP CONSTRAINT IF EXISTS uq_mining_dashboards_project_name;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.table_constraints
        WHERE table_schema = current_schema()
          AND table_name = 'miningcheck_settings'
          AND constraint_name = 'uq_mining_settings_project_owner'
    ) THEN
        ALTER TABLE miningcheck_settings
        ADD CONSTRAINT uq_mining_settings_project_owner UNIQUE (project_key, owner_sub);
    END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.table_constraints
        WHERE table_schema = current_schema()
          AND table_name = 'miningcheck_measurements'
          AND constraint_name = 'uq_mining_measurements_unique'
    ) THEN
        ALTER TABLE miningcheck_measurements
        ADD CONSTRAINT uq_mining_measurements_unique UNIQUE (project_key, owner_sub, measured_at, coins_total);
    END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.table_constraints
        WHERE table_schema = current_schema()
          AND table_name = 'miningcheck_targets'
          AND constraint_name = 'uq_mining_targets_project_label'
    ) THEN
        ALTER TABLE miningcheck_targets
        ADD CONSTRAINT uq_mining_targets_project_label UNIQUE (project_key, owner_sub, label);
    END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.table_constraints
        WHERE table_schema = current_schema()
          AND table_name = 'miningcheck_dashboard_definitions'
          AND constraint_name = 'uq_mining_dashboards_project_name'
    ) THEN
        ALTER TABLE miningcheck_dashboard_definitions
        ADD CONSTRAINT uq_mining_dashboards_project_name UNIQUE (project_key, owner_sub, name);
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_miningcheck_cost_plans_project_owner_start
    ON miningcheck_cost_plans(project_key, owner_sub, starts_at);

CREATE INDEX IF NOT EXISTS idx_miningcheck_measurements_project_owner_measured_at
    ON miningcheck_measurements(project_key, owner_sub, measured_at);

CREATE INDEX IF NOT EXISTS idx_miningcheck_measurement_rates_project_owner_measurement
    ON miningcheck_measurement_rates(project_key, owner_sub, measurement_id);

CREATE INDEX IF NOT EXISTS idx_miningcheck_payouts_project_owner_payout_at
    ON miningcheck_payouts(project_key, owner_sub, payout_at);

CREATE INDEX IF NOT EXISTS idx_miningcheck_targets_project_owner
    ON miningcheck_targets(project_key, owner_sub, sort_order, id);

CREATE INDEX IF NOT EXISTS idx_miningcheck_dashboards_project_owner
    ON miningcheck_dashboard_definitions(project_key, owner_sub, id);

CREATE INDEX IF NOT EXISTS idx_miningcheck_purchased_miners_project_owner_purchased_at
    ON miningcheck_purchased_miners(project_key, owner_sub, purchased_at);

COMMIT;
