<?php

declare(strict_types=1);

namespace BadamSoft\WooProductExporter\Settings;

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

use BadamSoft\WooProductExporter\Access\AccessManager;
use BadamSoft\WooProductExporter\Core\Plugin;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

use function __;
use function current_user_can;
use function is_array;
use function rest_ensure_response;
use function wp_unslash;

class SettingsRestController {
    private SettingsRepository $repository;
    private AccessManager $access_manager;
    private Plugin $plugin;

    public function __construct( SettingsRepository $repository, AccessManager $access_manager, Plugin $plugin ) {
        $this->repository      = $repository;
        $this->access_manager  = $access_manager;
        $this->plugin          = $plugin;

        add_action( 'rest_api_init', [ $this, 'register_routes' ] );
    }

    public function register_routes(): void {
        register_rest_route(
            'wc-pce/v1',
            '/settings',
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_settings' ],
                'permission_callback' => [ $this, 'can_manage_settings' ],
            ]
        );

        register_rest_route(
            'wc-pce/v1',
            '/settings',
            [
                'methods'             => WP_REST_Server::CREATABLE,
                'callback'            => [ $this, 'save_settings' ],
                'permission_callback' => [ $this, 'can_manage_settings' ],
                'args'                => [
                    'settings' => [
                        'required' => true,
                        'type'     => 'object',
                    ],
                ],
            ]
        );

        register_rest_route(
            'wc-pce/v1',
            '/settings/cleanup',
            [
                'methods'             => WP_REST_Server::CREATABLE,
                'callback'            => [ $this, 'cleanup_now' ],
                'permission_callback' => [ $this, 'can_manage_settings' ],
            ]
        );
    }

    public function can_manage_settings(): bool {
        // Restrict settings management to administrators.
        return current_user_can( 'manage_options' );
    }

    public function get_settings( WP_REST_Request $request ): WP_REST_Response {
        $state = $this->repository->get_state();

        return rest_ensure_response( [ 'settings' => $state ] );
    }

    public function save_settings( WP_REST_Request $request ): WP_REST_Response {
        $raw = $request->get_param( 'settings' );
        $settings = is_array( $raw ) ? wp_unslash( $raw ) : [];

        $saved = $this->repository->save_state( $settings );

        return rest_ensure_response( [ 'settings' => $saved ] );
    }

    public function cleanup_now(): WP_REST_Response {
        $this->plugin->maybe_cleanup_exports();

        return rest_ensure_response(
            [
                'status'  => 'ok',
                'message' => __( 'Cleanup triggered.', 'badamsoft-product-exporter-for-woocommerce' ),
            ]
        );
    }
}
