<?php

namespace PloverCRM\Modules\Integrations\Models;

class GoogleAdsConnection
{
  public int $id;
  public string $customer_id;
  public string $customer_name;
  public ?string $manager_id;
  public string $status;
  public int $connected_at;
  public int $updated_at;

  public function __construct(array $data)
  {
    $this->id = (int) ($data['id'] ?? 0);
    $this->customer_id = (string) ($data['customer_id'] ?? '');
    $this->customer_name = (string) ($data['customer_name'] ?? '');
    $this->manager_id = isset($data['manager_id']) ? (string) $data['manager_id'] : null;
    $this->status = (string) ($data['status'] ?? 'active');
    $this->connected_at = (int) ($data['connected_at'] ?? 0);
    $this->updated_at = (int) ($data['updated_at'] ?? 0);
  }

  public function isValid(): bool
  {
    return $this->status === 'active' && !empty($this->customer_id);
  }

  public function getConnectedDisplay(): string
  {
    if (!$this->connected_at) {
      return 'Unknown';
    }
    return human_time_diff($this->connected_at, time()) . ' ago';
  }

  public function toPublicArray(): array
  {
    return [
      'id' => $this->id,
      'customer_id' => $this->customer_id,
      'customer_name' => $this->customer_name,
      'status' => $this->status,
      'connected_at' => $this->connected_at,
      'connected_display' => $this->getConnectedDisplay(),
    ];
  }
}
