<?php
namespace Storzen;
defined('ABSPATH') || exit;

class Tab {
    /** @var string */    protected $slug;
    /** @var string */    protected $title;
    /** @var string */    protected $icon        = '';
    /** @var string */    protected $description = '';
    /** @var string */    protected $option_key  = '';
    /** @var bool   */    protected $pro         = false;
    /** @var Menu|null */ protected $menu        = null;
    /** @var Section[] */ protected $sections    = array();

    /** @return static */
    public static function make( $slug, $title ) { return new static( $slug, $title ); }

    public function __construct( $slug, $title ) {
        $this->slug  = $slug;
        $this->title = $title;
    }

    /** @return static */ public function set_icon( $i )        { $this->icon = $i;        return $this; }
    /** @return static */ public function icon( $i )            { return $this->set_icon($i); }
    /** @return static */ public function set_description( $d ) { $this->description = $d; return $this; }
    /** @return static */ public function description( $d )     { return $this->set_description($d); }
    /** @return static */ public function set_option_key( $k )  { $this->option_key = $k;  return $this; }
    /** @return static */ public function option_key( $k )      { return $this->set_option_key($k); }
    /** @return static */ public function set_pro( $p = true )  { $this->pro = (bool)$p;   return $this; }
    /** @return static */ public function pro( $p = true )      { return $this->set_pro($p); }
    public function is_pro() { return $this->pro; }

    /** @return static */
    public function add_section( Section $section ) {
        $section->set_tab( $this );
        $this->sections[] = $section;
        return $this;
    }

    public function set_menu( Menu $menu ) { $this->menu = $menu; }

    public function get_slug()        { return $this->slug; }
    public function get_title()       { return $this->title; }
    public function get_icon()        { return $this->icon; }
    public function get_description() { return $this->description; }
    public function get_sections()    { return $this->sections; }
    public function get_menu()        { return $this->menu; }

    public function get_option_key() {
        if ( $this->option_key ) return $this->option_key;
        $panel = $this->menu ? $this->menu->get_panel() : null;
        $parts = array_filter( array(
            'pk',
            $panel ? $panel->get_slug() : '',
            $this->menu ? $this->menu->get_slug() : '',
            $this->slug,
        ) );
        return implode( '_', $parts );
    }

    /** All fields keyed by slug */
    public function get_all_fields() {
        $out = array();
        foreach ( $this->sections as $s ) {
            foreach ( $s->get_fields() as $f ) {
                $out[ $f->get_slug() ] = $f;
            }
        }
        return $out;
    }

    /** Get saved values merged with defaults */
    public function get_values() {
        $saved    = (array) get_option( $this->get_option_key(), array() );
        $defaults = array();
        foreach ( $this->get_all_fields() as $slug => $f ) {
            $defaults[ $slug ] = $f->get_default();
        }
        return array_merge( $defaults, $saved );
    }

    /** Sanitize raw POST and save to wp_options */
    public function save( $raw ) {
        $raw           = is_array($raw) ? $raw : array();
        $is_pro_active = defined('STORZEN_IS_PRO') && STORZEN_IS_PRO === 'yes';
        $previous      = (array) get_option( $this->get_option_key(), array() );
        $out           = array();
        foreach ( $this->get_all_fields() as $slug => $field ) {
            if ( $field->get_type() === 'notice' ) continue;

            // Per-field PRO gating: without an active license, PRO fields keep
            // their previously stored value (or default) and ignore submitted
            // input, so a free user cannot set PRO-only options by tampering
            // with the POST payload.
            if ( $field->is_pro() && ! $is_pro_active ) {
                $out[ $slug ] = isset($previous[$slug]) ? $previous[$slug] : $field->get_default();
                continue;
            }

            $out[ $slug ] = $field->sanitize( isset($raw[$slug]) ? $raw[$slug] : null );
        }
        update_option( $this->get_option_key(), $out );
        return $out;
    }
}
