<?php

namespace App\Models\Boards;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Board extends Model
{
    use SoftDeletes;
    /**
     * Don't auto-apply mass assignment protection.
     *
     * @var array
     */
    protected $guarded = [];

    // explicitly define tables
    protected $table = 'boards';

    protected $fillable = [
        'user_id', 'name', 'description', 'data', 'data_location', 'public', 'width', 'height', 'thumb', 'readonly', 'state', 'is_template', 'paid', 'favourite','board_folder_id','lab_id', 'base_template', 'exclude_from_limit', 'bonus_template', 'from_template'
    ];

    // custom attributes
    public function getCreatedTimestampAttribute()
    {
        return strtotime($this->created_at);
    }

    public function getCreatedStrAttribute()
    {
        return $this->created_at->format('d/m/y');
    }

    public function getUpdatedStrAttribute()
    {
        return $this->updated_at->format('d/m/y');
    }

    public function path()
    {
        return \url('/board') . '/' . $this->id;
    }

    // board tags
    public function tags()
    {
        return $this->belongsToMany('App\Models\Tag', 'boards_tags', 'tag_id', 'board_id');
    }

    // board tags
    public function board_template_types()
    {
        return $this->belongsToMany('App\Models\Boards\BoardTemplateType', 'board_board_template_types', 'board_template_types_id', 'board_id');
    }

    // board owner
    public function user()
    {
        return $this->hasOne('App\Models\Auth\User', 'id', 'user_id');
    }

    // board folder
    public function folder()
    {
        return $this->belongsTo('App\Models\Boards\BoardFolder', 'board_folder_id', 'id');
    }

    // scope for public
    public function scopePublic($query){
        return $query-> where('public', '<>', '1');
        ;
    }

    // scope for user
    public function scopeForUser($query, $id){
        return $query-> where('user_id', '=', $id);
    }

    // scope for favourites
    public function scopeFavourite($query){
        return $query-> where('favourite', '=', '1');
    }

    // scope for bonus templates
    public function scopeBaseTemplates($query){
        return $query-> whereNotNull('base_template');
    }

    // scope for bonus templates
    public function scopeBonusTemplates($query){
        return $query-> whereNotNull('bonus_template');
    }

    // scope for public
//    public function publicBackgrounds(){
//        return $this->hasMany('App\Models\Graphics\BackgroundImage', 'user_id', 'id')->where('public', '=', '1');
//    }

    // filter by tags
    public function scopeWithTags($query, $tags)
    {
        return $query->whereHas('tags', function($q) use ($tags){
            $q->whereIn('tags.id',$tags);
        });
    }

    // filter by tags
    public function scopeWithType($query, $types)
    {
        return $query->whereHas('board_template_types', function($q) use ($types){
            $q->whereIn('board_template_types.id',$types);
        });
    }

    // filter by tags or types
    public function scopeWithTypeOrTag($query, $typesortagsarr)
    {
       // dd($typesortagsarr);
        $tags = $typesortagsarr['tags'];
        $types = $typesortagsarr['types'];
        if( $typesortagsarr['types']){
           // dd($typesortagsarr['types']);
            if( $typesortagsarr['tags']){
                return $query->whereHas('board_template_types', function($q) use ($types){
                    $q->whereIn('board_template_types.id',$types);
                })->whereHas('tags', function($q) use ($tags){
                    $q->whereIn('tags.id',$tags);
                });
            }else{
                return $query->whereHas('board_template_types', function($q) use ($types){
                    $q->whereIn('board_template_types.id',$types);
                });
            }
        }else{
            if( $typesortagsarr['tags']){
                return $query->whereHas('tags', function($q) use ($tags){
                    $q->whereIn('tags.id',$tags);
                });
            }
        }

    }

    public function scopeInUserFolderWithId($query, $id)
    {
        return $query-> where('board_folder_id', '=', $id);
    }


}
