<?php

    namespace App\Models;

    use App\Traits\common;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Http\Request;

    use Illuminate\Validation\Rule;
    use Kyslik\ColumnSortable\Sortable;
    use OwenIt\Auditing\Contracts\Auditable;


    class {{Model}} extends Model implements Auditable {

        use SoftDeletes, Sortable, \OwenIt\Auditing\Auditable, common;


        /**
         * The attributes that are mass assignable.
         * @var array
         */
        protected $fillable = [
            'title'
        ];


        /**
         * The attributes that should be hidden for arrays.
         * @var array
         */
        protected $hidden = [

        ];


        /**
         * The attributes that will be cast to carbon date instances.
         * @var array
         */
        protected $dates = ['created_at', 'updated_at', 'deleted_at'];


        /**
         * The attributes that will be sortable.
         * @var array
         */
        public $sortableAs = ['id', 'title', 'created_at'];


        //todo: Add below here the connections with other models


        /**
         * starts the sequence of activating the model and any of its parents if necessary
         */
        public function activateSequence() {
            try {
                \DB::transaction(function () {
                //todo: add any other parent connections to be restored
                    $this->restore();
                });
                return 'success';
            } catch (\Throwable $e) {
                return 'fail';
            }
        }


        /**
         * starts the sequence of deactivating the model and any of its children
         */
        public function deactivateSequence() {
            try {
                \DB::transaction(function () {
                //todo: add any other child connections to be deactivated
                    $this->delete();
                });
                return 'success';
            } catch (\Throwable $e) {
                return 'fail';
            }
        }


        /**
         * starts the sequence of deleting the model and any of its children
         */
        public function deleteSequence() {
            try {
                \DB::transaction(function () {
                //todo: add any other child connections to be deleted
                    $this->forceDelete();
                });
                return 'success';
            } catch (\Throwable $e) {
                return 'fail';
            }
        }


        /**
         * returns the children count of that model
         */
        public function children() {
            return [
                'pivot' => [
                    //todo: add the children count in the pivot tables here
                ],
                'child' => [
                    //todo: add the children count here
                ]
            ];
        }


        /**
         * returns the model rules
         * @param {{Model}}|null ${{model}}
         * @return array
         */
        public static function rules({{Model}} ${{model}} = null){
            $unique = ${{model}} ? Rule::unique('{{name_snake_plural}}')->ignore(${{model}}->id, 'id') : 'unique:{{name_snake_plural}}';
            return [
                'title' => ['required', 'string', 'max:55', $unique],
               //todo: add more rules below here
            ];
        }


        /**
         * returns the properties of the model after validating it against the rules
         * @param Request $request
         * @param {{Model}}|null ${{model}}
         * @return array
         */
        public static function getValidatedParameters(Request $request, {{Model}} ${{model}} = null){
            //todo: transform any fake id here before proceeding to the validation
            return $request->validate(self::rules(${{model}}));
        }


    }