<?php

namespace TSNV_SEO;

class GameSchema extends AbstractSchema {
  private ?string $name;
  private ?float $ratingValue;
  private ?float $bestRating;
  private ?int $ratingCount;

  public function __construct(array $data = []) {
    parent::__construct();

    $this->name = trim((string) ($data['name'] ?? '')) ?: null;
    $this->ratingValue = is_numeric($data['rating_value'] ?? null) ? (float) $data['rating_value'] : null;
    $this->bestRating = is_numeric($data['best_rating'] ?? null) ? (float) $data['best_rating'] : null;
    $this->ratingCount = is_numeric($data['rating_count'] ?? null) ? (int) $data['rating_count'] : null;
  }

  public function isValid(): bool {
    return $this->ratingValue !== null && $this->ratingValue > 0 &&
           $this->bestRating !== null && $this->bestRating > 0 &&
           $this->ratingCount !== null && $this->ratingCount > 0;
  }

  protected function getJsonLd(): array {
    return [
      '@context' => 'https://schema.org',
      '@type' => 'Game',
      'name' => $this->name,
      'aggregateRating' => [
        '@type' => 'AggregateRating',
        'ratingValue' => $this->ratingValue,
        'bestRating' => $this->bestRating,
        'ratingCount' => $this->ratingCount,
      ],
      'url' => $this->url,
    ];
  }
}
