<?php

namespace App\Models;

use App\Models\traits\SetConnection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;

class RelationPortalPhoto extends Model
{
    use SetConnection;

    protected $fillable = [
        'id','article_id', 'file_type', 'file_path', 'file_disp', 'property', 'property_sub', 'memo', 'order', 'company_id', 'portal_id', 'del', 'property_sub', 'type', 'facility', 'status', 'type_upload', 'mansion', 'created_at'
    ];

    const CREATED_AT = null;
    const UPDATED_AT = null;

    public function article()
    {
        return $this->belongsTo(Article::class, "article_id", "building_id");
    }

    //
    public static function getTopImagePath($id)
    {
        $user = Auth::user();
        $res = RelationPortalPhoto::where('article_id', '=', $id);
        if ($user) {
            $res = $res->where('company_id', $user->company_id);
        }
        $res = $res->orderBy('order')->first();
        return $res;
    }

    public static function getImgByArticleId($id, $type)
    {
        $res = RelationPortalPhoto::where('article_id', '=', $id)->where("company_id", auth()->user()->company_id)->where("type", $type)->where('del', 0)->orderBy('order')->get();
        return $res;
    }

    public static function getCountImgByArticleId($id, $property_sub = null)
    {
        $user = Auth::user();

        $res = RelationPortalPhoto::where('article_id', '=', $id);

        if ($user) {
            $res->where('company_id', $user->company_id);
        }

        if ($property_sub != null) {
            $res = $res->where('property_sub', '=', $property_sub);
        }

        return $res->count();
    }

    public static function getDispImgByArticleId($id, $property_sub = null)
    {

        $res = RelationPortalPhoto::where('article_id', '=', $id)->where("company_id", auth()->user()->company_id);
        if (!empty($property_sub)) {
            $res= $res->where('property_sub', '=', $property_sub);
        }
        $res= $res->orderBy('order')->get();
        return $res;
    }

    public function getStorageLinkAttribute()
    {
        return str_replace('public', '/storage', $this->attributes["file_path"]);
    }

    public function scopeOfImage(Builder $builder, $portal_id = null, $article_id, $type, $facility = null, $del = 0) {
        $res =  $builder->where('article_id', $article_id)
            ->where('type', $type)
            ->where('company_id', auth()->user()->company_id);
        if (!empty($portal_id)) {
            $res = $res->where('portal_id', $portal_id);
        }
        if (!empty($facility)) {
            $res = $res->where('facility', $facility);
        }
        $res = $res->where('del', $del);

        return $res;
    }

    public function scopeOfOrder(Builder $builder) {
        return $builder->orderBy('order', 'ASC');
    }

    public function scopeType(Builder $builder, $type) {
        return $builder->where('type', $type);
    }

    public function scopeDel(Builder $builder, $del) {
        return $builder->where('del', $del);
    }

    public function setCreatedAtAttribute($value)
    {
        $this->attributes['created_at'] = !empty($value) ? $value : Carbon::now();
    }
}
