<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class PlatformConnection extends Model
{
    use HasFactory;

    protected $fillable = [
        'client_id',
        'platform',
        'connection_name',
        'account_id',
        'account_name',
        'status',
        'currency_code',
        'timezone',
        'last_sync_at',
        'last_error',
        'meta',
        'secure_credential_id',
    ];

    protected $casts = [
        'last_sync_at' => 'datetime',
        'meta' => 'array',
    ];

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

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

    public function apiRequestLogs(): HasMany
    {
        return $this->hasMany(ApiRequestLog::class, 'client_id', 'client_id')
            ->where('platform', $this->platform);
    }
}
