<?php

namespace Gaco\SnapBi\Services;

use Gaco\SnapBi\Contracts\ConfigInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Date;

/**
 * Header Utilities
 * 
 * Provides methods for creating and verifying response headers
 */
class Header
{
    protected ?ConfigInterface $config = null;

    public function __construct(?ConfigInterface $config = null)
    {
        $this->config = $config;
    }

    /**
     * Create response headers
     * 
     * Adds X-TIMESTAMP header to response if not present
     * 
     * @param JsonResponse $response
     * @return JsonResponse
     */
    public function create(JsonResponse $response): JsonResponse
    {
        if (!$response->headers->has('X-TIMESTAMP')) {
            $timestamp = Date::now()->utc()->format('Y-m-d\TH:i:s.000\Z');
            $response->headers->set('X-TIMESTAMP', $timestamp);
        }

        return $response;
    }

    /**
     * Verify request headers
     * 
     * Validates required headers in request
     * 
     * @param \Illuminate\Http\Request $request
     * @param array $requiredHeaders Required header names
     * @return bool True if all required headers are present
     */
    public function verify(\Illuminate\Http\Request $request, array $requiredHeaders = []): bool
    {
        if (empty($requiredHeaders)) {
            // Default required headers
            $requiredHeaders = ['X-TIMESTAMP', 'X-CLIENT-KEY'];
        }

        foreach ($requiredHeaders as $header) {
            if (!$request->hasHeader($header)) {
                return false;
            }
        }

        return true;
    }
}

