<?php

namespace Fotogenic_AI;

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

final class Rest_History {
    private Service_History $service;

    public function __construct( Service_History $service ) {
        $this->service = $service;
    }

    public function register_routes( Rest_Router $router ): void {
        $router->register_route(
            'fotogenic-ai/v1',
            '/studio/history/runs',
            [
                'methods'             => 'GET',
                'callback'            => [ $this, 'runs' ],
                'permission_callback' => [ $router, 'permissions_generate' ],
                'args'                => [
                    'page'     => [
                        'type'    => 'integer',
                        'default' => 1,
                        'minimum' => 1,
                    ],
                    'perPage' => [
                        'type'    => 'integer',
                        'default' => 12,
                        'minimum' => 1,
                        'maximum' => 48,
                    ],
					'query'    => [
						'type'      => 'string',
						'default'   => '',
						'maxLength' => 200,
					],
					'status'   => [
						'type'    => 'string',
						'default' => 'all',
						'enum'    => [ 'all', 'queued', 'starting', 'processing', 'paused', 'stopping', 'uncertain', 'partial', 'completed', 'failed', 'stopped', 'removed' ],
					],
                ],
            ]
        );
    }

    public function runs( \WP_REST_Request $request ) {
        return $this->service->handle_runs_list( $request );
    }
}
