<?php

namespace App\Controllers\Api;

use CodeIgniter\HTTP\ResponseInterface;

class Branding extends BaseApiController
{
    public function logo(): ResponseInterface
    {
        $companyName = (string) setting('company_name', 'Rewarder');
        $ext = strtolower((string) setting('company_logo_type', ''));
        $allowedExt = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'];

        $logoUrl = '';
        $updatedAt = null;

        if ($ext !== '' && in_array($ext, $allowedExt, true)) {
            $rel = 'company/logo.' . $ext;
            $abs = FCPATH . 'uploads' . DIRECTORY_SEPARATOR . 'company' . DIRECTORY_SEPARATOR . 'logo.' . $ext;
            if (is_file($abs)) {
                $logoUrl = urlUpload($rel, false);
                $ts = @filemtime($abs);
                if ($ts) {
                    $logoUrl .= '?v=' . $ts;
                    $updatedAt = gmdate('c', (int) $ts);
                }
            }
        }

        return $this->ok((object) [
            'company_name' => $companyName,
            'logo_url' => $logoUrl,
            'logo_updated_at' => $updatedAt,
        ]);
    }
}

