<?php

namespace AtlasAiDev\Awfah\REST;

use AtlasAiDev\Awfah\Abilities\Content_Steward_Abilities;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;

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

class Content_Steward_Controller extends WP_REST_Controller {

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

    public function register_routes(): void {
        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . '/analyze',
            [
                [
                    'methods'             => 'POST',
                    'callback'            => [ $this, 'analyze_post' ],
                    'permission_callback' => [ $this, 'permissions_check' ],
                ],
            ]
        );

        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . '/suggest-titles',
            [
                [
                    'methods'             => 'POST',
                    'callback'            => [ $this, 'suggest_titles' ],
                    'permission_callback' => [ $this, 'permissions_check' ],
                ],
            ]
        );

        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . '/run-workflow',
            [
                [
                    'methods'             => 'POST',
                    'callback'            => [ $this, 'run_workflow' ],
                    'permission_callback' => [ $this, 'permissions_check' ],
                ],
            ]
        );
    }

    public function permissions_check(): bool {
        return current_user_can( 'edit_posts' );
    }

    public function analyze_post( WP_REST_Request $request ) {
        $params   = $request->get_json_params();
        $post_id  = isset( $params['post_id'] ) ? (int) $params['post_id'] : 0;
        $provider = isset( $params['provider'] ) ? (string) $params['provider'] : '';

        if ( ! $post_id ) {
            return new WP_Error(
                'awfah_missing_post_id',
                __( 'Missing post_id.', 'ai-workflow-automation-ai-agent-hub' ),
                [ 'status' => 400 ]
            );
        }

        $result = Content_Steward_Abilities::execute_analyze_post(
            [
                'post_id'  => $post_id,
                'provider' => $provider,
            ]
        );

        return new WP_REST_Response( $result );
    }

    public function suggest_titles( WP_REST_Request $request ) {
        $params   = $request->get_json_params();
        $post_id  = isset( $params['post_id'] ) ? (int) $params['post_id'] : 0;
        $text     = isset( $params['text'] ) ? (string) $params['text'] : '';
        $provider = isset( $params['provider'] ) ? (string) $params['provider'] : '';

        if ( ! $post_id && '' === $text ) {
            return new WP_Error(
                'awfah_missing_input',
                __( 'Provide either post_id or text to suggest titles.', 'ai-workflow-automation-ai-agent-hub' ),
                [ 'status' => 400 ]
            );
        }

        $result = Content_Steward_Abilities::execute_suggest_titles(
            [
                'post_id'  => $post_id,
                'text'     => $text,
                'provider' => $provider,
            ]
        );

        return new WP_REST_Response( $result );
    }

    /**
     * Backward-compatible delegate — forwards to Workflows_Controller::run_workflow().
     */
    public function run_workflow( WP_REST_Request $request ) {
        $controller = new Workflows_Controller();
        return $controller->run_workflow( $request );
    }

}
