<?php

namespace Opencart\Extension\Alsendo\Handlers;

require_once(DIR_EXTENSION . 'alsendo/src/DTO/ConfigurationRequests/SaveShippingSettingsTemplateRequestDTO.php');
require_once(DIR_EXTENSION . 'alsendo/src/DTO/ConfigurationRequests/UpdateShippingSettingsTemplateRequestDTO.php');
require_once(DIR_EXTENSION . 'alsendo/src/Repositories/ShippingSettingsRepository.php');

use Opencart\Extension\Alsendo\Repositories\ShippingSettingsRepository;
use Opencart\Extension\Alsendo\DTO\ConfigurationRequests\SaveShippingSettingsTemplateRequestDTO;
use Opencart\Extension\Alsendo\DTO\ConfigurationRequests\UpdateShippingSettingsTemplateRequestDTO;

class ShippingSettingsHandler
{
    private ShippingSettingsRepository $repository;
    private object $language;

    public function __construct(ShippingSettingsRepository $repository, object $language)
    {
        $this->repository = $repository;
        $this->language = $language;
    }

    public function saveTemplate(SaveShippingSettingsTemplateRequestDTO $dto): array
    {
        if (empty($dto->name)) {
            return ['error' => $this->language->get('text_template_name_is_required')];
        }

        $validation = $this->validateDefaultContent($dto);
        if (!$validation['valid']) {
            return ['error' => $validation['error']];
        }

        if ($this->templateNameExists($dto->name)) {
            return ['error' => $this->language->get('text_templates_must_have_unique_names')];
        }

        try {
            $templateId = $this->repository->saveTemplate($dto->toArray());
            return ['success' => true, 'id' => $templateId];
        } catch (\Exception $e) {
            return ['error' => ''.$this->language->get('text_failed_to_save_template').': ' . $e->getMessage()];
        }
    }

    public function updateTemplate(UpdateShippingSettingsTemplateRequestDTO $dto): array
    {
        if (!$dto->id) {
            return ['error' => $this->language->get('text_template_id_is_required')];
        }

        if (empty($dto->name)) {
            return ['error' => $this->language->get('text_template_name_is_required')];
        }

        $validation = $this->validateDefaultContent($dto);
        if (!$validation['valid']) {
            return ['error' => $validation['error']];
        }

        $existingTemplate = $this->repository->getTemplateById($dto->id);
        if (!$existingTemplate) {
            return ['error' => $this->language->get('text_template_not_found')];
        }

        if ($existingTemplate->getName() !== $dto->name &&
            $this->templateNameExists($dto->name)) {
            return ['error' => $this->language->get('text_templates_must_have_unique_names')];
        }

        try {
            $this->repository->updateTemplate($dto->id, $dto->toArray());
            return ['success' => true];
        } catch (\Exception $e) {
            return ['error' => ''.$this->language->get('text_failed_to_update_template').': ' . $e->getMessage()];
        }
    }

    public function deleteTemplate(int $id): array
    {
        if (!$id) {
            return ['error' => $this->language->get('text_template_id_is_required')];
        }

        try {
            $this->repository->deleteTemplate($id);
            return ['success' => true];
        } catch (\Exception $e) {
            return ['error' => ''.$this->language->get('text_failed_to_delete_template').': ' . $e->getMessage()];
        }
    }

    public function setAsMain(int $id): array
    {
        if (!$id) {
            return ['error' => $this->language->get('text_template_id_is_required')];
        }

        try {
            $this->repository->setAsMain($id);
            return ['success' => true];
        } catch (\Exception $e) {
            return ['error' => ''.$this->language->get('text_failed_to_set_template_as_main').': ' . $e->getMessage()];
        }
    }

    private function validateDefaultContent($data): array
    {
        return ['valid' => true];
    }

    private function templateNameExists(string $templateName): bool
    {
        $templates = $this->repository->getAllTemplates();

        foreach ($templates as $template) {
            if ($template->getName() === $templateName) {
                return true;
            }
        }

        return false;
    }
} 