<?php

namespace classes;

use interfaces\minio_uploads_i;
use traits\minio_t;

class upload_store implements minio_uploads_i
{
    use minio_t;

    public function __construct()
    {
        self::setBucket('uploads'); // Change the bucket to 'pdfs'
    }

    /**
     * @inheritDoc
     */
    public function generatePresignedUrl(string $objectName, int $expiry = 3600): string
    {
        // Generate a presigned URL for the given object name with the specified expiry time
        return self::getPresignedUrl($objectName, $expiry, true);
    }

    /**
     * @inheritDoc
     */
    public function isValidFileName(string $fileName): bool
    {
        // Check if the file name is valid
        return preg_match('/^[a-zA-Z0-9_\-.]+$/', $fileName) === 1;
    }

    /**
     * @inheritDoc
     */
    public function isValidFilePath(string $filePath): bool
    {
        // Check if the file path is valid
        return preg_match('/^[a-zA-Z0-9_\-\/.]+$/', $filePath) === 1;
    }

    /**
     * @inheritDoc
     */
    public function requireValidFileName(string $fileName): void
    {
        if (!$this->isValidFileName($fileName)) {
            throw new \InvalidArgumentException("Invalid file name: $fileName");
        }
    }

    /**
     * @inheritDoc
     */
    public function requireValidFilePath(string $filePath): void
    {
        if (!$this->isValidFilePath($filePath)) {
            throw new \InvalidArgumentException("Invalid file path: $filePath");
        }
    }

    public function isFileInStore(string $fileName): bool
    {
        // Check if the file exists in the store
        return self::doesObjectExist($fileName);
    }

    public function download(string $file): string
    {
        $path = '/tmp/' . $file;
        $result = self::getS3Client()->getObject([
            'Bucket' => self::getBucket(),
            'Key' => $file,
            'SaveAs' => $path
        ]);
        return $path;
    }

    public function generateDirectDownloadUrl(string $fileName): string
    {
        // Generate a direct download URL for the given file name
        return 'https://api.truckwash.dk:4433/files/' . $fileName;
    }
}