<?php
/**
 * BookiFlex Onboarding Extension
 *
 * @package    BookiFlex
 * @subpackage Onboarding
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Onboarding\Factory;

use BookiFlex\Extensions\Onboarding\ValueObject\WeeklyPricing;

defined( 'ABSPATH' ) || exit;

/**
 * DateUnit Price Factory
 *
 * Builds price array structure for UpdateDateUnits service based on weekly pricing.
 *
 * @package BookiFlex\Extensions\Onboarding\Factory
 * @since   1.0.0
 */
class DateUnitPriceFactory
{
    /**
     * Build prices array for a specific day of week
     *
     * Creates price entries for all variants (__1, __2, ...) with the price for the specified day.
     * UpdateDateUnits applies these prices to ALL selected days in dates[].
     *
     * Supports two pricing formats:
     * - Simple: single price for all occupancy variants
     * - Occupancy-based: different prices per variant (__1, __2, etc.)
     *
     * @param  WeeklyPricing $weeklyPricing Weekly pricing configuration
     * @param  int           $mainPlaces    Number of main places (for generating variants)
     * @param  string        $day           Day of week (mon, tue, wed, thu, fri, sat, sun)
     * @return array<int, array<string, mixed>> Price array for UpdateDateUnits
     */
    public function buildPricesForDay(WeeklyPricing $weeklyPricing, int $mainPlaces, string $day): array
    {
        $result = [];

        // Generate variants based on mainPlaces count
        // e.g., mainPlaces=2 → variants __1 and __2
        for ($i = 1; $i <= $mainPlaces; $i++) {
            $variant = "__{$i}";

            $result[] = [
                'variant' => $variant,
                'scenario' => 'adultOnMainPlace',
                'value' => $weeklyPricing->getPriceForDayAndVariant($day, $variant),
                'cancel' => false,
            ];
        }

        return $result;
    }

    /**
     * Build date range array for a specific day of week (1 year forward)
     *
     * Creates a date range that selects ONLY the specified day of week.
     * For example, 'mon' will select only Mondays in the date range.
     *
     * @param  string $day Day of week (mon, tue, wed, thu, fri, sat, sun)
     * @return array<int, array<string, mixed>> Date range array with only specified day enabled
     */
    public function buildDateRangeForDay(string $day): array
    {
        $dateFrom = (new \DateTime())->format('Y-m-d');
        $dateTo = (new \DateTime('+1 year'))->format('Y-m-d');

        return [[
            'dateFrom' => $dateFrom,
            'dateTo' => $dateTo,
            'mon' => $day === 'mon',
            'tue' => $day === 'tue',
            'wed' => $day === 'wed',
            'thu' => $day === 'thu',
            'fri' => $day === 'fri',
            'sat' => $day === 'sat',
            'sun' => $day === 'sun',
        ]];
    }

    /**
     * Build date range array for UpdateDateUnits (1 year forward, all days)
     *
     * @deprecated Use buildDateRangeForDay() instead for correct weekly pricing
     * @return array<int, array<string, mixed>> Date range array
     */
    public function buildOneYearForwardDateRange(): array
    {
        $dateFrom = (new \DateTime())->format('Y-m-d');
        $dateTo = (new \DateTime('+1 year'))->format('Y-m-d');

        return [[
            'dateFrom' => $dateFrom,
            'dateTo' => $dateTo,
            'mon' => true,
            'tue' => true,
            'wed' => true,
            'thu' => true,
            'fri' => true,
            'sat' => true,
            'sun' => true,
        ]];
    }
}
