<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Something the platform reported differently from the previous read.
 *
 * Deliberately not attributed to a person: between two reads the change may
 * have come from Ads Manager, the Google Ads UI, LinkedIn Campaign Manager, an
 * automated platform rule, or this product's own chat tools. The manager says
 * what changed and when it was noticed, not who did it.
 */
#[Fillable([
    'campaign_id', 'campaign_sync_run_id', 'campaign_platform_entity_id',
    'kind', 'field', 'before', 'after', 'detected_at',
])]
class CampaignPlatformDrift extends Model
{
    public const string Added = 'added';

    public const string Changed = 'changed';

    public const string Removed = 'removed';

    public const string Restored = 'restored';

    protected function casts(): array
    {
        return [
            'before' => 'json',
            'after' => 'json',
            'detected_at' => 'datetime',
        ];
    }

    public function campaign(): BelongsTo
    {
        return $this->belongsTo(Campaign::class);
    }

    public function entity(): BelongsTo
    {
        return $this->belongsTo(CampaignPlatformEntity::class, 'campaign_platform_entity_id');
    }

    public function run(): BelongsTo
    {
        return $this->belongsTo(CampaignSyncRun::class, 'campaign_sync_run_id');
    }
}
