<?php

namespace App\Controllers;

use App\Libraries\TemporaryImageUpload;
use RuntimeException;

class TempUploadController extends BaseController
{
    private const UPLOAD_TYPES = [
        'settings-logo' => [
            'extensions' => ['jpg', 'jpeg', 'png', 'webp'],
            'administrator' => true,
        ],
        'settings-favicon' => [
            'extensions' => ['ico', 'jpg', 'jpeg', 'png', 'webp'],
            'administrator' => true,
        ],
        'user-photo' => [
            'extensions' => ['jpg', 'jpeg', 'png', 'webp'],
            'administrator' => false,
        ],
        'barang-photo' => [
            'extensions' => ['jpg', 'jpeg', 'png', 'webp'],
            'administrator' => true,
        ],
        'teknisi-photo' => [
            'extensions' => ['jpg', 'jpeg', 'png', 'webp'],
            'administrator' => true,
        ],
    ];

    public function store(string $type)
    {
        if (! session()->get('isLoggedIn')) {
            return $this->response->setStatusCode(401)->setJSON([
                'message' => 'Sesi login telah berakhir.',
            ]);
        }

        $configuration = self::UPLOAD_TYPES[$type] ?? null;

        if ($configuration === null) {
            return $this->response->setStatusCode(404)->setJSON([
                'message' => 'Tipe upload tidak ditemukan.',
            ]);
        }

        if ($configuration['administrator'] && ! $this->isAdministratorRole()) {
            return $this->response->setStatusCode(403)->setJSON([
                'message' => 'Anda tidak memiliki akses untuk upload ini.',
            ]);
        }

        if ($type === 'user-photo' && ! $this->isUserAccount()) {
            return $this->response->setStatusCode(403)->setJSON([
                'message' => 'Upload foto user hanya tersedia pada area admin.',
            ]);
        }

        if (isset($configuration['account_type']) && $this->currentAccountType() !== $configuration['account_type']) {
            return $this->response->setStatusCode(403)->setJSON([
                'message' => 'Anda tidak memiliki akses untuk upload ini.',
            ]);
        }

        $extensions = implode(',', $configuration['extensions']);
        $rules = 'uploaded[upload]|max_size[upload,' . ($configuration['maximum_size'] ?? 2048) . ']|ext_in[upload,' . $extensions . ']';

        if ($configuration['image_only'] ?? true) {
            $rules .= '|is_image[upload]';
        }

        if (! $this->validate(['upload' => $rules])) {
            return $this->response->setStatusCode(422)->setJSON([
                'message' => implode(' ', $this->validator->getErrors()),
            ]);
        }

        try {
            $fileName = (new TemporaryImageUpload())->store(
                $this->request->getFile('upload'),
                $configuration['extensions']
            );
        } catch (RuntimeException $exception) {
            return $this->response->setStatusCode(422)->setJSON([
                'message' => $exception->getMessage(),
            ]);
        }

        return $this->response->setJSON([
            'token' => $fileName,
        ]);
    }
}
