<?php
namespace PortalPeople\ShippingFields\Model\Checkout;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use PortalPeople\DisabledDates\Model\ResourceModel\DisabledDate\CollectionFactory;

class DeliveryDateConfigProvider implements ConfigProviderInterface
{
    private const XML_PATH_DISABLE_MON = 'portalpeople_disableddates/weekly_block/disable_mon';
    private const XML_PATH_DISABLE_TUE = 'portalpeople_disableddates/weekly_block/disable_tue';
    private const XML_PATH_DISABLE_WED = 'portalpeople_disableddates/weekly_block/disable_wed';
    private const XML_PATH_DISABLE_THU = 'portalpeople_disableddates/weekly_block/disable_thu';
    private const XML_PATH_DISABLE_FRI = 'portalpeople_disableddates/weekly_block/disable_fri';
    private const XML_PATH_DISABLE_SAT = 'portalpeople_disableddates/weekly_block/disable_sat';
    private const XML_PATH_DISABLE_SUN = 'portalpeople_disableddates/weekly_block/disable_sun';

    private ScopeConfigInterface $scopeConfig;
    private CollectionFactory $collectionFactory;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        CollectionFactory $collectionFactory
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->collectionFactory = $collectionFactory;
    }

    public function getConfig(): array
    {
        $map = [
            0 => self::XML_PATH_DISABLE_SUN,
            1 => self::XML_PATH_DISABLE_MON,
            2 => self::XML_PATH_DISABLE_TUE,
            3 => self::XML_PATH_DISABLE_WED,
            4 => self::XML_PATH_DISABLE_THU,
            5 => self::XML_PATH_DISABLE_FRI,
            6 => self::XML_PATH_DISABLE_SAT,
        ];

        $disabledWeekdays = [];
        foreach ($map as $dayIndex => $path) {
            if ($this->scopeConfig->isSetFlag($path, ScopeInterface::SCOPE_STORE)) {
                $disabledWeekdays[] = $dayIndex;
            }
        }

        // Fetch disabled dates from database and format as YYYY-MM-DD strings
        $disabledDates = [];
        try {
            $collection = $this->collectionFactory->create();
            $dates = $collection->getColumnValues('disabled_date');
            foreach ($dates as $date) {
                if ($date) {
                    $disabledDates[] = $this->formatDate($date);
                }
            }
        } catch (\Exception $e) {
            // Log error but don't break checkout if database query fails
            error_log('Error fetching disabled dates: ' . $e->getMessage());
        }

        return [
            'portalpeopleShippingFields' => [
                'disabledWeekdays' => $disabledWeekdays,
                'disabledDates' => $disabledDates,
            ],
        ];
    }

    /**
     * Format date to YYYY-MM-DD string
     */
    private function formatDate($date): string
    {
        if ($date instanceof \DateTime) {
            return $date->format('Y-m-d');
        }
        return (string)$date;
    }
}
