<?php

namespace App\Models;

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

class UpworkEarning extends Model
{
    protected $fillable = [
        'ref_id',
        'transaction_date',
        'date_from',
        'date_to',
        'team_name',
        'team_upwork_id',
        'client_name',
        'contract_title',
        'contract_upwork_id',
        'amount',
        'payment_type',
        'client_id',
        'sale_id',
        'account_id',
        'imported_by_user_id',
        'imported_at',
    ];

    protected function casts(): array
    {
        return [
            'transaction_date' => 'date',
            'date_from'        => 'date',
            'date_to'          => 'date',
            'amount'           => 'decimal:2',
            'imported_at'      => 'datetime',
        ];
    }

    public function isEarning(): bool
    {
        return in_array($this->payment_type, ['Hourly', 'Fixed-price', 'Fixed Price', 'Bonus']);
    }

    public function isWithdrawal(): bool
    {
        return in_array($this->payment_type, ['Withdrawal', 'Withdrawal Fee']);
    }

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

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

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

    public function importedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'imported_by_user_id');
    }
}
