<?php
declare(strict_types=1);

namespace Itdatex\Rechnungseingang\Admin;

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

use Itdatex\Rechnungseingang\Admin\Pages\Inbox;
use Itdatex\Rechnungseingang\Admin\Pages\Invoice;
use Itdatex\Rechnungseingang\Admin\Pages\Upload;
use Itdatex\Rechnungseingang\Plugin;

/**
 * Registriert das Admin-Menue.
 *
 * Top-Level "Rechnungen" mit Submenues:
 *   - Eingang         (Liste, Slug: itdatex-e-invoice-intake)
 *   - Hochladen       (Upload-Form)
 *   - (versteckt: Einzelansicht via Slug itdatex-e-invoice-intake-invoice)
 */
final class Menu
{
    public const SLUG_INBOX   = 'itdatex-e-invoice-intake';
    public const SLUG_UPLOAD  = 'itdatex-e-invoice-intake-upload';
    public const SLUG_INVOICE = 'itdatex-e-invoice-intake-invoice';

    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(
            __( 'itdatex E-Invoice Intake', 'itdatex-e-invoice-intake' ),
            __( 'Rechnungen', 'itdatex-e-invoice-intake' ),
            Plugin::CAPABILITY,
            self::SLUG_INBOX,
            [ Inbox::class, 'render' ],
            'dashicons-portfolio',
            30
        );

        add_submenu_page(
            self::SLUG_INBOX,
            __( 'Eingang', 'itdatex-e-invoice-intake' ),
            __( 'Eingang', 'itdatex-e-invoice-intake' ),
            Plugin::CAPABILITY,
            self::SLUG_INBOX,
            [ Inbox::class, 'render' ]
        );

        add_submenu_page(
            self::SLUG_INBOX,
            __( 'Hochladen', 'itdatex-e-invoice-intake' ),
            __( 'Hochladen', 'itdatex-e-invoice-intake' ),
            Plugin::CAPABILITY,
            self::SLUG_UPLOAD,
            [ Upload::class, 'render' ]
        );

        // Einzelansicht: nicht im Menue, aber callbacks-erreichbar
        add_submenu_page(
            (string) null,
            __( 'Rechnung', 'itdatex-e-invoice-intake' ),
            __( 'Rechnung', 'itdatex-e-invoice-intake' ),
            Plugin::CAPABILITY,
            self::SLUG_INVOICE,
            [ Invoice::class, 'render' ]
        );
    }

    public function enqueue_assets( string $hook ): void
    {
        // Nur auf unseren Seiten Assets laden
        if ( strpos( $hook, 'itdatex-e-invoice-intake' ) === false ) {
            return;
        }
        wp_enqueue_style(
            'reing-admin',
            REING_URL . 'admin/assets/admin.css',
            [],
            REING_VERSION
        );
    }
}
