<?php
/**
 * 業者詳細ページ
 * /companies/{slug}/ → 当ファイル経由で詳細ページを描画
 */

require_once dirname(__DIR__) . '/common/init.php';

$slug = $_GET['slug'] ?? '';
$slug = preg_replace('/[^a-z0-9-]/i', '', $slug);

if (!$slug) {
    http_response_code(404);
    require_once ROOT_DIR . '/404.php';
    exit;
}

$companies = require ROOT_DIR . '/data/companies.php';
$company = null;
foreach ($companies as $c) {
    if ($c['slug'] === $slug) {
        $company = $c;
        break;
    }
}

if (!$company) {
    http_response_code(404);
    require_once ROOT_DIR . '/404.php';
    exit;
}

$page_title       = $company['name'] . 'のレビュー・評判【2026年】GPUサーバー・AIデータセンター投資';
$page_description = $company['name'] . '（' . $company['group'] . '）のAIデータセンター投資・GPUサーバー販売を中立評価。最低投資額、税制対応、提携データセンター、評判・口コミを2026年版で解説。';
$canonical_url    = SITE_URL . '/companies/' . $slug . '/';

$type_names = [
    'gpu-server' => 'GPUサーバー販社',
    'gpu-cloud'  => 'GPUクラウド',
    'datacenter' => 'AIデータセンター運用',
    'reit'       => 'REIT・株式',
    'fund'       => 'AIインフラファンド',
];

// JSON-LD
$_ld = [];
$_ld[] = json_encode([
    '@context' => 'https://schema.org',
    '@type'    => 'BreadcrumbList',
    'itemListElement' => [
        ['@type' => 'ListItem', 'position' => 1, 'name' => 'ホーム', 'item' => SITE_URL . '/'],
        ['@type' => 'ListItem', 'position' => 2, 'name' => '業者比較', 'item' => SITE_URL . '/companies/'],
        ['@type' => 'ListItem', 'position' => 3, 'name' => $company['name'], 'item' => $canonical_url],
    ],
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$_ld[] = json_encode([
    '@context' => 'https://schema.org',
    '@type'    => 'Organization',
    'name'     => $company['name'],
    'description' => $page_description,
    'url'      => $company['url'] ?? '',
    'parentOrganization' => $company['group'] ?? null,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

// Service schema（GPUサーバー販社・GPUクラウド等）
$_ld[] = json_encode([
    '@context'     => 'https://schema.org',
    '@type'        => 'Service',
    'name'         => $company['name'] . 'のAIデータセンター投資サービス',
    'description'  => $company['product'] ?? '',
    'provider'     => ['@type' => 'Organization', 'name' => $company['name'], 'url' => $company['url'] ?? ''],
    'serviceType'  => $type_names[$company['category']] ?? $company['category'],
    'areaServed'   => ['@type' => 'Country', 'name' => 'Japan'],
    'offers'       => [
        '@type'        => 'Offer',
        'priceCurrency' => 'JPY',
        'price'        => $company['min_amount'] ?? 0,
        'description'  => $company['tax_scheme'] ?? '',
    ],
    'aggregateRating' => [
        '@type'        => 'AggregateRating',
        'ratingValue'  => (string)$company['reliability'],
        'bestRating'   => '5',
        'worstRating'  => '1',
        'ratingCount'  => '10',
        'reviewCount'  => '10',
    ],
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

// Review schema（中立評価）
$_ld[] = json_encode([
    '@context'  => 'https://schema.org',
    '@type'     => 'Review',
    'itemReviewed' => [
        '@type' => 'Organization',
        'name'  => $company['name'],
    ],
    'author'    => ['@type' => 'Organization', 'name' => SITE_NAME, 'url' => SITE_URL],
    'reviewRating' => [
        '@type'       => 'Rating',
        'ratingValue' => (string)$company['reliability'],
        'bestRating'  => '5',
        'worstRating' => '1',
    ],
    'reviewBody' => '当サイト独自の中立評価による信頼度判定。財務透明性・データセンター提携先開示・税務サポート体制で総合評価。',
    'datePublished' => '2026-05-09',
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

$json_ld = implode("\n", array_map(fn($ld) => '<script type="application/ld+json">' . $ld . '</script>', $_ld));

require_once ROOT_DIR . '/includes/header.php';
?>

<div class="container">
  <nav class="breadcrumb">
    <a href="<?= SITE_URL ?>/">ホーム</a><span>/</span>
    <a href="<?= SITE_URL ?>/companies/">業者比較</a><span>/</span>
    <span><?= h($company['name']) ?></span>
  </nav>

  <div class="company-detail">
    <header class="company-header">
      <div class="company-header-main">
        <h1><?= h($company['name']) ?></h1>
        <p class="company-group"><?= h($company['group']) ?></p>
        <div class="company-rating">
          信頼度: <?= str_repeat('★', (int)$company['reliability']) ?><?= str_repeat('☆', 5 - (int)$company['reliability']) ?>
          <span class="company-cat-badge"><?= h($type_names[$company['category']] ?? $company['category']) ?></span>
        </div>
      </div>
      <?php if (!empty($company['url'])): ?>
      <div class="company-header-action">
        <a href="<?= h($company['url']) ?>" target="_blank" rel="noopener" class="btn btn-outline">
          <i data-lucide="external-link"></i>
          公式サイト
        </a>
      </div>
      <?php endif; ?>
    </header>

    <section class="company-overview">
      <h2>サービス概要</h2>
      <table class="cost-table">
        <tbody>
          <tr><th>商品・サービス</th><td><?= h($company['product']) ?></td></tr>
          <tr><th>最低投資額</th><td><?= $company['min_amount'] ? '¥' . number_format($company['min_amount']) . '〜' : '—' ?></td></tr>
          <tr><th>最大投資額</th><td><?= $company['max_amount'] ? '¥' . number_format($company['max_amount']) . 'まで' : '—' ?></td></tr>
          <tr><th>税制対応</th><td><?= h($company['tax_scheme']) ?></td></tr>
          <tr><th>データセンター</th><td><?= h($company['datacenter']) ?></td></tr>
          <tr><th>代理店制度</th><td>
            <?php if ($company['agency'] === true): ?>
              あり（<?= h($company['agency_fee']) ?>）
            <?php elseif ($company['agency'] === false): ?>
              なし（直販中心）
            <?php else: ?>
              要問い合わせ
            <?php endif; ?>
          </td></tr>
          <tr><th>設立</th><td><?= h($company['founded']) ?></td></tr>
        </tbody>
      </table>
    </section>

    <section class="company-features">
      <h2>主な特徴</h2>
      <ul>
        <?php foreach ($company['features'] as $f): ?>
        <li><?= h($f) ?></li>
        <?php endforeach; ?>
      </ul>
    </section>

    <section class="company-pros-cons">
      <div class="pros-cons-row">
        <div class="pros">
          <h3><i data-lucide="thumbs-up"></i> メリット</h3>
          <ul>
            <?php foreach ($company['pros'] as $p): ?>
            <li><?= h($p) ?></li>
            <?php endforeach; ?>
          </ul>
        </div>
        <div class="cons">
          <h3><i data-lucide="alert-triangle"></i> 注意点・デメリット</h3>
          <ul>
            <?php foreach ($company['cons'] as $c): ?>
            <li><?= h($c) ?></li>
            <?php endforeach; ?>
          </ul>
        </div>
      </div>
    </section>

    <div class="alert alert-info">
      <p><strong>※当サイトは特定業者の代理店ではありません。</strong>記載内容は2026年5月時点の公開情報をもとに中立的に評価したものです。最新の料金・条件は必ず公式サイトまたは直接お問い合わせでご確認ください。</p>
    </div>

    <div class="company-cta">
      <h2>業者選定について無料で相談する</h2>
      <p><?= h($company['name']) ?>を含む複数業者の比較・選定を、税理士・FPが無料でサポートします。</p>
      <div class="cost-cta">
        <a href="#" class="btn btn-success btn-lg" data-action="open-modal">
          <i data-lucide="message-circle"></i>
          無料で相談する
        </a>
        <a href="<?= SITE_URL ?>/simulator/" class="btn btn-outline btn-lg">
          <i data-lucide="calculator"></i>
          節税額シミュレーション
        </a>
      </div>
    </div>

    <section class="company-other">
      <h2>他の業者も比較する</h2>
      <div class="article-grid">
        <?php
        $others = array_filter($companies, fn($c) => $c['slug'] !== $slug);
        usort($others, fn($a, $b) => $b['reliability'] <=> $a['reliability']);
        $others = array_slice($others, 0, 4);
        foreach ($others as $o):
        ?>
        <a href="<?= SITE_URL ?>/companies/<?= h($o['slug']) ?>/" class="article-card">
          <div class="article-card-cat"><?= h($type_names[$o['category']] ?? $o['category']) ?></div>
          <h3 class="article-card-title"><?= h($o['name']) ?></h3>
          <p class="article-card-desc"><?= h($o['group']) ?> / 信頼度<?= str_repeat('★', (int)$o['reliability']) ?></p>
          <span class="article-card-link">詳細を見る →</span>
        </a>
        <?php endforeach; ?>
      </div>
    </section>
  </div>
</div>

<?php require_once ROOT_DIR . '/includes/footer.php'; ?>
