<?php

namespace App\Models;

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

class TradingAccount extends Model
{
    protected $fillable = [
        'user_id',
        'name',
        'broker_name',
        'account_number',
        'account_type',
        'platform',
        'server',
        'currency',
        'leverage',
        'balance',
        'equity',
        'margin',
        'free_margin',
        'api_connected',
        'api_provider',
        'api_account_id',
        'api_status',
        'api_error',
        'last_synced_at',
    ];

    protected function casts(): array
    {
        return [
            'balance' => 'decimal:2',
            'equity' => 'decimal:2',
            'margin' => 'decimal:2',
            'free_margin' => 'decimal:2',
            'api_connected' => 'boolean',
            'last_synced_at' => 'datetime',
        ];
    }

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

    public function isManual(): bool
    {
        return ! $this->api_connected;
    }

    public function isApiConnected(): bool
    {
        return $this->api_connected;
    }
}
