<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Enqueues the necessary styles for the maintenance page.
 */
function litemamo_enqueue_maintenance_assets() {
    wp_enqueue_style(
        'litemamo-maintenance-style',
        plugin_dir_url( __FILE__ ) . 'assets/css/maintenance-style.css',
        array(),
        '1.0.0'
    );

    $options  = get_option( 'litemamo_settings', [] );
    $bg_image = ! empty( $options['bg_image'] ) ? esc_url( $options['bg_image'] ) : '';

    if ( ! empty( $bg_image ) ) {
        $dynamic_css = ".maintenance { background-image: url('{$bg_image}'); }";
        wp_add_inline_style( 'litemamo-maintenance-style', $dynamic_css );
    }
}

// Show maintenance page
add_action( 'template_redirect', 'litemamo_show_maintenance_page' );
function litemamo_show_maintenance_page() {
    $options = get_option( 'litemamo_settings', [] );
    $enabled = ! empty( $options['enabled'] );

    if ( $enabled && ! current_user_can( 'manage_options' ) ) {
        litemamo_enqueue_maintenance_assets();
        
        status_header( 503 );
        header( 'Retry-After: 3600' );
        nocache_headers();

        echo litemamo_get_maintenance_html();
        exit;
    }
}

/**
 * Generate maintenance page HTML
 */
function litemamo_get_maintenance_html() {
    $options = get_option( 'litemamo_settings', [] );

    // Prepare values
    $heading    = isset( $options['heading'] )    ? $options['heading']    : '';
    $subheading = isset( $options['subheading'] ) ? $options['subheading'] : '';
    $message    = isset( $options['message'] )    ? $options['message']    : '';
    $main_image = isset( $options['main_image'] ) ? $options['main_image'] : '';
    $fav_icon    = ! empty( $options['fav_icon'] ) ? $options['fav_icon'] : $main_image;

    // Social Links
    $facebook   = isset( $options['facebook'] )   ? $options['facebook']  : '';
    $instagram  = isset( $options['instagram'] )  ? $options['instagram'] : '';
    $twitter    = isset( $options['twitter'] )    ? $options['twitter']   : '';
    $whatsapp   = isset( $options['whatsapp'] )   ? $options['whatsapp']  : '';

    ob_start(); ?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo esc_html( get_bloginfo( 'name' ) ); ?> — <?php esc_html_e( 'Maintenance', 'lite-maintenance-mode' ); ?></title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">

    <?php if ( ! empty( $fav_icon ) ) : ?>
        <link rel="icon" type="image/x-icon" href="<?php echo esc_url( $fav_icon ); ?>?v=<?php echo time(); ?>">
        <link rel="apple-touch-icon" href="<?php echo esc_url( $fav_icon ); ?>">
    <?php endif; ?>

    <?php wp_head(); ?>
</head>
<body <?php body_class( 'maintenance-body' ); ?>>
<div class="maintenance">
  <div class="maintenance_contain">
    <?php if ( ! empty( $main_image ) ) : ?>
        <img src="<?php echo esc_url( $main_image ); ?>" class="mamo-logo" alt="<?php esc_attr_e( 'Logo', 'lite-maintenance-mode' ); ?>">
    <?php endif; ?>

    <div class="row-box">
        <?php if ( ! empty( $subheading ) ) : ?>
            <span class="pp-infobox-title-prefix"><?php echo esc_html( $subheading ); ?></span>
        <?php endif; ?>

        <?php if ( ! empty( $heading ) ) : ?>
        <div class="pp-infobox-title-wrapper">
            <h3 class="pp-infobox-title"><?php echo esc_html( $heading ); ?></h3>
        </div>
        <?php endif; ?>

        <?php if ( ! empty( $message ) ) : ?>
            <div class="pp-infobox-description"><?php echo wp_kses_post( $message ); ?></div>
        <?php endif; ?>

        <div class="mamo-social-icons">
            <?php if ( ! empty( $facebook ) ) : ?>
                <a href="<?php echo esc_url( $facebook ); ?>" target="_blank" aria-label="<?php esc_attr_e( 'Facebook', 'lite-maintenance-mode' ); ?>"><i class="fab fa-facebook-f"></i></a>
            <?php endif; ?>
            <?php if ( ! empty( $instagram ) ) : ?>
                <a href="<?php echo esc_url( $instagram ); ?>" target="_blank" aria-label="<?php esc_attr_e( 'Instagram', 'lite-maintenance-mode' ); ?>"><i class="fab fa-instagram"></i></a>
            <?php endif; ?>
            <?php if ( ! empty( $twitter ) ) : ?>
                <a href="<?php echo esc_url( $twitter ); ?>" target="_blank" aria-label="<?php esc_attr_e( 'Twitter', 'lite-maintenance-mode' ); ?>"><i class="fab fa-twitter"></i></a>
            <?php endif; ?>
            <?php if ( ! empty( $whatsapp ) ) : ?>
                <a href="https://wa.me/<?php echo esc_attr( $whatsapp ); ?>" target="_blank" aria-label="<?php esc_attr_e( 'WhatsApp', 'lite-maintenance-mode' ); ?>"><i class="fab fa-whatsapp"></i></a>
            <?php endif; ?>
        </div>
    </div>
  </div>

  <div class="mamo-copyright">
      <?php 
        printf( 
            /* translators: 1: Current Year, 2: Site Name */
            esc_html__( 'Copyright © %1$s %2$s - All Rights Reserved.', 'lite-maintenance-mode' ), 
            date('Y'), 
            get_bloginfo('name') 
        ); 
      ?>
  </div>
</div>
<?php wp_footer(); ?>
</body>
</html>
<?php
    return ob_get_clean();
}

add_action( 'wp_head', function() {
    $options = get_option( 'litemamo_settings', [] );
    if ( ! empty( $options['enabled'] ) && ! current_user_can( 'manage_options' ) ) {
        remove_action( 'wp_head', 'wp_site_icon', 99 );
    }
}, 1 );