<?php

namespace Opencart\Extension\Alsendo\Providers;

use Opencart\Extension\Alsendo\Models\OrderData;
use Opencart\Extension\Alsendo\Repositories\SenderAddressRepository;
use Opencart\Extension\Alsendo\Repositories\ShippingSettingsRepository;

class OrderSectionDataProvider
{
    private SenderAddressRepository $senderAddressRepository;
    private ShippingSettingsRepository $shippingSettingsRepository;

    public function __construct(SenderAddressRepository $senderAddressRepository, ShippingSettingsRepository $shippingSettingsRepository)
    {
        $this->senderAddressRepository = $senderAddressRepository;
        $this->shippingSettingsRepository = $shippingSettingsRepository;
    }

    public function getOrderSectionData(OrderData $orderData): array
    {
        $senderTemplates = $this->senderAddressRepository->getAllTemplates();
        $shippingTemplates = $this->shippingSettingsRepository->getAllTemplates();
        
        $data = [];
        
        $data['sender_templates'] = array_map(function($template) {
            return [
                'id' => $template->getId(),
                'display_name' => $template->getCompanyName() . ' (Id: ' . $template->getId() . ')',
                'data' => $template->toArray()
            ];
        }, $senderTemplates);
        
        $data['shipping_templates'] = array_map(function($template) use ($orderData) {
            $templateData = $template->toArray();

            $templateData['calculated_default_content'] = $templateData['shipment_content'];
            
            return [
                'id' => $templateData['id'],
                'display_name' => $templateData['name'] . ' (Id: ' . $templateData['id'] . ')',
                'data' => $templateData
            ];
        }, $shippingTemplates);
        
        $data['recipient_data'] = $orderData->toArray();
        
        $data['pickup_hours'] = $this->generatePickupHours();
        
        return $data;
    }

    private function generatePickupHours(): array
    {
        $hours = [];
        for ($hour = 6; $hour <= 20; $hour++) {
            $hours[] = sprintf('%02d:00', $hour);
        }
        return $hours;
    }
} 