<?php
/**
 * Plugin Name: Lite Maintenance Mode
 * Plugin URI: https://dharmendrapatidar.com/lite-maintenance-mode/
 * Description: Displays a custom maintenance page for visitors while the site is under maintenance.
 * Version: 1.0.3
 * Author: Dharmendra Patidar
 * Author URI: https://in.linkedin.com/in/dharmendra-patidar
 * License: GPLv2 or later
 * Text Domain: lite-maintenance-mode
 * Domain Path: /languages
 */

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

define( 'LITEMAMO_VERSION', '1.0.3' );
define( 'LITEMAMO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'LITEMAMO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

// Load includes
require_once LITEMAMO_PLUGIN_DIR . 'includes/public/maintenance-page.php';
require_once LITEMAMO_PLUGIN_DIR . 'includes/admin/admin-menu.php';
require_once LITEMAMO_PLUGIN_DIR . 'includes/admin/settings-page.php';

/**
 * Step 1: Load Translation Files
 */
function litemamo_load_textdomain() {
    load_plugin_textdomain( 
        'lite-maintenance-mode', 
        false, 
        dirname( plugin_basename( __FILE__ ) ) . '/languages' 
    );
}
add_action( 'init', 'litemamo_load_textdomain' );

// Admin enqueue
add_action( 'admin_enqueue_scripts', function( $hook ) {
    if ( $hook === 'toplevel_page_litemamo_settings' ) {
        wp_enqueue_media();
        wp_enqueue_script(
            'litemamo-admin-js',
            LITEMAMO_PLUGIN_URL . 'assets/js/litemamo-admin.js',
            [ 'jquery' ],
            LITEMAMO_VERSION,
            true
        );
        wp_enqueue_style(
            'litemamo-admin-css',
            LITEMAMO_PLUGIN_URL . 'assets/css/litemamo-admin.css',
            [],
            LITEMAMO_VERSION
        );
    }
});

// Frontend enqueue
add_action( 'wp_enqueue_scripts', function() {
    $options = get_option( 'litemamo_settings', [] );
    if ( ! empty( $options['enabled'] ) && ! current_user_can( 'manage_options' ) ) {
        wp_enqueue_style(
            'litemamo-style',
            LITEMAMO_PLUGIN_URL . 'assets/css/litemamo-style.css',
            [],
            LITEMAMO_VERSION
        );
        wp_enqueue_style(
            'litemamo-mode',
            LITEMAMO_PLUGIN_URL . 'assets/css/mode.css',
            [],
            LITEMAMO_VERSION
        );
    }
});

// Activation hook
register_activation_hook( __FILE__, 'litemamo_upload_default_images' );

function litemamo_upload_default_images() {
    $options = get_option( 'litemamo_settings', [] );

    $bg_image_name   = 'litemamo-bg.jpg';
    $logo_image_name = 'litemamo-logo.png';
    $fav_icon_name   = 'litemamo-logo.png';

    $bg_image_path   = LITEMAMO_PLUGIN_DIR . 'assets/images/' . $bg_image_name;
    $logo_image_path = LITEMAMO_PLUGIN_DIR . 'assets/images/' . $logo_image_name;
    $fav_image_path  = LITEMAMO_PLUGIN_DIR . 'assets/images/' . $fav_icon_name;

    $bg_image_url   = litemamo_get_or_upload_image( $bg_image_name, $bg_image_path );
    $logo_image_url = litemamo_get_or_upload_image( $logo_image_name, $logo_image_path );
    $fav_image_url  = litemamo_get_or_upload_image( $fav_icon_name, $fav_image_path );

    if ( empty( $options['bg_image'] ) && $bg_image_url ) {
        $options['bg_image'] = $bg_image_url;
    }
    if ( empty( $options['main_image'] ) && $logo_image_url ) {
        $options['main_image'] = $logo_image_url;
    }
    if ( empty( $options['fav_icon'] ) && $fav_image_url ) {
        $options['fav_icon'] = $fav_image_url;
    }

    update_option( 'litemamo_settings', $options );
}

// Helper functions for image upload (Remain same)
function litemamo_get_or_upload_image( $file_name, $file_path ) {
    $attachment_id = litemamo_get_attachment_id_by_filename( $file_name );
    if ( $attachment_id ) {
        return wp_get_attachment_url( $attachment_id );
    }
    return litemamo_upload_image_to_media( $file_path, $file_name );
}

function litemamo_get_attachment_id_by_filename( $file_name ) {
    $args = [
        'post_type'      => 'attachment',
        'title'          => pathinfo( $file_name, PATHINFO_FILENAME ),
        'posts_per_page' => 1,
        'fields'         => 'ids',
    ];
    $attachments = get_posts( $args );
    return ! empty( $attachments ) ? intval( $attachments[0] ) : 0;
}

function litemamo_upload_image_to_media( $file_path, $file_name ) {
    if ( ! file_exists( $file_path ) ) {
        return '';
    }
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    require_once ABSPATH . 'wp-admin/includes/image.php';

    $filetype = wp_check_filetype( $file_name, null );
    $upload   = wp_upload_bits( $file_name, null, @file_get_contents( $file_path ) );

    if ( ! $upload['error'] ) {
        $attachment = [
            'post_mime_type' => $filetype['type'],
            'post_title'     => sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) ),
            'post_content'   => '',
            'post_status'    => 'inherit',
        ];
        $attach_id   = wp_insert_attachment( $attachment, $upload['file'] );
        $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
        wp_update_attachment_metadata( $attach_id, $attach_data );

        return $upload['url'];
    }
    return '';
}

/**
 * Step 2: Settings link on Plugin page
 */
function litemamo_add_settings_link( $links ) {
    $settings_link = '<a href="' . admin_url( 'admin.php?page=litemamo_settings' ) . '">' . __( 'Settings', 'lite-maintenance-mode' ) . '</a>';
    array_unshift( $links, $settings_link );
    return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'litemamo_add_settings_link' );