<?php

defined("ABSPATH") || exit();

class Chit_Admin
{
    public function __construct()
    {
        require_once CHIT_PATH . "includes/admin/class-chit-admin-generate.php";
        $this->generate = new Chit_Admin_Generate();

        require_once CHIT_PATH . "includes/admin/class-chit-admin-view.php";
        $this->view = new Chit_Admin_View();

        require_once CHIT_PATH . "includes/admin/class-chit-admin-import.php";
        $this->import = new Chit_Admin_Import();

        require_once CHIT_PATH . "includes/admin/class-chit-admin-reports.php";
        $this->reports = new Chit_Admin_Reports();

        require_once CHIT_PATH . "includes/admin/class-chit-admin-settings.php";
        $this->settings = new Chit_Admin_Settings();

        add_action("admin_menu", [$this, "register_menu"]);
    }

    public function register_menu()
    {
        add_menu_page(
            __("Chit", "chit-download-codes-for-woocommerce"),
            __("Chit", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit",
            [$this, "render_dashboard"],
            "dashicons-tickets-alt",
            30,
        );
        add_submenu_page(
            "chit",
            __("Dashboard", "chit-download-codes-for-woocommerce"),
            __("Dashboard", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit",
            [$this, "render_dashboard"],
        );

        add_submenu_page(
            "chit",
            __("Generate", "chit-download-codes-for-woocommerce"),
            __("Generate", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit-generate",
            [$this->generate, "render"],
        );

        add_submenu_page(
            "chit",
            __("View Codes", "chit-download-codes-for-woocommerce"),
            __("View Codes", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit-view",
            [$this->view, "render"],
        );

        add_submenu_page(
            "chit",
            __("Import", "chit-download-codes-for-woocommerce"),
            __("Import", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit-import",
            [$this->import, "render"],
        );

        add_submenu_page(
            "chit",
            __("Reports", "chit-download-codes-for-woocommerce"),
            __("Reports", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit-reports",
            [$this->reports, "render"],
        );

        $settings_hook = add_submenu_page(
            "chit",
            __("Settings", "chit-download-codes-for-woocommerce"),
            __("Settings", "chit-download-codes-for-woocommerce"),
            "manage_woocommerce",
            "chit-settings",
            [$this->settings, "render"],
        );
        if ($settings_hook) {
            add_action("admin_print_scripts-$settings_hook", [
                $this->settings,
                "enqueue_assets",
            ]);
        }
    }

    public static function render_stat_tiles(array $stats): void
    {
        $tiles = [
            "total" => [
                "label" => __("Total", "chit-download-codes-for-woocommerce"),
                "color" => "#1d2327",
            ],
            "unused" => [
                "label" => __("Unused", "chit-download-codes-for-woocommerce"),
                "color" => "#46b450",
            ],
            "activated" => [
                "label" => __("Activated", "chit-download-codes-for-woocommerce"),
                "color" => "#0073aa",
            ],
            "revoked" => [
                "label" => __("Revoked", "chit-download-codes-for-woocommerce"),
                "color" => "#dc3232",
            ],
        ];
        ?>
        <div style="display: flex; gap: 1em; flex-wrap: wrap; margin-top: 1em;">
            <?php foreach ($tiles as $key => $tile): ?>
                <div style="
                    flex: 1 1 160px;
                    min-width: 140px;
                    background: #fff;
                    border: 1px solid #c3c4c7;
                    border-left: 4px solid <?php echo esc_attr(
                        $tile["color"]
                    ); ?>;
                    padding: 1em 1.25em;
                    box-shadow: 0 1px 1px rgba(0,0,0,0.04);
                ">
                    <div style="font-size: 2em; font-weight: 600; line-height: 1.1;
                                color: <?php echo esc_attr(
                                    $tile["color"]
                                ); ?>;">
                        <?php echo esc_html(
                            number_format_i18n((int) ($stats[$key] ?? 0))
                        ); ?>
                    </div>
                    <div style="text-transform: uppercase; font-size: 0.8em;
                                color: #50575e; letter-spacing: 0.04em;
                                margin-top: 0.25em;">
                        <?php echo esc_html($tile["label"]); ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <?php
    }

    public function render_dashboard()
    {
        $stats = Chit_Codes::get_stats();
        $recent = Chit_Codes::get_recent_activations(5);
        ?>
        <div class="wrap">
            <h1 style="display: flex; align-items: center; gap: 1em;">
                <?php esc_html_e("Chit", "chit-download-codes-for-woocommerce"); ?>
                <a href="<?php echo esc_url(
                    admin_url("admin.php?page=chit-generate")
                ); ?>" class="page-title-action">
                    <?php esc_html_e("Generate codes", "chit-download-codes-for-woocommerce"); ?>
                </a>
            </h1>

            <?php self::render_stat_tiles($stats); ?>

            <h2 style="margin-top: 2em;">
                <?php esc_html_e("Recently activated", "chit-download-codes-for-woocommerce"); ?>
            </h2>

            <?php if (empty($recent)): ?>
                <p><?php esc_html_e(
                    "No codes have been redeemed yet.",
                    "chit-download-codes-for-woocommerce"
                ); ?></p>
            <?php else: ?>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php esc_html_e("Code", "chit-download-codes-for-woocommerce"); ?></th>
                            <th><?php esc_html_e("Product", "chit-download-codes-for-woocommerce"); ?></th>
                            <th><?php esc_html_e(
                                "Redeemed by",
                                "chit-download-codes-for-woocommerce"
                            ); ?></th>
                            <th><?php esc_html_e("When", "chit-download-codes-for-woocommerce"); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent as $code):
                            $product = wc_get_product((int) $code["product_id"]);
                            $product_name = $product
                                ? $product->get_name()
                                : __("(deleted product)", "chit-download-codes-for-woocommerce");

                            $user_name = Chit_Codes::activator_label($code);
                            if ($user_name === "") {
                                $user_name = __("(unknown)", "chit-download-codes-for-woocommerce");
                            }
                            ?>
                            <tr>
                                <td><strong><?php echo esc_html(
                                    $code["code"]
                                ); ?></strong></td>
                                <td><?php echo esc_html(
                                    $product_name
                                ); ?></td>
                                <td><?php echo esc_html($user_name); ?></td>
                                <td><?php echo esc_html(
                                    $code["activated_at"]
                                ); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <p style="margin-top: 1em;">
                    <a href="<?php echo esc_url(
                        admin_url(
                            "admin.php?page=chit-view&status=activated"
                        )
                    ); ?>">
                        <?php esc_html_e("View all activated codes →", "chit-download-codes-for-woocommerce"); ?>
                    </a>
                </p>
            <?php endif; ?>
        </div>
        <?php
    }
}
