<?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;
use Spatie\Permission\Traits\HasRoles;

class Contract extends Model
{
    use HasFactory, HasRoles;

    protected $table = "CONTRACT";

    protected $primaryKey = 'RID';
    const CREATED_AT = 'DATE_CREATED';
    const UPDATED_AT = 'DATE_MODIFIED';

    protected $fillable = [
        "RID",
        "CLIENT_RID",
        "D_DOC",
        "D_BGN",
        "D_END",
        "MANAGER_SURN",
        "MANAGER_NAME",
        "MANAGER_OTHNAM",
        "MANAGER_POST",
        "MANAGER_PHONE",
        "MANAGER_CELL",
        "MANAGER_EMAIL",
        "MANAGER_FAX",
        "IN_USE",
        "DATE_APPROV",
        "PAYMENT_TYPE",
        "CREDIT_LIMIT",
        "NOTIFICATION_LIMIT",
        "POSTED_BY_INTERNET",
        "POSTED_BY_EMAIL",
        "POSTED_BY_SMS",
        "POSTED_BY_SERVICE",
    ];

    /**
     * Relates a contract to it's equivalent client
     * @return BelongsTo
     */
    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'client_rid', 'rid');
    }
    /**
     * Relates a contract to it's many users
     * @return HasMany
     */
    public function users(): HasMany
    {
        return $this->hasMany(User::class,'contract_rid', 'rid');
    }

    public function contractIprsService(): HasMany
    {
        return $this->hasMany(ContractIprsService::class);
    }

    protected $dates = [
        'd_bgn',
        'd_end',
    ];

    public function tokens(): HasMany
    {
        return $this->hasMany(Tokens::class, 'contract_id', 'rid');
    }
    /*protected $casts = [
        'd_bgn'  => 'date:d-m-Y',
        'd_end' => 'date:d-m-Y',
    ];*/

    public function payments(): HasMany
    {
        return $this->hasMany(Payments::class, 'contract_id','rid');
    }
}
