<?php

declare(strict_types=1);

namespace Drupal\flowdrop_session\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form controller for the session entity edit forms.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class FlowDropSessionForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   *
   * @return array<string, mixed>
   *   The form array.
   */
  public function form(array $form, FormStateInterface $form_state): array {
    $form = parent::form($form, $form_state);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state): int {
    $result = parent::save($form, $form_state);

    $entity = $this->getEntity();
    $logger_args = [
      '%name' => $entity->label(),
      'link' => $entity->toLink($this->t('View'))->toString(),
    ];

    switch ($result) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('Created new session %name.', $logger_args));
        $this->logger('flowdrop_session')->notice('Created new session %name.', $logger_args);
        break;

      default:
        $this->messenger()->addStatus($this->t('Saved session %name.', $logger_args));
        $this->logger('flowdrop_session')->notice('Saved session %name.', $logger_args);
    }

    $form_state->setRedirectUrl($entity->toUrl('collection'));

    return $result;
  }

}
