<?php

namespace App\Commands;

use App\Libraries\FigmaSyncService;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class SyncFigmaData extends BaseCommand
{
	protected $group = 'Figma';
	protected $name = 'figma:sync';
	protected $description = 'Full sync single Figma file snapshots and comments into local database.';

	public function run(array $params)
	{
		CLI::write('Starting Figma full sync...', 'yellow');

		$service = new FigmaSyncService();
		$result = $service->syncAll();

		if (!($result['success'] ?? false)) {
			CLI::error('Sync failed: ' . ($result['message'] ?? 'Unknown error'));
			return;
		}

		$stats = $result['stats'] ?? [];
		CLI::write('Sync done.', 'green');
		CLI::write('Files: ' . ($stats['files_synced'] ?? 0));
		CLI::write('Snapshots: ' . ($stats['versions_synced'] ?? 0));
		CLI::write('Comments: ' . ($stats['comments_synced'] ?? 0));

		$errors = $stats['errors'] ?? [];
		if (!empty($errors)) {
			CLI::newLine();
			CLI::write('Warnings / Errors:', 'yellow');
			foreach ($errors as $error) {
				CLI::write('- ' . $error, 'light_red');
			}
		}
	}
}
