<?php

namespace App\Services\Integrations\Preview;

class DataPreviewResult
{
    public function __construct(
        public readonly bool $ok,
        public readonly string $message,
        public readonly string $platform,
        public readonly string $from,
        public readonly string $to,
        public readonly array $totals = [],
        public readonly array $rows = [],
        public readonly array $diagnostics = [],
        public readonly ?string $error = null,
        public readonly int $durationMs = 0,
    ) {}

    public static function success(string $message, string $platform, string $from, string $to, array $totals = [], array $rows = [], array $diagnostics = [], int $durationMs = 0): self
    {
        return new self(true, $message, $platform, $from, $to, $totals, $rows, $diagnostics, null, $durationMs);
    }

    public static function failed(string $message, string $platform, string $from, string $to, ?string $error = null, array $diagnostics = [], int $durationMs = 0): self
    {
        return new self(false, $message, $platform, $from, $to, [], [], $diagnostics, $error, $durationMs);
    }

    public function toArray(): array
    {
        return [
            'ok' => $this->ok,
            'message' => $this->message,
            'platform' => $this->platform,
            'from' => $this->from,
            'to' => $this->to,
            'totals' => $this->totals,
            'rows' => $this->rows,
            'diagnostics' => $this->diagnostics,
            'error' => $this->error,
            'duration_ms' => $this->durationMs,
            'previewed_at' => now()->toIso8601String(),
        ];
    }
}
