<?php

namespace Technical\Integrations\Models;

use Functional\Users\Models\User;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Technical\Integrations\Database\Factories\ExternalActivityFactory;

/**
 * Activité importée depuis un fournisseur externe (course, sortie vélo, etc.), rattachée
 * à l'intégration qui l'a récupérée. Couche générique : "provider" et "type" restent des strings
 * libres propres au fournisseur, jamais de référence en dur.
 *
 * @property string $provider
 * @property string $external_id
 * @property string $type
 * @property \Illuminate\Support\Carbon $started_at
 * @property int|null $duration_seconds
 * @property float|null $distance_meters
 * @property int|null $calories
 * @property array|null $raw
 */
#[UseFactory(ExternalActivityFactory::class)]
class ExternalActivity extends Model
{
    use HasFactory;

    protected $fillable = [
        'integration_id', 'user_id', 'provider', 'external_id', 'type', 'started_at',
        'duration_seconds', 'distance_meters', 'calories', 'raw',
    ];

    protected $casts = [
        'started_at' => 'datetime',
        'duration_seconds' => 'integer',
        'distance_meters' => 'decimal:2',
        'calories' => 'integer',
        'raw' => 'array',
    ];

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

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