<?php
/**
 * @package Hafenstudios\Leadlotse
 */

namespace Hafenstudios\Leadlotse\Events;

use Hafenstudios\Leadlotse\Db\EntryRepository;

defined( 'ABSPATH' ) || exit;

/**
 * Teilnehmerliste eines RSVP-Formulars (L-R1): eine Zeile je Anmeldung mit
 * Vorname, E-Mail, Telefon, Begleitpersonen, Personen gesamt, Status und
 * Anmeldezeit, plus Summen fuer die Summenzeile.
 *
 * Status-Logik: eingecheckt (checked_in_at gesetzt) vor bestaetigt
 * (doi_confirmed_at gesetzt ODER Formular ohne DOI) vor unbestaetigt.
 */
final class ParticipantList {

	public const STATUS_CONFIRMED   = 'confirmed';
	public const STATUS_PENDING     = 'pending';
	public const STATUS_CHECKED_IN  = 'checked_in';

	private EntryRepository $entries;

	/**
	 * @param EntryRepository $entries Entry-Repository.
	 */
	public function __construct( EntryRepository $entries ) {
		$this->entries = $entries;
	}

	/**
	 * Status eines Entries.
	 *
	 * @param array<string, mixed> $entry       Entry.
	 * @param bool                 $doi_enabled Verlangt das Formular eine DOI-Bestaetigung?
	 * @return string
	 */
	public static function status( array $entry, bool $doi_enabled ): string {
		if ( ! empty( $entry['checked_in_at'] ) ) {
			return self::STATUS_CHECKED_IN;
		}
		if ( ! $doi_enabled || ! empty( $entry['doi_confirmed_at'] ) ) {
			return self::STATUS_CONFIRMED;
		}
		return self::STATUS_PENDING;
	}

	/**
	 * Uebersetzte Status-Labels.
	 *
	 * @return array<string, string>
	 */
	public static function status_labels(): array {
		return array(
			self::STATUS_CONFIRMED  => __( 'bestaetigt', 'leadlotse' ),
			self::STATUS_PENDING    => __( 'unbestaetigt', 'leadlotse' ),
			self::STATUS_CHECKED_IN => __( 'eingecheckt', 'leadlotse' ),
		);
	}

	/**
	 * Baut die Teilnehmerliste eines Formulars.
	 *
	 * @param int                  $form_id    Formular-ID.
	 * @param array<string, mixed> $definition Formular-Definition.
	 * @return array{rows:array<int,array<string,mixed>>,total_persons:int,total_companions:int}
	 */
	public function for_form( int $form_id, array $definition ): array {
		$doi_enabled = 'newsletter' === (string) ( $definition['purpose'] ?? '' )
			|| ! empty( $definition['settings']['doi']['enabled'] );

		$phone_field = '';
		foreach ( (array) ( $definition['fields'] ?? array() ) as $field ) {
			if ( is_array( $field ) && 'phone' === (string) ( $field['type'] ?? '' ) ) {
				$phone_field = (string) ( $field['id'] ?? '' );
				break;
			}
		}

		$rows             = array();
		$total_persons    = 0;
		$total_companions = 0;

		foreach ( $this->entries->all_for_form( $form_id ) as $entry ) {
			$values     = (array) ( $entry['field_values'] ?? array() );
			$companions = '' !== EventCapacity::companions_field_id( $definition )
				? absint( $values[ EventCapacity::companions_field_id( $definition ) ] ?? 0 )
				: 0;
			$persons    = 1 + $companions;

			$first_name = (string) ( $values['first_name'] ?? ( $values['name'] ?? '' ) );

			$rows[] = array(
				'entry_id'   => (int) $entry['id'],
				'first_name' => $first_name,
				'email'      => (string) ( $entry['email'] ?? '' ),
				'phone'      => '' !== $phone_field ? (string) ( $values[ $phone_field ] ?? '' ) : '',
				'companions' => $companions,
				'persons'    => $persons,
				'status'     => self::status( $entry, $doi_enabled ),
				'created_at' => (string) ( $entry['created_at'] ?? '' ),
			);

			$total_persons    += $persons;
			$total_companions += $companions;
		}

		return array(
			'rows'             => $rows,
			'total_persons'    => $total_persons,
			'total_companions' => $total_companions,
		);
	}
}
