<?php

use MintyPHP\Http\ApiAuth;
use MintyPHP\Http\ApiBootstrap;
use MintyPHP\Http\ApiResponse;

ApiBootstrap::init();
$request = requestInput();

$uuid = trim((string) ($id ?? ''));
if ($uuid === '') {
    ApiResponse::notFound();
}

$method = $request->method();

if ($method === 'GET') {
    ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_TENANTS_VIEW);

    $tenant = app(\MintyPHP\Service\Directory\DirectoryServicesFactory::class)->createTenantService()->findByUuid($uuid);
    if (!$tenant) {
        ApiResponse::notFound();
    }

    $tenantId = (int) ($tenant['id'] ?? 0);
    ApiAuth::requireResourceAccess('tenants', $tenantId);
    $allowUserThemeRaw = $tenant['allow_user_theme'] ?? null;
    $allowUserThemeMode = '';
    if ($allowUserThemeRaw !== null && $allowUserThemeRaw !== '') {
        $allowUserThemeMode = ((int) $allowUserThemeRaw) === 1 ? '1' : '0';
    }
    $primaryColor = (string) ($tenant['primary_color'] ?? '');

    ApiResponse::success([
        'data' => [
            'uuid' => $tenant['uuid'] ?? '',
            'description' => $tenant['description'] ?? '',
            'status' => $tenant['status'] ?? '',
            'address' => $tenant['address'] ?? '',
            'postal_code' => $tenant['postal_code'] ?? '',
            'city' => $tenant['city'] ?? '',
            'country' => $tenant['country'] ?? '',
            'region' => $tenant['region'] ?? '',
            'vat_id' => $tenant['vat_id'] ?? '',
            'tax_number' => $tenant['tax_number'] ?? '',
            'phone' => $tenant['phone'] ?? '',
            'fax' => $tenant['fax'] ?? '',
            'email' => $tenant['email'] ?? '',
            'support_email' => $tenant['support_email'] ?? '',
            'support_phone' => $tenant['support_phone'] ?? '',
            'billing_email' => $tenant['billing_email'] ?? '',
            'website' => $tenant['website'] ?? '',
            'privacy_url' => $tenant['privacy_url'] ?? '',
            'imprint_url' => $tenant['imprint_url'] ?? '',
            'primary_color' => $primaryColor,
            'primary_color_use_default' => $primaryColor === '',
            'default_theme' => $tenant['default_theme'] ?? '',
            'allow_user_theme_mode' => $allowUserThemeMode,
            'created' => $tenant['created'] ?? '',
            'modified' => $tenant['modified'] ?? '',
        ],
    ]);
}

if ($method === 'PUT' || $method === 'PATCH') {
    ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_TENANTS_UPDATE);

    $tenant = app(\MintyPHP\Service\Directory\DirectoryServicesFactory::class)->createTenantService()->findByUuid($uuid);
    if (!$tenant) {
        ApiResponse::notFound();
    }

    $tenantId = (int) ($tenant['id'] ?? 0);
    ApiAuth::requireResourceAccess('tenants', $tenantId);

    $input = $request->bodyAll();
    $result = app(\MintyPHP\Service\Directory\DirectoryServicesFactory::class)->createTenantService()->updateFromAdmin($tenantId, $input, ApiAuth::userId());
    ApiResponse::fromServiceResult($result);
}

if ($method === 'DELETE') {
    ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_TENANTS_DELETE);

    $tenant = app(\MintyPHP\Service\Directory\DirectoryServicesFactory::class)->createTenantService()->findByUuid($uuid);
    if (!$tenant) {
        ApiResponse::notFound();
    }

    $tenantId = (int) ($tenant['id'] ?? 0);
    ApiAuth::requireResourceAccess('tenants', $tenantId);

    $result = app(\MintyPHP\Service\Directory\DirectoryServicesFactory::class)->createTenantService()->deleteByUuid($uuid);
    ApiResponse::fromServiceResult($result);
}

ApiResponse::methodNotAllowed();
