<?php
/**
 * WordPress Admin Menu & Settings Controller
 */

class Proto_Preview_Admin {

    protected $db;

    public function __construct(Proto_Preview_DB $db) {
        $this->db = $db;
    }

    public function init() {
        add_action('admin_menu', [$this, 'add_menu_pages']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
    }

    public function add_menu_pages() {
        // Main Menu
        add_menu_page(
            'Proto Preview — Board',
            'Proto Preview',
            'read',
            'proto-preview',
            [$this, 'render_board_page'],
            'dashicons-art',
            30
        );

        // Submenus
        add_submenu_page(
            'proto-preview',
            'Prototype Board',
            'Prototype Board',
            'read',
            'proto-preview',
            [$this, 'render_board_page']
        );

        add_submenu_page(
            'proto-preview',
            'Client Player',
            'Open Player ↗',
            'read',
            'proto-preview-player-link',
            [$this, 'redirect_to_player']
        );
    }

    public function redirect_to_player() {
        wp_redirect(home_url('/?proto_preview=1'));
        exit;
    }

    public function enqueue_assets($hook) {
        if (strpos($hook, 'proto-preview') === false) {
            return;
        }

        // Styles
        wp_enqueue_style('proto-preview-fonts', 'https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,500;0,600;0,700;0,800;1,500;1,600;1,700&family=Radio+Canada:ital,wght@0,400;0,500;0,600;0,700;1,400;1,600&family=JetBrains+Mono:wght@400;500;700&display=swap', [], null);
        wp_enqueue_style('proto-preview-css', PROTO_PREVIEW_PLUGIN_URL . 'assets/css/proto-preview.css', [], PROTO_PREVIEW_VERSION);

        // Tailwind & Lucide
        wp_enqueue_script('tailwind-cdn', 'https://cdn.tailwindcss.com', [], null, false);
        wp_enqueue_script('lucide-cdn', 'https://unpkg.com/lucide@latest', [], null, false);

        // Scripts
        wp_enqueue_script('proto-preview-api', PROTO_PREVIEW_PLUGIN_URL . 'assets/js/proto-preview-api.js', [], PROTO_PREVIEW_VERSION, true);
        wp_enqueue_script('proto-preview-admin-js', PROTO_PREVIEW_PLUGIN_URL . 'assets/js/proto-preview-admin.js', ['proto-preview-api'], PROTO_PREVIEW_VERSION, true);

        wp_localize_script('proto-preview-api', 'protoPreviewSettings', [
            'root' => esc_url_raw(rest_url('proto-preview/v1/')),
            'nonce' => wp_create_nonce('wp_rest'),
            'playerUrl' => home_url('/?proto_preview=1'),
            'currentUser' => [
                'id' => get_current_user_id(),
                'name' => wp_get_current_user()->display_name,
                'role' => current_user_can('manage_options') ? 'designer' : 'client',
                'avatar' => get_avatar_url(get_current_user_id())
            ]
        ]);
    }

    public function render_board_page() {
        require_once PROTO_PREVIEW_PLUGIN_DIR . 'templates/admin-board.php';
    }
}
