<?php
/**
 * Plugin Name: Bro Beer Reviews
 * Description: A rating system for the real bro to rate their brews.
 * Version: 1.0.0
 * Author: Real Bro
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Register Custom Post Type: Beer Review
function bro_register_beer_review_cpt() {
    $labels = array(
        'name'                  => 'Beer Reviews',
        'singular_name'         => 'Beer Review',
        'menu_name'             => 'Beer Reviews',
        'add_new_item'          => 'Add New Beer Review',
        'edit_item'             => 'Edit Beer Review',
        'new_item'              => 'New Beer Review',
        'view_item'             => 'View Beer Review',
        'search_items'          => 'Search Beer Reviews',
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'menu_icon'          => 'dashicons-beer',
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'rewrite'            => array( 'slug' => 'beer-reviews' ),
        'show_in_rest'       => true, // Enable Gutenberg
    );

    register_post_type( 'beer_review', $args );
}
add_action( 'init', 'bro_register_beer_review_cpt' );

// Add Meta Box for Rating
function bro_add_beer_rating_meta_box() {
    add_meta_box(
        'bro_beer_rating_box',
        'Bro Rating (1-5 Bottles)',
        'bro_render_beer_rating_meta_box',
        'beer_review',
        'side',
        'high'
    );
}
add_action( 'add_meta_boxes', 'bro_add_beer_rating_meta_box' );

function bro_render_beer_rating_meta_box( $post ) {
    $rating = get_post_meta( $post->ID, '_bro_beer_rating', true );
    wp_nonce_field( 'bro_save_beer_rating', 'bro_beer_rating_nonce' );
    ?>
    <select name="bro_beer_rating" style="width: 100%;">
        <?php for ( $i = 1; $i <= 5; $i++ ) : ?>
            <option value="<?php echo $i; ?>" <?php selected( $rating, $i ); ?>>
                <?php echo $i; ?> Bottle<?php echo $i > 1 ? 's' : ''; ?>
            </option>
        <?php endfor; ?>
    </select>
    <?php
}

// Save Meta Box Data
function bro_save_beer_rating_data( $post_id ) {
    if ( ! isset( $_POST['bro_beer_rating_nonce'] ) || ! wp_verify_nonce( $_POST['bro_beer_rating_nonce'], 'bro_save_beer_rating' ) ) {
        return;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    if ( isset( $_POST['bro_beer_rating'] ) ) {
        update_post_meta( $post_id, '_bro_beer_rating', sanitize_text_field( $_POST['bro_beer_rating'] ) );
    }
}
add_action( 'save_post', 'bro_save_beer_rating_data' );

// Display Rating on Frontend
function bro_display_beer_rating( $content ) {
    if ( is_singular( 'beer_review' ) && in_the_loop() && is_main_query() ) {
        $rating = get_post_meta( get_the_ID(), '_bro_beer_rating', true );
        if ( $rating ) {
            $stars = '';
            for ( $i = 1; $i <= 5; $i++ ) {
                $color = $i <= $rating ? '#FFBF00' : '#444';
                $stars .= '<span style="color:' . $color . '; font-size: 24px; margin-right: 5px;">🍺</span>';
            }
            $rating_html = '<div class="bro-beer-rating" style="margin-bottom: 20px; padding: 10px; background: #1a1a1a; border-radius: 8px; border-left: 4px solid #FFBF00;">';
            $rating_html .= '<strong style="color: #FFBF00; text-transform: uppercase; font-family: sans-serif;">Bro Rating:</strong><br>' . $stars;
            $rating_html .= '</div>';
            return $rating_html . $content;
        }
    }
    return $content;
}
add_filter( 'the_content', 'bro_display_beer_rating' );
