<?php
namespace BLR\Backline\Modules\Contacts;

use BLR\Backline\Core\Access;
use BLR\Backline\Core\AdminMenu;
use BLR\Backline\Core\Sanitizer;

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

final class Contacts {
    public static function type_options(): array {
        return ['Venue', 'Promoter', 'Label', 'Blog', 'Playlist', 'Sync', 'Press', 'Fan', 'Other'];
    }

    public static function normalize_type(string $type): string {
        $type = trim(sanitize_text_field($type));
        if ($type === '') return '';

        foreach (self::type_options() as $option) {
            if (strcasecmp($option, $type) === 0) {
                return $option;
            }
        }

        return 'Other';
    }

    public static function type_label(string $type): string {
        switch ($type) {
            case 'Venue':
                return __('Venue', 'blr-backline');
            case 'Promoter':
                return __('Promoter', 'blr-backline');
            case 'Label':
                return __('Label', 'blr-backline');
            case 'Blog':
                return __('Blog', 'blr-backline');
            case 'Playlist':
                return __('Playlist', 'blr-backline');
            case 'Sync':
                return __('Sync', 'blr-backline');
            case 'Press':
                return __('Press', 'blr-backline');
            case 'Fan':
                return __('Fan', 'blr-backline');
            case 'Other':
                return __('Other', 'blr-backline');
            default:
                return $type;
        }
    }

    public static function register(): void {
        add_action('init', [self::class, 'register_cpt']);
        add_action('init', [self::class, 'register_meta'], 11);
    }

    public static function register_cpt(): void {
        register_post_type('blr_contact', [
            'labels' => [
                'name' => __('Contacts', 'blr-backline'),
                'singular_name' => __('Contact', 'blr-backline'),
            ],
            'public' => false,
            'show_ui' => false,
            'show_in_menu' => false,
            'show_in_rest' => false,
            'supports' => ['title','editor','custom-fields','revisions','author'],
        ] + Access::post_type_registration_args('blr_contact'));
    }

    public static function register_meta(): void {
        self::reg_meta('blr_contact', 'blr_contact_first_name', 'string');
        self::reg_meta('blr_contact', 'blr_contact_email', 'string');
        self::reg_meta('blr_contact', 'blr_contact_phone', 'string');
        self::reg_meta('blr_contact', 'blr_contact_role', 'string');
        self::reg_meta('blr_contact', 'blr_contact_type', 'string');
        self::reg_meta('blr_contact', 'blr_contact_company', 'string');
        self::reg_meta('blr_contact', 'blr_contact_city', 'string');
        self::reg_meta('blr_contact', 'blr_contact_state', 'string');
        self::reg_meta('blr_contact', 'blr_contact_website', 'string');
        self::reg_meta('blr_contact', 'blr_contact_newsletter_signup', 'string');
        self::reg_meta('blr_contact', NewsletterDoubleOptIn::META_STATUS, 'string');
        self::reg_meta('blr_contact', NewsletterDoubleOptIn::META_CONFIRM_TOKEN_HASH, 'string');
        self::reg_meta('blr_contact', NewsletterDoubleOptIn::META_CONFIRM_EXPIRES_GMT, 'string');
        self::reg_meta('blr_contact', NewsletterDoubleOptIn::META_REQUESTED_GMT, 'string');
        self::reg_meta('blr_contact', NewsletterDoubleOptIn::META_CONFIRMED_GMT, 'string');
        self::reg_meta('blr_contact', 'blr_contact_notes', 'string');
        self::reg_meta('blr_contact', 'blr_contact_last_contacted', 'string');
        self::reg_meta('blr_contact', 'blr_contact_total_emails_sent', 'integer');
        self::reg_meta('blr_contact', 'blr_contact_last_reply', 'string');
        self::reg_meta('blr_contact', 'blr_contact_outreach_status', 'string');
    }

    private static function reg_meta(string $post_type, string $key, string $type): void {
        register_post_meta($post_type, $key, [
            'type' => $type,
            'single' => true,
            'show_in_rest' => false,
            'sanitize_callback' => static function ($value) use ($key, $type) {
                if ($key === 'blr_contact_type') {
                    return self::normalize_type((string) $value);
                }

                if ($key === NewsletterDoubleOptIn::META_STATUS) {
                    return NewsletterDoubleOptIn::normalize_status((string) $value);
                }

                return Sanitizer::meta_value($key, $value, $type);
            },
            'auth_callback' => Access::rest_meta_auth_callback($post_type),
        ]);
    }
}