<?php
namespace FlysystemOffload\Filesystem\Adapters;

use Aws\S3\S3Client;
use FlysystemOffload\Filesystem\AdapterInterface;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\FilesystemAdapter;
use WP_Error;

class S3Adapter implements AdapterInterface
{
    public function validate(array $settings)
    {
        $required = ['access_key', 'secret_key', 'region', 'bucket'];

        foreach ($required as $field) {
            if (empty($settings[$field])) {
                return new WP_Error('flysystem_offload_invalid_s3', sprintf(__('Campo S3 faltante: %s', 'flysystem-offload'), $field));
            }
        }

        return true;
    }

    public function create(array $settings): FilesystemAdapter|WP_Error
    {
        try {
            $clientConfig = [
                'credentials' => [
                    'key' => $settings['access_key'],
                    'secret' => $settings['secret_key']
                ],
                'region' => $settings['region'],
                'version' => 'latest'
            ];

            if (!empty($settings['endpoint'])) {
                $clientConfig['endpoint'] = $settings['endpoint'];
                $clientConfig['use_path_style_endpoint'] = true;
            }

            $client = new S3Client($clientConfig);

            return new AwsS3V3Adapter($client, $settings['bucket'], $settings['prefix'] ?? '');
        } catch (\Throwable $e) {
            return new WP_Error('flysystem_offload_s3_error', $e->getMessage());
        }
    }

    public function publicBaseUrl(array $settings): string
    {
        if (!empty($settings['cdn_url'])) {
            return rtrim($settings['cdn_url'], '/');
        }

        $bucket = $settings['bucket'];
        $region = $settings['region'];

        if ($region === 'us-east-1') {
            return "https://{$bucket}.s3.amazonaws.com";
        }

        return "https://{$bucket}.s3.{$region}.amazonaws.com";
    }
}
