<?php

namespace Hilco\Models;

/**
 * Class WebCategory_WebGroup
 * @package Hilco\Models
 * @property int $webcategory_id
 * @property int $webgroup_id
 * @property bool $is_featured
 * @property int $display_order
 */
class WebCategory_WebGroup extends WebModel {
    protected $table = 'WebCategory_WebGroup';
    protected $fillable = ['webcategory_id', 'webgroup_id', 'is_featured', 'display_order', 'commit_sequence'];

    public static function maxCommit(): int {
        return WebCategory_WebGroup::withTrashed()->max("commit_sequence");
    }

    public static function nextCommit(): int {
        return WebCategory_WebGroup::maxCommit() + 1;
    }

    public static function createWebCategoryWebGroup (array $wcwgData) {
        if (! isset($wcwgData['commit_sequence'])) {
            $wcwgData['commit_sequence'] = WebCategory_WebGroup::nextCommit();
        }
        return WebCategory_WebGroup::create($wcwgData);
    }

    public static function deleteWebCategoryWebGroup ($id) {
        WebCategory_WebGroup::find($id)->delete();
        WebCategory_WebGroup::onlyTrashed()->find($id)->update([
            'commit_sequence' => WebCategory_WebGroup::nextCommit()
        ]);
    }

    public function webCategory() {
        return $this->belongsTo(WebCategory::class, 'webcategory_id', 'id');
    }

    public function webGroup() {
        return $this->belongsTo(WebGroup::class, 'webgroup_id', 'id');
    }

    public function scopeByWCWG ($query, $webcategory_id, $webgroup_id) {
        return $query->where('webcategory_id', $webcategory_id)->where('webgroup_id', $webgroup_id);
    }
}