<?php
declare(strict_types=1);

namespace Itdatex\Ausgangsrechnung\Admin;

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

use Itdatex\Ausgangsrechnung\Admin\Pages\CreatePage;
use Itdatex\Ausgangsrechnung\Admin\Pages\InvoiceDetail;
use Itdatex\Ausgangsrechnung\Admin\Pages\IssuedList;
use Itdatex\Ausgangsrechnung\Admin\Pages\SettingsPage;
use Itdatex\Ausgangsrechnung\Plugin;

/**
 * Top-Level "Ausgangsrechnung" mit Submenues.
 */
final class Menu
{
    public const SLUG_LIST     = 'ausrg-list';
    public const SLUG_CREATE   = 'ausrg-create';
    public const SLUG_DETAIL   = 'ausrg-detail';
    public const SLUG_SETTINGS = 'ausrg-settings';

    public function register(): void
    {
        add_action( 'admin_menu', [ $this, 'add_menu' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
    }

    public function add_menu(): void
    {
        add_menu_page(
            __( 'Ausgangsrechnung', 'itdatex-e-invoice-output' ),
            __( 'Ausgangsrechnung', 'itdatex-e-invoice-output' ),
            Plugin::CAPABILITY,
            self::SLUG_LIST,
            class_exists( IssuedList::class ) ? [ IssuedList::class, 'render' ] : [ self::class, 'render_placeholder' ],
            'dashicons-media-document',
            31
        );

        if ( class_exists( IssuedList::class ) ) {
            add_submenu_page(
                self::SLUG_LIST,
                __( 'Ausgegangen', 'itdatex-e-invoice-output' ),
                __( 'Ausgegangen', 'itdatex-e-invoice-output' ),
                Plugin::CAPABILITY,
                self::SLUG_LIST,
                [ IssuedList::class, 'render' ]
            );
        }

        if ( class_exists( CreatePage::class ) ) {
            add_submenu_page(
                self::SLUG_LIST,
                __( 'Neue Rechnung', 'itdatex-e-invoice-output' ),
                __( 'Neue Rechnung', 'itdatex-e-invoice-output' ),
                Plugin::CAPABILITY,
                self::SLUG_CREATE,
                [ CreatePage::class, 'render' ]
            );
        }

        add_submenu_page(
            self::SLUG_LIST,
            __( 'Einstellungen', 'itdatex-e-invoice-output' ),
            __( 'Einstellungen', 'itdatex-e-invoice-output' ),
            Plugin::CAPABILITY,
            self::SLUG_SETTINGS,
            [ SettingsPage::class, 'render' ]
        );

        if ( class_exists( InvoiceDetail::class ) ) {
            // Versteckte Detail-Seite — wird nur per Link erreicht
            add_submenu_page(
                (string) null,
                __( 'Rechnung', 'itdatex-e-invoice-output' ),
                __( 'Rechnung', 'itdatex-e-invoice-output' ),
                Plugin::CAPABILITY,
                self::SLUG_DETAIL,
                [ InvoiceDetail::class, 'render' ]
            );
        }
    }

    public static function render_placeholder(): void
    {
        echo '<div class="wrap"><h1>' . esc_html__( 'Ausgangsrechnung', 'itdatex-e-invoice-output' ) . '</h1>';
        echo '<p>' . esc_html__( 'Listen-Ansicht folgt. Bitte zunaechst die Einstellungen pflegen.', 'itdatex-e-invoice-output' ) . '</p></div>';
    }

    public function enqueue_assets( string $hook ): void
    {
        if ( strpos( $hook, 'ausrg' ) === false && strpos( $hook, 'ausgangsrechnung' ) === false ) {
            return;
        }

        // Nur auf der Settings-Seite: Highlight-Style fuer fehlende Pflichtfelder.
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nur zum Screen-Erkennen; kein State-Change.
        $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( (string) $_GET['page'] ) ) : '';
        if ( $page === self::SLUG_SETTINGS ) {
            wp_enqueue_style(
                'ausrg-settings',
                AUSRG_URL . 'admin/assets/settings.css',
                [],
                AUSRG_VERSION
            );
        }
    }
}
