<?php

/**
 * Settings API registration and handling.
 *
 * @package MediaTasker
 */
// Prevent direct file access.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Class Mediat_Settings
 *
 * Handles registering settings, sections, and fields for the plugin.
 */
class Mediat_Settings {
    /**
     * Hook into WordPress.
     *
     * @return void
     */
    public static function init() {
        add_action( 'admin_init', array(__CLASS__, 'register_settings') );
    }

    /**
     * Register plugin settings.
     *
     * @return void
     */
    public static function register_settings() {
        // Register a single option array to store all settings.
        register_setting( 
            'mediat_settings',
            // Option group
            'mediat_options',
            // Option name
            array(
                'type'              => 'array',
                'sanitize_callback' => array(__CLASS__, 'sanitize_options'),
                'default'           => self::get_default_options(),
            )
         );
    }

    /**
     * Get the default plugin options.
     *
     * @return array
     */
    public static function get_default_options() {
        return array(
            'max_file_size'        => 5,
            'allowed_file_types'   => 'jpg,jpeg,png,gif,pdf',
            'default_status'       => 'mediat_pending',
            'allowed_roles'        => array('administrator', 'editor'),
            'notification_emails'  => '',
            'notify_status_change' => false,
            'notify_comments'      => false,
        );
    }

    /**
     * Get a specific option value.
     *
     * @param string $key The option key.
     * @return mixed
     */
    public static function get_option( $key ) {
        $options = get_option( 'mediat_options', self::get_default_options() );
        $defaults = self::get_default_options();
        return ( isset( $options[$key] ) ? $options[$key] : (( isset( $defaults[$key] ) ? $defaults[$key] : null )) );
    }

    /**
     * Sanitize options before saving to the database.
     *
     * @param array $input The submitted options.
     * @return array The sanitized options.
     */
    public static function sanitize_options( $input ) {
        $sanitized = array();
        $defaults = self::get_default_options();
        // Max File Size: positive integer.
        if ( isset( $input['max_file_size'] ) ) {
            $sanitized['max_file_size'] = absint( $input['max_file_size'] );
            if ( $sanitized['max_file_size'] === 0 ) {
                $sanitized['max_file_size'] = $defaults['max_file_size'];
            }
        }
        // Allowed File Types: text field, strip spaces and HTML.
        if ( isset( $input['allowed_file_types'] ) ) {
            // Remove spaces, sanitize text, force lowercase.
            $types = str_replace( ' ', '', strtolower( sanitize_text_field( $input['allowed_file_types'] ) ) );
            $sanitized['allowed_file_types'] = ( empty( $types ) ? $defaults['allowed_file_types'] : $types );
        }
        // Default Status: must be a registered status.
        if ( isset( $input['default_status'] ) ) {
            $valid_statuses = Mediat_Post_Types::get_task_statuses();
            $status_key = sanitize_key( $input['default_status'] );
            if ( array_key_exists( $status_key, $valid_statuses ) ) {
                $sanitized['default_status'] = $status_key;
            } else {
                $sanitized['default_status'] = $defaults['default_status'];
            }
        }
        return $sanitized;
    }

}
