<?php

/**
 * Subscription Notes CPT Registration
 *
 * Registers the subscription notes custom post type and meta fields.
 *
 * @package ArraySubs\Features\SubscriptionNotes\Services
 */

namespace ArraySubs\Features\SubscriptionNotes\Services;

defined('ABSPATH') || exit;

/**
 * Initialize subscription notes CPT
 */
class Init
{
    /**
     * Enable auto-loading
     *
     * @var bool
     */
    public static $loadable = true;

    /**
     * Constructor
     */
    public function __construct()
    {
        add_action('init', [$this, 'registerPostType'], 5);
        add_action('init', [$this, 'registerMetaFields'], 10);
    }

    /**
     * Register the subscription notes custom post type
     *
     * @return void
     */
    public function registerPostType(): void
    {
        $labels = [
            'name'               => __('Subscription Notes', 'arraysubs'),
            'singular_name'      => __('Subscription Note', 'arraysubs'),
            'add_new'            => __('Add New', 'arraysubs'),
            'add_new_item'       => __('Add New Note', 'arraysubs'),
            'edit_item'          => __('Edit Note', 'arraysubs'),
            'new_item'           => __('New Note', 'arraysubs'),
            'view_item'          => __('View Note', 'arraysubs'),
            'search_items'       => __('Search Notes', 'arraysubs'),
            'not_found'          => __('No notes found', 'arraysubs'),
            'not_found_in_trash' => __('No notes found in trash', 'arraysubs'),
        ];

        $args = [
            'labels'              => $labels,
            'public'              => false,
            'publicly_queryable'  => false,
            'show_ui'             => false,
            'show_in_menu'        => false,
            'query_var'           => false,
            'rewrite'             => false,
            'capability_type'     => 'post',
            'has_archive'         => false,
            'hierarchical'        => false,
            'show_in_rest'        => true,
            'rest_base'           => 'subscription-notes',
            'supports'            => ['editor'],
            'menu_position'       => null,
        ];

        register_post_type('arraysubs_sub_note', $args);
    }

    /**
     * Register meta fields for subscription notes
     *
     * @return void
     */
    public function registerMetaFields(): void
    {
        $meta_fields = [
            '_subscription_id' => [
                'type'              => 'integer',
                'description'       => 'Parent subscription ID',
                'default'           => 0,
                'sanitize_callback' => 'absint',
            ],
            '_note_type' => [
                'type'              => 'string',
                'description'       => 'Note type: private or customer',
                'default'           => 'private',
                'sanitize_callback' => [$this, 'sanitizeNoteType'],
            ],
            '_added_by' => [
                'type'              => 'integer',
                'description'       => 'User ID who added the note (0 for system)',
                'default'           => 0,
                'sanitize_callback' => 'absint',
            ],
            '_is_system' => [
                'type'              => 'boolean',
                'description'       => 'Whether this is an auto-generated system note',
                'default'           => false,
                'sanitize_callback' => 'rest_sanitize_boolean',
            ],
            '_audit_entity' => [
                'type'              => 'string',
                'description'       => 'Entity type for audit classification',
                'default'           => '',
                'sanitize_callback' => 'sanitize_key',
            ],
            '_event_type' => [
                'type'              => 'string',
                'description'       => 'Structured event type for timeline and reporting classification',
                'default'           => '',
                'sanitize_callback' => 'sanitize_key',
            ],
        ];

        foreach ($meta_fields as $key => $config) {
            register_post_meta('arraysubs_sub_note', $key, [
                'type'              => $config['type'],
                'description'       => $config['description'],
                'single'            => true,
                'show_in_rest'      => true,
                'default'           => $config['default'],
                'sanitize_callback' => $config['sanitize_callback'],
                'auth_callback'     => function () {
                    return current_user_can('edit_posts');
                },
            ]);
        }
    }

    /**
     * Sanitize note type value
     *
     * @param string $value The note type value.
     * @return string Sanitized value
     */
    public function sanitizeNoteType(string $value): string
    {
        return in_array($value, ['private', 'customer', 'scheduled_job', 'portal_action_failure'], true) ? $value : 'private';
    }
}
