<?php
/**
 * Wide layout template for a Bidspirit auction card in the bp_upcoming_wide shortcode.
 *
 * @var $index string
 * @var $featureImage string
 * @var $auction WP_Term
 * @var $auctionState string
 * @var $auctionDay object
 * @var $linkToAuction string
 * @var $dateTimeRaw string
 * @var $dateTime string
 * @var $auctionTitle string
 * @var $registerUrl string
 */

use auctionforge\inc\controllers\Settings;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\model\Auction;

$limitWords = apply_filters('auctionforge_lot_title_words', 80);

// Build the same UTC-normalized data-time string the catalog pages use
// so the existing printTime() JS function can convert to the user's locale.
$dataTime = SettingsHelper::dateTimeFormat($dateTimeRaw);
// Fallback display for no-JS
$dateTimeStr = date_i18n('F jS, Y g:i A', strtotime($dateTimeRaw));

// BidSpirit times are in the auction house's local timezone.
// Convert to UTC using the AF time_zone_name setting for the $isLive check.
$afTzName = SettingsHelper::getOption(Settings::OPTIONS_TIMEZONES, '');
try {
    $tz = !empty($afTzName) ? new \DateTimeZone($afTzName) : new \DateTimeZone('UTC');
    $dtObj = new \DateTime($dateTimeRaw, $tz);
    $auctionStartUtc = $dtObj->getTimestamp();
} catch (\Exception $e) {
    $auctionStartUtc = strtotime($dateTimeRaw);
}
$isLive = ($auctionStartUtc - 3600) <= time();

$isTimedAuction = false;
$auctionAutomatic = get_term_meta($auction->term_id, 'auction_automaticAuction', true);
if ($auctionAutomatic === '1' || $auctionAutomatic === 'true' || $auctionAutomatic === true) {
    $isTimedAuction = true;
} else {
    $auctionType = get_term_meta($auction->term_id, 'auction_type', true);
    if ($auctionType === 'online') {
        $isTimedAuction = true;
    }
}
$enableTimedClosingText = SettingsHelper::isChecked(Settings::ENABLE_TIMED_AUCTION_CLOSING_TEXT, false);

// Resolve countdown label text (custom or default)
$countdownType = $isTimedAuction ? 'timed' : 'live';
$countdownLabel = Settings::getCountdownText($countdownType, 'shortcode');
?>
<div class="bpw-auction-card">
    <div class="bpw-auction-img">
        <?php if ($auctionState != Auction::STATE_PENDING) { ?>
            <a href="<?= esc_url($linkToAuction) ?>">
                <img src="<?= esc_url($featureImage) ?>" alt="<?= esc_attr($auctionTitle) ?>" />
            </a>
        <?php } else { ?>
            <div class="bpw-img-wrap">
                <img src="<?= esc_url($featureImage) ?>" alt="<?= esc_attr($auctionTitle) ?>" />
            </div>
        <?php } ?>
    </div>
    <div class="bpw-auction-body">
        <h3 class="bpw-title">
            <?php if ($auctionState != Auction::STATE_PENDING) { ?>
                <a href="<?= esc_url($linkToAuction) ?>"><?= $auctionTitle ?></a>
            <?php } else { ?>
                <?= $auctionTitle ?>
            <?php } ?>
        </h3>

        <div class="bpw-desc">
            <?php if (!empty($auctionDay->details) && $auctionDay->details != "") { ?>
                <?= wp_trim_words($auctionDay->details, $limitWords, '...') ?>
            <?php } elseif (!empty($auctionDay->name) && $auctionDay->name != "") { ?>
                <?= wp_trim_words($auctionDay->name, $limitWords, '...') ?>
            <?php } else { ?>
                <?= wp_trim_words($auction->description, $limitWords, '...') ?>
            <?php } ?>
        </div>

        <div class="bpw-date bp-auction-start-date"
             data-time="<?= $dataTime ?>"
             data-custom-format=""
             data-custom-format-time=""
             data-unknown-exact-date="<?= !empty($auctionDay->unknownExactDate) ? '1' : '0' ?>"
             data-unknown-date="<?= date_i18n('F Y', strtotime($dateTimeRaw)) ?>"><?= $dateTimeStr ?></div>

        <div class="bpw-countdown-row">
            <?php if ($isLive) { ?>
                <a href="<?= esc_url($linkToAuction) ?>" class="bpw-live-btn"><?= __('Enter Live Auction', 'auctionforge') ?></a>
            <?php } else { ?>
                <span class="bpw-countdown-label"><?= esc_html($countdownLabel) ?></span>
                <div id="countdown-wide-<?= $index ?>" class="bp-countdown" data-time="<?= preg_replace('/\s/', 'T', $dateTime) ?>" style="display:inline-flex;gap:4px;align-items:center;">
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-days bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Days", "auctionforge") ?></span>
                    </div>
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-hours bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Hrs", "auctionforge") ?></span>
                    </div>
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-minutes bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Min", "auctionforge") ?></span>
                    </div>
                    <div style="display:inline-flex;align-items:baseline;gap:2px;">
                        <span class="bp-seconds bpw-cd-num"></span>
                        <span class="bpw-cd-unit"><?= __("Sec", "auctionforge") ?></span>
                    </div>
                </div>
            <?php } ?>
        </div>

        <?php $hasRegister = !empty($registerUrl); ?>
        <div class="bpw-buttons<?= $hasRegister ? '' : ' bpw-buttons-single' ?>">
            <?php if ($auctionState == Auction::STATE_READY || $auctionState == Auction::STATE_RUNNING || $auctionState == Auction::STATE_PRE_AUCTION) { ?>
                <a href="<?= esc_url($linkToAuction) ?>" class="bpw-btn-primary"><?= __('View Catalog', 'auctionforge') ?></a>
                <?php if (!empty($registerUrl)) { ?>
                    <a href="#" class="bpw-btn-secondary" data-micromodal-trigger="modal-register"><?= __('Register', 'auctionforge') ?></a>
                <?php } ?>
            <?php } elseif ($auctionState == Auction::STATE_PENDING) { ?>
                <span class="bpw-btn-disabled"><?= __('Catalog Coming Soon', 'auctionforge') ?></span>
                <?php if (!empty($registerUrl)) { ?>
                    <a href="#" class="bpw-btn-secondary" data-micromodal-trigger="modal-register"><?= __('Register', 'auctionforge') ?></a>
                <?php } ?>
            <?php } elseif ($auctionState == Auction::STATE_ENDING || $auctionState == Auction::STATE_ARCHIVED) { ?>
                <span class="bpw-status-text"><?= __('The auction has ended', 'auctionforge') ?></span>
                <a href="<?= esc_url($linkToAuction) ?>" class="bpw-btn-primary"><?= __('View Results', 'auctionforge') ?></a>
            <?php } ?>
        </div>
    </div>
</div>
