<?php
/**
 * REST controller for plugin/theme ZIP uploads.
 *
 * @package FleekDash\Controllers
 */

namespace FleekDash\Controllers;

use FleekDash\Controllers\Plugins\PackageUploadExecutor;
use FleekDash\Services\ResponseService;
use WP_REST_Request;
use WP_REST_Response;

/**
 * Thin REST entry points for package uploads.
 */
class PackageUploadController
{
    private PackageUploadExecutor $executor;

    public function __construct(?PackageUploadExecutor $executor = null)
    {
        $this->executor = $executor ?? new PackageUploadExecutor();
    }

    public function uploadPlugin(WP_REST_Request $request): WP_REST_Response
    {
        try {
            return $this->executor->uploadPlugin($request);
        } catch (\Throwable $e) {
            return ResponseService::error(
                'Plugin upload failed.',
                ['error' => $e->getMessage()],
                500,
            );
        }
    }

    public function uploadTheme(WP_REST_Request $request): WP_REST_Response
    {
        try {
            return $this->executor->uploadTheme($request);
        } catch (\Throwable $e) {
            return ResponseService::error(
                'Theme upload failed.',
                ['error' => $e->getMessage()],
                500,
            );
        }
    }
}
