<?php

namespace App\Contracts;

use App\Support\Resources\CustomAnonymousResourceCollection;
use App\Support\Resources\CustomPaginatedResourceResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Pagination\AbstractPaginator;

/**
 * Base class for a resource/response
 */
class Resource extends JsonResource
{
    final public function __construct($resource)
    {
        parent::__construct($resource);
    }

    /**
     * Iterate through the list of $fields and check if they're a "Unit"
     * If they are, then add the response
     */
    public function checkUnitFields(array &$response, array $fields): void
    {
        foreach ($fields as $f) {
            $response[$f] = $this->{$f} instanceof Unit ? $this->{$f}->getResponseUnits() : $this->{$f};
        }
    }

    /**
     * Customize the response to exclude all the extra data that isn't used. Based on:
     * https://gist.github.com/derekphilipau/4be52164a69ce487dcd0673656d280da
     *
     * @param  Request      $request
     * @return JsonResponse
     */
    #[\Override]
    public function toResponse($request)
    {
        return $this->resource instanceof AbstractPaginator
                    ? (new CustomPaginatedResourceResponse($this))->toResponse($request)
                    : parent::toResponse($request);
    }

    #[\Override]
    public static function collection($resource)
    {
        return tap(new CustomAnonymousResourceCollection($resource, static::class), function ($collection): void {
            if (property_exists(static::class, 'preserveKeys')) {
                // TODO: figure out what is this preserveKeys thing and whether we still need this
                // @phpstan-ignore-next-line
                $collection->preserveKeys = (new static([]))->preserveKeys === true;
            }
        });
    }
}
