<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Project
 *
 * @property int $id
 * @property int $client_id
 * @property int $parent_project_id
 * @property int $status
 * @property string $title
 * @property string $description
 * @property string $begin_at
 * @property string $end_at
 * @property int $price
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Project newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Project newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Project query()
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereBeginAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereClientId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereEndAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereParentProjectId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereUpdatedAt($value)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|Project whereStatus($value)
 * @property-read Project|null $parentProject
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Payment[] $payments
 * @property-read int|null $payments_count
 * @property-read \Illuminate\Database\Eloquent\Collection|Project[] $childProjects
 * @property-read int|null $child_projects_count
 * @property-read \App\Models\Client|null $client
 * @property-read int $paid_amount
 * @property-read int $unpaid_amount
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Note[] $notes
 * @property-read int|null $notes_count
 */
class Project extends Model
{
    use HasFactory;

    const STATUS_OPENING = 1;

    const STATUS_CLOSED = 0;

    protected $fillable = ['client_id', 'parent_project_id', 'status', 'title', 'description', 'begin_at', 'end_at', 'price'];

    public function getPaidAmountAttribute()
    {
        return $this->payments->sum(function($payment){ return $payment->amount; });
    }

    public function getUnpaidAmountAttribute()
    {
        return $this->price - $this->paid_amount;
    }



    public function parentProject()
    {
        return $this->belongsTo(Project::class, 'parent_project_id');
    }

    public function childProjects()
    {
        return $this->hasMany(Project::class, 'parent_project_id');
    }

    public function payments()
    {
        return $this->hasMany(Payment::class);
    }

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

    public function notes()
    {
        return $this->hasMany(Note::class);
    }
}
