<?php
// app/controllers/layout.php

declare(strict_types=1);

use ScMedia\Http\Response;
use ScMedia\Http\Router;

final class LayoutController extends BaseController {
  public static function register(Router $router, Container $container): void {
    $self = new self($container);
    $router->add('POST', '/api/layout/preview', fn() => $self->preview());
  }

  private function preview(): void {
    $logger = $this->logger();
    $body = read_json_body();
    $all = $this->settings()->getAll();

    $kind = (string)($body['kind'] ?? 'movies');
    $mode = (string)($body['mode'] ?? 'from_items');
    $limit = (int)($body['limit'] ?? 10);

    $data = $this->services()['layout']->preview(
      $all['data'] ?? $all,
      $kind === 'series' ? 'series' : 'movies',
      $mode === 'samples' ? 'samples' : 'from_items',
      $limit
    );

    $logger->log('info', 'Layout preview generated', ['kind' => $kind, 'mode' => $mode, 'limit' => $limit]);
    Response::json(['ok' => true, 'data' => $data]);
  }
}
