<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Cron;

use Simplyin\Payment\Service\OrderSyncService;
use Psr\Log\LoggerInterface;

/**
 * Cron job for syncing existing customer orders to SimplyIN
 * Runs every 2 hours to sync orders from today to transaction_history_length_days ago
 */
class SyncOrders
{
    /**
     * @var OrderSyncService
     */
    private $orderSyncService;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param OrderSyncService $orderSyncService
     * @param LoggerInterface $logger
     */
    public function __construct(
        OrderSyncService $orderSyncService,
        LoggerInterface $logger
    ) {
        $this->orderSyncService = $orderSyncService;
        $this->logger = $logger;
    }

    /**
     * Execute cron job
     *
     * @return void
     */
    public function execute(): void
    {
        $this->logger->info('SimplyIN: Starting order sync cron job');

        try {
            $result = $this->orderSyncService->syncOrders();
            
            $this->logger->info('SimplyIN: Order sync cron completed', [
                'processed' => $result['processed'],
                'synced' => $result['synced'],
                'skipped' => $result['skipped'],
                'failed' => $result['failed']
            ]);
        } catch (\Exception $e) {
            $this->logger->error('SimplyIN: Order sync cron failed', [
                'error' => $e->getMessage()
            ]);
        }
    }
}
