<?php
/**
 * Settings page for BW Expiry Post
 */

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

class BW_Expiry_Settings {

    private static $instance = null;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
        add_action( 'admin_init', array( $this, 'register_settings' ) );
        add_filter( 'plugin_action_links_' . BW_EXPIRY_PLUGIN_BASENAME, array( $this, 'add_settings_link' ) );
    }

    /**
     * Add settings page to admin menu
     */
    public function add_settings_page() {
        add_options_page(
            __( 'Expiry Post Config', 'bw-expiry-post' ),
            __( 'Expiry Post Config', 'bw-expiry-post' ),
            'manage_options',
            'bw-expiry-post',
            array( $this, 'render_settings_page' )
        );
    }

    /**
     * Add settings link to plugins page
     */
    public function add_settings_link( $links ) {
        $settings_link = sprintf(
            '<a href="%s">%s</a>',
            admin_url( 'options-general.php?page=bw-expiry-post' ),
            __( 'Settings', 'bw-expiry-post' )
        );
        array_unshift( $links, $settings_link );
        return $links;
    }

    /**
     * Register settings
     */
    public function register_settings() {
        register_setting(
            'bw_expiry_settings_group',
            'bw_expiry_settings',
            array( $this, 'sanitize_settings' )
        );

        // Post types section
        add_settings_section(
            'bw_expiry_post_types_section',
            __( 'Enabled Post Types', 'bw-expiry-post' ),
            array( $this, 'render_post_types_section' ),
            'bw-expiry-post'
        );

        add_settings_field(
            'bw_expiry_post_types',
            __( 'Post Types', 'bw-expiry-post' ),
            array( $this, 'render_post_types_field' ),
            'bw-expiry-post',
            'bw_expiry_post_types_section'
        );
    }

    /**
     * Sanitize settings before save
     */
    public function sanitize_settings( $input ) {
        $sanitized = array();

        // Post types
        $sanitized['post_types'] = isset( $input['post_types'] ) && is_array( $input['post_types'] )
            ? array_map( 'sanitize_key', $input['post_types'] )
            : array( 'post' );

        return $sanitized;
    }

    /**
     * Render settings page
     */
    public function render_settings_page() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Show save message
        if ( isset( $_GET['settings-updated'] ) ) {
            add_settings_error(
                'bw_expiry_messages',
                'bw_expiry_message',
                __( 'Settings saved.', 'bw-expiry-post' ),
                'updated'
            );
        }

        settings_errors( 'bw_expiry_messages' );
        ?>
        <div class="wrap">
            <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

            <div class="bw-expiry-settings-wrap">
                <form action="options.php" method="post">
                    <?php
                    settings_fields( 'bw_expiry_settings_group' );
                    do_settings_sections( 'bw-expiry-post' );
                    submit_button( __( 'Save Settings', 'bw-expiry-post' ) );
                    ?>
                </form>
            </div>

            <div class="bw-expiry-info-box">
                <h3><?php esc_html_e( 'How It Works', 'bw-expiry-post' ); ?></h3>
                <p><?php esc_html_e( 'When a post reaches its expiry date, it will automatically be set to "Draft" status. This ensures the post is no longer publicly visible while preserving all its content for future use.', 'bw-expiry-post' ); ?></p>
                <p><?php esc_html_e( 'The plugin checks for expired posts hourly via WordPress cron, and also on each page load for immediate expiry.', 'bw-expiry-post' ); ?></p>
            </div>
        </div>
        <?php
    }

    /**
     * Render post types section description
     */
    public function render_post_types_section() {
        echo '<p>' . esc_html__( 'Select which post types should have the expiry feature enabled.', 'bw-expiry-post' ) . '</p>';
    }

    /**
     * Render post types checkboxes
     */
    public function render_post_types_field() {
        $options = get_option( 'bw_expiry_settings', array() );
        $enabled_types = isset( $options['post_types'] ) ? $options['post_types'] : array( 'post' );

        // Get all public post types
        $post_types = get_post_types( array( 'public' => true ), 'objects' );

        // Exclude attachments
        unset( $post_types['attachment'] );

        foreach ( $post_types as $post_type ) {
            $checked = in_array( $post_type->name, $enabled_types );
            ?>
            <label class="bw-expiry-post-type-option">
                <input
                    type="checkbox"
                    name="bw_expiry_settings[post_types][]"
                    value="<?php echo esc_attr( $post_type->name ); ?>"
                    <?php checked( $checked ); ?>
                >
                <?php echo esc_html( $post_type->labels->singular_name ); ?>
                <span class="description">(<?php echo esc_html( $post_type->name ); ?>)</span>
            </label>
            <br>
            <?php
        }
    }
}
