<?php
/**
 * Settings Page Display
 */

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

// Handle form submission
if ( isset( $_POST['submit'] ) ) {
    // Verify nonce.
    $nonce = isset( $_POST['ccspllms_settings_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['ccspllms_settings_nonce'] ) ) : '';
    if ( ! wp_verify_nonce( $nonce, 'ccspllms_settings' ) ) {
        return;
    }

    // Update settings with sanitization.
    update_option( 'ccspllms_enabled', isset( $_POST['ccspllms_enabled'] ) ? 1 : 0 );

    $site_title   = isset( $_POST['ccspllms_site_title'] ) ? sanitize_text_field( wp_unslash( $_POST['ccspllms_site_title'] ) ) : '';
    $site_tagline = isset( $_POST['ccspllms_site_tagline'] ) ? sanitize_textarea_field( wp_unslash( $_POST['ccspllms_site_tagline'] ) ) : '';

    update_option( 'ccspllms_site_title', $site_title );
    update_option( 'ccspllms_site_tagline', $site_tagline );

    // Handle post types (as comma-separated string).
    $post_types = array();
    if ( isset( $_POST['ccspllms_post_types'] ) && is_array( $_POST['ccspllms_post_types'] ) ) {
        $post_types = array_map( 'sanitize_text_field', wp_unslash( $_POST['ccspllms_post_types'] ) );
    }
    update_option( 'ccspllms_post_types', implode( ',', $post_types ) );

    $excerpt_priority = isset( $_POST['ccspllms_excerpt_priority'] ) ? sanitize_text_field( wp_unslash( $_POST['ccspllms_excerpt_priority'] ) ) : '';
    $word_limit       = isset( $_POST['ccspllms_word_limit'] ) ? intval( wp_unslash( $_POST['ccspllms_word_limit'] ) ) : 0;

    update_option( 'ccspllms_excerpt_priority', $excerpt_priority );
    update_option( 'ccspllms_word_limit', $word_limit );

    update_option( 'ccspllms_use_canonical', isset( $_POST['ccspllms_use_canonical'] ) ? 1 : 0 );
    update_option( 'ccspllms_auto_regenerate', isset( $_POST['ccspllms_auto_regenerate'] ) ? 1 : 0 );

    // Output safe success message.
    echo wp_kses_post( '<div class="ccspllms_alert success">Settings saved successfully!</div>' );
}


$enabled = get_option('ccspllms_enabled', 1);
$site_title = get_option('ccspllms_site_title', get_bloginfo('name'));
$site_tagline = get_option('ccspllms_site_tagline', get_bloginfo('description'));
$post_types = get_option('ccspllms_post_types', array('post', 'page'));
if (!is_array($post_types)) {
    if (empty($post_types)) {
        $post_types = array();
    } else {
        $post_types = explode(',', $post_types);
        $post_types = array_map('trim', $post_types);
    }
}

$excerpt_priority = get_option('ccspllms_excerpt_priority', 'excerpt_meta_content');
$word_limit = get_option('ccspllms_word_limit', 50);
$use_canonical = get_option('ccspllms_use_canonical', 1);
$auto_regenerate = get_option('ccspllms_auto_regenerate', 1);
?>

<div class="ccspllms_container">
    
    <div class="ccspllms_header">
        <div class="ccspllmshead_left">
            <div class="ccspllmshead_logo">
            <img src="<?php echo esc_html( CCSPLLMS_PLUGIN_URL . 'assets/cloudcusplogo.svg' ); ?>" alt="Logo" class="ccspllms_logo">
            </div>
            <div class="ccspllmshead_text"><h1>LLMS.txt  Generator</h1></div>
        </div>
        <div class="ccspllmshead_right">
             <h1><i class="fas fa-cogs"></i> Settings</h1>
        <p>Configure how your LLMS.txt file is generated</p>
        </div>
        
    </div>

    <div class="ccspllms_content">
        <form method="post" id="ccspllms_settings_form">
            <?php wp_nonce_field('ccspllms_settings', 'ccspllms_settings_nonce'); ?>
            
            <div class="ccspllms_card">
                <h3>General Settings</h3>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">Enable Plugin</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="ccspllms_enabled" <?php checked($enabled, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Enable or disable LLMS.txt file generation</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Site Title</th>
                        <td>
                            <input type="text" name="ccspllms_site_title" value="<?php echo esc_attr($site_title); ?>" class="regular-text">
                            <p class="description">Title to display in the LLMS.txt file header</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Site Tagline</th>
                        <td>
                            <textarea name="ccspllms_site_tagline" rows="3" class="large-text"><?php echo esc_textarea($site_tagline); ?></textarea>
                            <p class="description">Optional tagline or description for your site</p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="ccspllms_card">
                <h3>Content Settings</h3>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">Post Types</th>
                        <td>
                            <?php
                            $available_post_types = get_post_types(array('public' => true), 'objects');
                            foreach ( $available_post_types as $post_type ) {
                                $is_checked = in_array( $post_type->name, $post_types, true ) ? 'checked="checked"' : '';
                                ?>
                                <label>
                                    <input type="checkbox" 
                                        name="ccspllms_post_types[]" 
                                        value="<?php echo esc_attr( $post_type->name ); ?>" 
                                        <?php echo esc_html( $is_checked ); ?> />
                                    <?php echo esc_html( $post_type->labels->name ); ?>
                                </label>
                                <br>
                                <?php
                            }

                            ?>
                            <p class="description">Select which post types to include in the LLMS.txt file</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Excerpt Priority</th>
                        <td>
                            <select name="ccspllms_excerpt_priority">
                                <option value="excerpt_meta_content" <?php selected($excerpt_priority, 'excerpt_meta_content'); ?>>Excerpt → Meta → Content</option>
                                <option value="meta_excerpt_content" <?php selected($excerpt_priority, 'meta_excerpt_content'); ?>>Meta → Excerpt → Content</option>
                                <option value="content_excerpt_meta" <?php selected($excerpt_priority, 'content_excerpt_meta'); ?>>Content → Excerpt → Meta</option>
                            </select>
                            <p class="description">Priority order for generating post descriptions</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Word Limit</th>
                        <td>
                            <input type="number" name="ccspllms_word_limit" value="<?php echo esc_attr($word_limit); ?>" min="10" max="200" class="small-text">
                            <p class="description">Maximum words per post description</p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="ccspllms_card">
                <h3>Publishing Options</h3>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">Use Canonical URLs</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="ccspllms_use_canonical" <?php checked($use_canonical, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Use canonical URLs from SEO plugins when available</p>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row">Auto Regenerate</th>
                        <td>
                            <label class="ccspllms_toggle">
                                <input type="checkbox" name="ccspllms_auto_regenerate" <?php checked($auto_regenerate, 1); ?>>
                                <span class="ccspllms_slider"></span>
                            </label>
                            <p class="description">Automatically regenerate file when posts are updated</p>
                        </td>
                    </tr>
                </table>
            </div>

            <p class="submit">
                <input type="submit" name="submit" class="ccspllms_button" value="Save Settings">
                <!-- <button type="button" id="reset_settings" class="ccspllms_button" style="background: #dc3545;">Reset to Defaults</button> -->
            </p>
        </form>
    </div>
</div>