<?php
/**
 * Plugin Name: Established Year
 * Plugin URI:  https://bowdenworks.com/
 * Description: Calculates the years since a company's establishment. Use shortcodes [bw_year_number] for the number or [bw_year_word] for the English text.
 * Version:     1.1
 * Author:      Bowden Works
 * Author URI:  https://bowdenworks.com/
 * License:     GPL2
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/**
 * 1. Register the Settings Menu
 */
function bw_add_admin_menu() {
    add_options_page(
        'Established Year Settings', 
        'Established Year', 
        'manage_options', 
        'established-year', 
        'bw_options_page'
    );
}
add_action( 'admin_menu', 'bw_add_admin_menu' );

/**
 * 2. Initialize Settings
 */
function bw_settings_init() {
    register_setting( 'bwPlugin', 'bw_established_date' );

    add_settings_section(
        'bw_plugin_section', 
        __( 'Configuration', 'established-year' ), 
        'bw_settings_section_callback', 
        'established-year'
    );

    add_settings_field(
        'bw_established_date', 
        __( 'Date of Establishment', 'established-year' ), 
        'bw_date_render', 
        'established-year', 
        'bw_plugin_section'
    );
}
add_action( 'admin_init', 'bw_settings_init' );

/**
 * 3. Render Settings Field and Section
 */
function bw_date_render() {
    $value = get_option( 'bw_established_date' );
    // Ensure we render it correctly for the date input
    if ( ! $value ) {
        $value = '';
    }
    ?>
    <input type='date' name='bw_established_date' value='<?php echo esc_attr( $value ); ?>'>
    <p class="description">Enter the specific date the company started (DD-MM-YYYY).</p>
    <?php
}

function bw_settings_section_callback() {
    echo __( 'Set the exact date your company was founded below for precise calculation.', 'established-year' );
}

/**
 * 4. Render the Options Page HTML
 */
function bw_options_page() {
    ?>
    <div class="wrap">
        <h1>Established Year</h1>
        <form action='options.php' method='post'>
            <?php
            settings_fields( 'bwPlugin' );
            do_settings_sections( 'established-year' );
            submit_button();
            ?>
        </form>
        
        <hr>
        
        <h2>Documentation / How to Use</h2>
        <p>Once you have saved the date above, you can use the following shortcodes:</p>
        
        <table class="widefat fixed" cellspacing="0">
            <thead>
                <tr>
                    <th>Shortcode</th>
                    <th>Description</th>
                    <th>Example Output</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><code>[bw_year_number]</code></td>
                    <td>Outputs the precise number of years since the specific date.</td>
                    <td><strong>45</strong></td>
                </tr>
                <tr>
                    <td><code>[bw_year_word]</code></td>
                    <td>Outputs the number spelled out in English words.</td>
                    <td><strong>forty-five</strong></td>
                </tr>
            </tbody>
        </table>
        <p><em>Note: The calculation is precise. If your company started on Dec 31, 1980, and today is Dec 1, 2025, it will show 44 years (not 45 yet).</em></p>
    </div>
    <?php
}

/**
 * 5. Helper Function: Calculate Precise Years
 */
function bw_get_years_since() {
    $start_date_str = get_option( 'bw_established_date' );
    
    // Fallback if no date is set
    if ( empty( $start_date_str ) ) {
        return 0;
    }

    try {
        $start_date = new DateTime( $start_date_str );
        $current_date = new DateTime();
        
        // Calculate the difference
        $interval = $current_date->diff( $start_date );
        
        // Return only the years
        return $interval->y;
    } catch ( Exception $e ) {
        return 0;
    }
}

/**
 * 6. Helper Function: Convert Number to Words
 */
function bw_convert_number_to_words( $number ) {
    $hyphen      = '-';
    $conjunction = ' and ';
    $negative    = 'negative ';
    $dictionary  = array(
        0                   => 'zero',
        1                   => 'one',
        2                   => 'two',
        3                   => 'three',
        4                   => 'four',
        5                   => 'five',
        6                   => 'six',
        7                   => 'seven',
        8                   => 'eight',
        9                   => 'nine',
        10                  => 'ten',
        11                  => 'eleven',
        12                  => 'twelve',
        13                  => 'thirteen',
        14                  => 'fourteen',
        15                  => 'fifteen',
        16                  => 'sixteen',
        17                  => 'seventeen',
        18                  => 'eighteen',
        19                  => 'nineteen',
        20                  => 'twenty',
        30                  => 'thirty',
        40                  => 'forty',
        50                  => 'fifty',
        60                  => 'sixty',
        70                  => 'seventy',
        80                  => 'eighty',
        90                  => 'ninety',
        100                 => 'hundred',
    );

    if ( ! is_numeric( $number ) ) {
        return false;
    }

    if ( $number < 0 ) {
        return $negative . bw_convert_number_to_words( abs( $number ) );
    }

    $string = null;

    if ( $number < 21 ) {
        $string = $dictionary[$number];
    } elseif ( $number < 100 ) {
        $tens   = ( (int) ( $number / 10 ) ) * 10;
        $units  = $number % 10;
        $string = $dictionary[$tens];
        if ( $units ) {
            $string .= $hyphen . $dictionary[$units];
        }
    } elseif ( $number < 1000 ) {
        $hundreds  = $number / 100;
        $remainder = $number % 100;
        $string = $dictionary[ (int) $hundreds ] . ' ' . $dictionary[100];
        if ( $remainder ) {
            $string .= $conjunction . bw_convert_number_to_words( $remainder );
        }
    } else {
        return $number; // Fallback for very large numbers
    }

    return $string;
}

/**
 * 7. Shortcode: [bw_year_number]
 */
function bw_shortcode_year_number() {
    return bw_get_years_since();
}
add_shortcode( 'bw_year_number', 'bw_shortcode_year_number' );

/**
 * 8. Shortcode: [bw_year_word]
 */
function bw_shortcode_year_word() {
    $years = bw_get_years_since();
    return bw_convert_number_to_words( $years );
}
add_shortcode( 'bw_year_word', 'bw_shortcode_year_word' );