#include <stdio.h>

// Program: emitlim.c
// Compilation: gcc emitlim.c -o emitlim -lm

int main() {
    // Bitmark’s emission model:
    // - Genesis epoch: 20
    // - 1st Quartering epoch: 15
    // - 1st Halving epoch: 10
    // - 2nd Quartering epoch: 7.5
    // - 2nd Halving epoch: 5
    // - etc...
    // The pattern from epoch 2 onward is to alternate:
    //    Even epochs -> Quartering  (multiply by 0.75)
    //    Odd  epochs -> Halving     (multiply by 2/3)

    // Additional info:
    //  - Each epoch lasts 1.5 years.
    //  - A block is produced every 2 minutes => 720 blocks/day => 394200 blocks per epoch.
    //  - If emission drops below 0.00000001, we stop.

    double min_emission_threshold = 0.00000001; // Minimum emission threshold
    int blocks_per_epoch = 394200;

    // Start with Genesis epoch:
    double current_emission = 20.0; // blocks per epoch at Genesis
    double cumulative_emission = 0.0;

    // Print column headers
    printf("Epoch #\tEpoch Name\t  Rate (per block)\t   Epoch Emission\t  Cumulative Emission\n");
    printf("---------------------------------------------------------------------------------------\n");

    // We'll track epochs starting at 1 (Genesis)
    // The approach:
    //  1) Print info for the current epoch.
    //  2) After printing, update the emission for the next epoch.
    //  3) If the next emission < threshold, break.

    int epoch = 1;

    while (current_emission >= min_emission_threshold)
    {
        // Calculate this epoch's emission
        double epoch_emission = current_emission * blocks_per_epoch;
        cumulative_emission += epoch_emission;

        // Identify epoch name
        if (epoch == 1) {
            // Genesis
            printf("%-7d\t%-11s\t%16.9f\t%16.9f\t\t%16.9f\n",
                   epoch, "Genesis", current_emission,
                   epoch_emission, cumulative_emission);
        } else {
            // Even epoch => Quartering, Odd epoch => Halving
            // But note epoch=2 => Quartering1, epoch=3 => Halving1, etc.
            // We'll define an integer 'sequenceIndex' = (epoch - 2) + 1,
            // so epoch=2 => sequenceIndex=1, epoch=3 => sequenceIndex=2, etc.

            int sequenceIndex = (epoch - 2) + 1; // the index after Genesis
            const char* epochType = (epoch % 2 == 0) ? "Quartering" : "Halving";
            printf("%-7d\t%-10s %2d\t%16.9f\t%16.9f\t\t%16.9f\n",
                   epoch, epochType, (sequenceIndex + 1) / 2, // integer division rounding
                   current_emission, epoch_emission, cumulative_emission);
        }

        // Prepare for the next epoch
        epoch++;
        double next_emission = current_emission;

        if (epoch == 2) {
            // 1st Quartering => 15
            next_emission = 15.0;
        } else if (epoch == 3) {
            // 1st Halving => 10
            next_emission = 10.0;
        } else {
            // From epoch=4 onward, alternate Quartering (0.75) & Halving (2/3)
            if (epoch % 2 == 0) {
                // Quartering
                next_emission *= 0.75;
            } else {
                // Halving
                next_emission *= (2.0 / 3.0);
            }
        }

        if (next_emission < min_emission_threshold) {
            break;
        }

        current_emission = next_emission;
    }

    // Print final summary
    printf("Final Total Cumulative Emission after %d epochs: %.9f\n", epoch - 1, cumulative_emission);

    return 0;
}

