<?php

use Phalcon\Events\Event;
use Phalcon\Mvc\Micro;
use Phalcon\Logger\Formatter\Line as LineFormatter;

/**
 * LoggingMiddleware
 *
 * Processes the 404s
 */
class AttachmentHelper
{
    public static function delete($attachment, $soft_delete = true, $minio = null, $directory = '')
    {
        if (is_null($attachment) || empty($attachment)) {

            return false;
        }

        if (!$soft_delete) {
            if (getenv("FILESYSTEM_CLOUD") == FileSystemCloud::LOCAL) {
                $filename = self::getLocalPath($attachment, $directory);

                if (file_exists($filename)) {
                    unlink($filename);
                }
            } elseif (getenv("FILESYSTEM_CLOUD") == FileSystemCloud::MINIO) {
                $minio->delete($attachment->uuid, $attachment->tipe, $directory);
            }

            return $attachment->delete();
        } else {
            $now = date('Y-m-d H:i:s');
            $attachment->updated_at = $now;
            $attachment->deleted_at = $now;

            return $attachment->save();
        }
    }

    public static function deletion(Attachments $attachment, $soft_delete, MinioService $minio, $directory)
    {
        if ($soft_delete) {
            $now = date('Y-m-d H:i:s');
            $attachment->updated_at = $now;
            $attachment->deleted_at = $now;

            return $attachment->save();
        } else {
            if (getenv("FILESYSTEM_CLOUD") == FileSystemCloud::LOCAL) {
                $filename = self::getLocalPath($attachment, $directory);

                if (file_exists($filename)) {
                    unlink($filename);
                }
            } elseif (getenv("FILESYSTEM_CLOUD") == FileSystemCloud::MINIO) {
                $key = $directory . DIRECTORY_SEPARATOR . $attachment->uuid;
                $minio->deletion($key, $attachment->tipe);
            }

            return $attachment->delete();
        }
    }

    public static function restore($attachment)
    {
        $now = date('Y-m-d H:i:s');
        $attachment->updated_at = $now;
        $attachment->deleted_at = null;

        return $attachment->save();
    }

    public static function getLocalPath($attachment, $directory = '')
    {
        $root = $attachment->tipe == Attachments::TIPE_PUBLIC
            ? getenv("USER_IMAGE_FOLDER") . DIRECTORY_SEPARATOR
            : getenv("PRIVATE_FOLDER") . DIRECTORY_SEPARATOR . 'diskusi_attachment' . DIRECTORY_SEPARATOR;

        $key = $directory . DIRECTORY_SEPARATOR . $attachment->uuid;

        return $root . DIRECTORY_SEPARATOR . $key;
    }

    public static function getMcpLocalPath($attachment, $directory = '')
    {
        $root = $attachment->tipe == McpAttachments::TIPE_PUBLIC
            ? getenv("USER_IMAGE_FOLDER") . DIRECTORY_SEPARATOR
            : getenv("PRIVATE_FOLDER") . DIRECTORY_SEPARATOR . 'diskusi_attachment' . DIRECTORY_SEPARATOR;

        $key = $directory . DIRECTORY_SEPARATOR . $attachment->uuid;
        
        return $root . DIRECTORY_SEPARATOR . $key;
    }

    public static function getLocalPublicPath($filename)
    {
        return getenv("USER_IMAGE_FOLDER") . DIRECTORY_SEPARATOR . $filename;
    }
}
