<?php

namespace Vendtics\App\Http\Controllers\Admin;

defined( "ABSPATH" ) || exit;

use Vendtics\App\Repositories\SettingsRepository;
use Vendtics\App\Http\Controllers\Controller;
use Vendtics\WpMVC\Routing\Response;
use Vendtics\WpMVC\RequestValidator\Validator;
use WP_REST_Request;

class SettingsController extends Controller {
    public SettingsRepository $repository;

    public function __construct( SettingsRepository $repository ) {
        $this->repository = $repository;
    }

    /**
     * Display a listing of the resource.
     *
     * @param Validator $validator Instance of the Validator.
     * @param WP_REST_Request $request The REST request instance.
     * @return array
     */
    public function index( Validator $validator, WP_REST_Request $request ): array {
        return Response::send(
            [
                "settings" => $this->repository->get_settings()
            ]
        );
    }

    /**
     * update settings
     *
     * @param Validator $validator Instance of the Validator.
     * @param WP_REST_Request $request The REST request instance.
     * @return array
     */
    public function update( Validator $validator, WP_REST_Request $request ): array {
        $validator->validate(
            [
                'settings' => 'required|array'
            ]
        );

        $this->repository->save_settings( $request->get_param( 'settings' ) );

        return Response::send(
            [
                "message" => esc_html__( "Settings was updated successfully", 'vendtics' )
            ]
        );
    }
}