CREATE TABLE IF NOT EXISTS miningcheck_projects (
    project_key VARCHAR(64) NOT NULL PRIMARY KEY,
    project_name VARCHAR(160) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS miningcheck_currencies (
    code VARCHAR(10) NOT NULL PRIMARY KEY,
    name VARCHAR(64) NOT NULL,
    symbol VARCHAR(8) NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    is_crypto TINYINT(1) NOT NULL DEFAULT 0,
    sort_order INT NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS miningcheck_currency_aliases (
    alias_code VARCHAR(10) NOT NULL PRIMARY KEY,
    currency_code VARCHAR(10) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_currency_aliases_currency FOREIGN KEY (currency_code) REFERENCES miningcheck_currencies(code) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS miningcheck_settings (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    baseline_measured_at DATETIME NOT NULL,
    baseline_coins_total DECIMAL(20,6) NOT NULL,
    daily_cost_amount DECIMAL(20,10) NOT NULL,
    daily_cost_currency VARCHAR(10) NOT NULL,
    report_currency VARCHAR(10) NULL,
    crypto_currency VARCHAR(10) NULL,
    display_timezone VARCHAR(64) NULL,
    fx_max_age_hours DECIMAL(10,2) NULL,
    module_theme_mode VARCHAR(16) NULL,
    module_theme_accent VARCHAR(16) NULL,
    preferred_currencies JSON NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_settings_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_settings_daily_cost_currency_currency FOREIGN KEY (daily_cost_currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT fk_mining_settings_report_currency_currency FOREIGN KEY (report_currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT fk_mining_settings_crypto_currency_currency FOREIGN KEY (crypto_currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT uq_mining_settings_project UNIQUE (project_key)
);

CREATE TABLE IF NOT EXISTS miningcheck_cost_plans (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    label VARCHAR(120) NOT NULL,
    starts_at DATETIME NOT NULL,
    runtime_months INT NOT NULL,
    mining_speed_value DECIMAL(20,4) NULL,
    mining_speed_unit VARCHAR(8) NULL,
    bonus_speed_value DECIMAL(20,4) NULL,
    bonus_speed_unit VARCHAR(8) NULL,
    auto_renew TINYINT(1) NOT NULL DEFAULT 0,
    base_price_amount DECIMAL(20,8) NULL,
    payment_type VARCHAR(10) NOT NULL DEFAULT 'fiat',
    total_cost_amount DECIMAL(20,8) NOT NULL,
    currency VARCHAR(10) NOT NULL,
    note TEXT NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_cost_plans_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_cost_plans_currency_currency FOREIGN KEY (currency) REFERENCES miningcheck_currencies(code)
);

CREATE INDEX idx_miningcheck_cost_plans_project_start
    ON miningcheck_cost_plans(project_key, starts_at);

CREATE TABLE IF NOT EXISTS miningcheck_measurements (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    measured_at DATETIME NOT NULL,
    coins_total DECIMAL(20,6) NOT NULL,
    price_per_coin DECIMAL(20,8) NULL,
    price_currency VARCHAR(10) NULL,
    fx_fetch_id BIGINT UNSIGNED NULL,
    note TEXT NULL,
    source ENUM('manual', 'image_ocr', 'seed_import') NOT NULL,
    image_path VARCHAR(255) NULL,
    ocr_raw_text MEDIUMTEXT NULL,
    ocr_confidence DECIMAL(6,4) NULL,
    ocr_flags JSON NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_measurements_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_measurements_price_currency_currency FOREIGN KEY (price_currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT uq_mining_measurements_unique UNIQUE (project_key, measured_at, coins_total)
);

CREATE INDEX idx_miningcheck_measurements_project_measured_at
    ON miningcheck_measurements(project_key, measured_at);

CREATE INDEX idx_miningcheck_measurements_fx_fetch
    ON miningcheck_measurements(fx_fetch_id);

CREATE TABLE IF NOT EXISTS miningcheck_measurement_rates (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    measurement_id BIGINT UNSIGNED NOT NULL,
    project_key VARCHAR(64) NOT NULL,
    base_currency VARCHAR(10) NOT NULL,
    quote_currency VARCHAR(10) NOT NULL,
    rate DECIMAL(20,10) NOT NULL,
    provider VARCHAR(32) NOT NULL DEFAULT 'derived',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_measurement_rates_measurement FOREIGN KEY (measurement_id) REFERENCES miningcheck_measurements(id) ON DELETE CASCADE,
    CONSTRAINT fk_mining_measurement_rates_base_currency_currency FOREIGN KEY (base_currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT fk_mining_measurement_rates_quote_currency_currency FOREIGN KEY (quote_currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT uq_mining_measurement_rate_pair UNIQUE (measurement_id, base_currency, quote_currency),
    KEY idx_miningcheck_measurement_rates_project_measurement (project_key, measurement_id)
);

CREATE TABLE IF NOT EXISTS miningcheck_payouts (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    payout_at TIMESTAMP NOT NULL,
    coins_amount DECIMAL(20,6) NOT NULL,
    payout_currency VARCHAR(10) NOT NULL DEFAULT 'DOGE',
    note TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_payouts_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_payouts_payout_currency_currency FOREIGN KEY (payout_currency) REFERENCES miningcheck_currencies(code),
    KEY idx_miningcheck_payouts_project_payout_at (project_key, payout_at)
);

CREATE TABLE IF NOT EXISTS miningcheck_miner_offers (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    label VARCHAR(120) NOT NULL,
    runtime_months INT NULL,
    mining_speed_value DECIMAL(20,4) NULL,
    mining_speed_unit VARCHAR(8) NULL,
    bonus_speed_value DECIMAL(20,4) NULL,
    bonus_speed_unit VARCHAR(8) NULL,
    base_price_amount DECIMAL(20,8) NOT NULL,
    base_price_currency VARCHAR(10) NOT NULL,
    payment_type VARCHAR(10) NOT NULL DEFAULT 'fiat',
    auto_renew TINYINT(1) NOT NULL DEFAULT 0,
    note TEXT,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_miner_offers_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_miner_offers_base_price_currency_currency FOREIGN KEY (base_price_currency) REFERENCES miningcheck_currencies(code)
);

CREATE TABLE IF NOT EXISTS miningcheck_targets (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    label VARCHAR(120) NOT NULL,
    target_amount_fiat DECIMAL(20,2) NOT NULL,
    currency VARCHAR(10) NOT NULL,
    miner_offer_id BIGINT UNSIGNED NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    sort_order INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_targets_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_targets_currency_currency FOREIGN KEY (currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT fk_mining_targets_offer FOREIGN KEY (miner_offer_id) REFERENCES miningcheck_miner_offers(id) ON DELETE SET NULL,
    CONSTRAINT uq_mining_targets_project_label UNIQUE (project_key, label)
);

CREATE TABLE IF NOT EXISTS miningcheck_dashboard_definitions (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    name VARCHAR(160) NOT NULL,
    chart_type ENUM('line', 'bar', 'area', 'table') NOT NULL,
    x_field VARCHAR(64) NOT NULL,
    y_field VARCHAR(64) NOT NULL,
    aggregation VARCHAR(32) NOT NULL DEFAULT 'none',
    filters_json JSON NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_dashboards_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT uq_mining_dashboards_project_name UNIQUE (project_key, name)
);

CREATE TABLE IF NOT EXISTS miningcheck_purchased_miners (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    project_key VARCHAR(64) NOT NULL,
    miner_offer_id BIGINT UNSIGNED NULL,
    purchased_at TIMESTAMP NOT NULL,
    label VARCHAR(120) NOT NULL,
    runtime_months INT NULL,
    mining_speed_value DECIMAL(20,4) NULL,
    mining_speed_unit VARCHAR(8) NULL,
    bonus_speed_value DECIMAL(20,4) NULL,
    bonus_speed_unit VARCHAR(8) NULL,
    total_cost_amount DECIMAL(20,8) NOT NULL,
    currency VARCHAR(10) NOT NULL,
    usd_reference_amount DECIMAL(20,8) NULL,
    reference_price_amount DECIMAL(20,8) NULL,
    reference_price_currency VARCHAR(10) NULL,
    auto_renew TINYINT(1) NOT NULL DEFAULT 0,
    note TEXT,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_purchased_miners_project FOREIGN KEY (project_key) REFERENCES miningcheck_projects(project_key) ON DELETE CASCADE,
    CONSTRAINT fk_mining_purchased_miners_offer FOREIGN KEY (miner_offer_id) REFERENCES miningcheck_miner_offers(id) ON DELETE SET NULL,
    CONSTRAINT fk_mining_purchased_miners_currency_currency FOREIGN KEY (currency) REFERENCES miningcheck_currencies(code),
    CONSTRAINT fk_mining_purchased_miners_reference_price_currency_currency FOREIGN KEY (reference_price_currency) REFERENCES miningcheck_currencies(code)
);

CREATE TABLE IF NOT EXISTS miningcheck_fx_fetches (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    provider VARCHAR(32) NOT NULL DEFAULT 'currencyapi',
    base_currency VARCHAR(10) NOT NULL,
    rate_date DATE NOT NULL,
    fetched_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_mining_fx_fetches_base_currency_currency FOREIGN KEY (base_currency) REFERENCES miningcheck_currencies(code),
    KEY idx_miningcheck_fx_fetches_base_fetched (base_currency, fetched_at)
);

CREATE TABLE IF NOT EXISTS miningcheck_fx_rates (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    fetch_id BIGINT UNSIGNED NOT NULL,
    currency_code VARCHAR(10) NOT NULL,
    current_value DECIMAL(20,10) NOT NULL,
    KEY idx_miningcheck_fx_rates_fetch (fetch_id),
    KEY idx_miningcheck_fx_rates_currency (currency_code),
    CONSTRAINT fk_mining_fx_rates_fetch FOREIGN KEY (fetch_id) REFERENCES miningcheck_fx_fetches(id) ON DELETE CASCADE,
    CONSTRAINT fk_mining_fx_rates_currency_code_currency FOREIGN KEY (currency_code) REFERENCES miningcheck_currencies(code)
);
