<?php

/**
 * Audit Export REST Controller
 *
 * Handles PDF/CSV export and AI fix suggestions via server API
 *
 * @package ProRank\SEO\Core\RestApi
 * @since   1.0.0
 */

declare(strict_types=1);

namespace ProRank\SEO\Core\RestApi;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

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


/**
 * AuditExportController class
 */


class AuditExportController {

    private const NAMESPACE = 'prorank-seo/v1';

    /**
     * Register routes
     */
    public function register_routes(): void {
        register_rest_route(self::NAMESPACE, '/audit/export/pdf/(?P<audit_id>[a-zA-Z0-9-]+)', [
            'methods' => 'POST',
            'callback' => [$this, 'export_pdf'],
            'permission_callback' => [$this, 'check_permission'],
            'args' => [
                'audit_id' => [
                    'required' => true,
                    'type' => 'string',
                    'sanitize_callback' => 'sanitize_text_field',
                ],
                'include_ignored' => [
                    'type' => 'boolean',
                    'default' => false,
                ],
            ],
        ]);

        register_rest_route(self::NAMESPACE, '/audit/export/csv/(?P<audit_id>[a-zA-Z0-9-]+)', [
            'methods' => 'POST',
            'callback' => [$this, 'export_csv'],
            'permission_callback' => [$this, 'check_permission'],
            'args' => [
                'audit_id' => [
                    'required' => true,
                    'type' => 'string',
                    'sanitize_callback' => 'sanitize_text_field',
                ],
                'export_type' => [
                    'type' => 'string',
                    'default' => 'issues',
                    'enum' => ['issues', 'urls'],
                ],
            ],
        ]);

        register_rest_route(self::NAMESPACE, '/audit/ai-fix', [
            'methods' => 'POST',
            'callback' => [$this, 'get_ai_suggestions'],
            'permission_callback' => [$this, 'check_permission'],
            'args' => [
                'issues' => [
                    'required' => true,
                    'type' => 'array',
                ],
            ],
        ]);
    }

    /**
     * Check permission
     */
    public function check_permission(): bool {
        return current_user_can('manage_options');
    }

    /**
     * Export PDF report
     */
    public function export_pdf(WP_REST_Request $request): WP_REST_Response|WP_Error {
        return new WP_Error(
            'free_package_only',
            __('Audit PDF export is not available in the free plugin.', 'prorank-seo'),
            ['status' => 403]
        );
    }

    /**
     * Export CSV
     */
    public function export_csv(WP_REST_Request $request): WP_REST_Response|WP_Error {
        return new WP_Error(
            'free_package_only',
            __('Audit CSV export is not available in the free plugin.', 'prorank-seo'),
            ['status' => 403]
        );
    }

    /**
     * Get AI fix suggestions
     */
    public function get_ai_suggestions(WP_REST_Request $request): WP_REST_Response|WP_Error {
        return new WP_Error(
            'free_package_only',
            __('AI fix suggestions are not available in the free plugin.', 'prorank-seo'),
            ['status' => 403]
        );
    }
}
