<?php

namespace App\Models;

use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * A data table object (referencing revisions)
 *
 * @property int $id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property int $owner_id
 * @property int $ancestor_id
 * @property int $revision_id
 * @property string $title
 * @property string $description
 * @property string $license
 * @property string $source_link
 * @property User $owner
 * @property Table $parentTable
 * @property Table[]|Collection $forks
 * @property Revision[]|Collection $revisions
 * @property Revision $activeRevision
 * @property Proposal[]|Collection $proposal
 * @property TableComment[]|Collection $comments
 * @property User[]|Collection $favouritingUsers
 * @property User[]|Collection $discussionFollowers
 */
class Table extends Model
{
    use Reportable;

    protected static function boot()
    {
        parent::boot();

        static::deleting(function(Table $self) {
            // update revision refcounts
            $self->revisions()->decrement('refs');

            $self->reportsOf()->delete();

            // delete revisions with zero refs (manually, to properly cascade to rows)
            foreach ($self->revisions()->where('refs', '<=', 0)->get() as $rev) {
                $rev->delete();
            }
        });
    }

    /** Owning user */
    public function owner()
    {
        return $this->belongsTo(User::class, 'owner_id');
    }

    /** Fork precursor */
    public function parentTable()
    {
        return $this->belongsTo(Table::class, 'ancestor_id');
    }

    /** Fork descendants */
    public function forks()
    {
        return $this->hasMany(Table::class, 'ancestor_id');
    }

    /** All related revisions (this may include merged revisions) */
    public function revisions()
    {
        return $this->belongsToMany(Revision::class, 'table_revision_pivot');
    }

    /** Active revision */
    public function activeRevision()
    {
        return $this->hasOne(Revision::class, 'revision_id');
    }

    /** Proposals submitted to this table */
    public function proposals()
    {
        return $this->hasMany(Proposal::class);
    }

    /** User-submitted comments */
    public function comments()
    {
        return $this->hasMany(TableComment::class);
    }

    /** Users favouriting this table */
    public function favouritingUsers()
    {
        return $this->belongsToMany(User::class, 'table_favourites');
    }

    /** Users to notify about comments */
    public function discussionFollowers()
    {
        return $this->belongsToMany(User::class, 'discussion_follows');
    }
}