<?php
/**
 * Copyright (C) 2026 Plugixa (https://plugixa.com)
 * License: GNU General Public License v2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */

namespace PlugixaRecipe\Api;

defined( 'ABSPATH' ) || exit;

use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

/**
 * Base REST Controller
 */
abstract class RestController extends WP_REST_Controller {

    public function __construct() {
        $this->namespace = 'plugixarecipe/v1';
    }

    public function check_permission($request) {
        if (!current_user_can('edit_posts')) {
            return new WP_Error(
                'rest_forbidden',
                __('You do not have permission to perform this action.', 'plugixarecipe'),
                ['status' => 403]
            );
        }
        return true;
    }

    public function check_admin_permission($request) {
        if (!current_user_can('manage_options')) {
            return new WP_Error(
                'rest_forbidden',
                __('Administrator access required.', 'plugixarecipe'),
                ['status' => 403]
            );
        }
        return true;
    }

    protected function success($data, $message = '') {
        return new WP_REST_Response([
            'success' => true,
            'message' => $message,
            'data'    => $data,
        ], 200);
    }

    protected function error($message, $status = 400, $errors = []) {
        return new WP_Error(
            'rest_error',
            $message,
            [
                'status' => $status,
                'errors' => $errors,
            ]
        );
    }

    protected function get_pagination($request) {
        $page = (int) ($request->get_param('page') ?? 1);
        $per_page = (int) ($request->get_param('per_page') ?? $request->get_param('limit') ?? 20);
        $limit = $per_page <= 0 ? 999999 : $per_page;
        $offset = (int) ($request->get_param('offset') ?? (($page - 1) * $limit));
        return [
            'limit'  => $limit,
            'offset' => $offset,
        ];
    }

    protected function get_filters($request) {
        $filters = $request->get_param('filter');

        if (!is_array($filters)) {
            $filters = [];
        }

        if (empty($filters['search']) && $request->get_param('search')) {
            $filters['search'] = $request->get_param('search');
        }
        if (empty($filters['status']) && $request->get_param('status')) {
            $filters['status'] = $request->get_param('status');
        }
        if (empty($filters['category_id']) && $request->get_param('category_id')) {
            $filters['category_id'] = $request->get_param('category_id');
        }

        return $filters;
    }

    protected function get_sort($request) {
        return [
            'orderby' => $request->get_param('sort_by') ?? $request->get_param('orderby') ?? 'created_at',
            'order'   => strtoupper($request->get_param('sort_order') ?? $request->get_param('order') ?? 'DESC'),
        ];
    }

    public function get_collection_params() {
        return [
            'limit' => [
                'type'              => 'integer',
                'default'           => 20,
                'minimum'           => -1,
                'sanitize_callback' => function($value) {
                    return (int) $value;
                },
            ],
            'offset' => [
                'type'              => 'integer',
                'default'           => 0,
                'minimum'           => 0,
                'sanitize_callback' => 'absint',
            ],
            'orderby' => [
                'type'    => 'string',
                'default' => 'created_at',
            ],
            'order' => [
                'type'    => 'string',
                'default' => 'DESC',
                'enum'    => ['ASC', 'DESC'],
            ],
            'filter' => [
                'type'    => 'object',
                'default' => [],
            ],
        ];
    }
}
