<?php

namespace App\Domain\Payments\Models;

use App\Models\User;
use App\Models\Model;
use App\Domain\Points\Models\Point;

class Withdrawal extends Model
{

    public function getUserPhoneAttribute()
    {
        return $this->user->phone;
    }

    public function getUserTypeAttribute()
    {
        return $this->user->type;
    }

    public function getRequisitesAttribute()
    {
        return 'ИНН: ' . $this->user->inn . ' '  .
            'БИК: ' . $this->user->bik . ' ' .
            'Счет: ' . $this->user->checking_account;
    }

    public static function getTotalAmountForUserInCurrentMonth($userId)
    {
        return self::where('user_id', $userId)
            ->where('status', '<>', 'cancel')
            ->where('created_at', '>=', now()->startOfMonth())
            ->where('created_at', '<', now()->startOfMonth()->addMonth())
            ->sum('amount');
    }

    public static function canRequestWithdrawalPhysical($userId, $requestedAmount)
    {
        $totalAmount = self::getTotalAmountForUserInCurrentMonth($userId);

        return ($totalAmount + $requestedAmount) <= 12000;
    }

    public static function canRequestWithdrawalLegal($userId, $requestedAmount)
    {
        $totalAmount = self::getTotalAmountForUserInCurrentMonth($userId);
        return ($totalAmount + $requestedAmount) <= 150000;
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function requisites()
    {
        return $this->belongsTo(Requisites::class);
    }
    public function point()
    {
        return $this->belongsTo(Point::class);
    }
}
