<?php

namespace App\Models;

use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use \App\Models\traits\SetConnection;

use GuzzleHttp\Psr7;
use Illuminate\Support\Facades\Storage;

class RelationArticlePhoto extends Model
{
    use SetConnection;

    protected $fillable = [
        'article_id', 'file_type', 'file_path', 'file_disp', 'property', 'property_sub', 'memo', 'order', 'company_id', 'vertical', 'file_path2', 'file_type2'
    ];
    protected static function boot()
    {
        parent::boot();
        self::created(function (RelationArticlePhoto $item) {
            if (!empty($item->attributes["file_path"]) && $item->attributes["company_id"] == 1) {
                self::uploadRemoteMic($item->attributes["file_path"]);
            }
        });
    }

    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 = RelationArticlePhoto::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)
    {
        $res = RelationArticlePhoto::where('article_id', '=', $id)
	->where("company_id", auth()->user()->company_id)
        ->orderBy('order')
        ->get();
        return $res;
    }

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

        $res = RelationArticlePhoto::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();
    }

    static function countImageArticleHouse($id, $property_sub)
    {
        $user = Auth::user();

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

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

        $res = $res->whereIn('property_sub', $property_sub);

        return $res->count();
    }

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

        $res = RelationArticlePhoto::where('article_id', '=', $id)->where("company_id", auth()->user()->company_id);
        if (!empty($property_sub)) {
            $in = [7,8,9,10,11,12,13,14,15,16,18,19,21];
            $out = [4,5,6,17,20];
            if($property_sub == 2){
                $res = $res->whereIn('property_sub', $in);
            }else{
                $res = $res->whereIn('property_sub', $out);
            }
        }
        $res= $res->orderBy('order')->get();
        return $res;
    }

    public function getStorageLinkAttribute()
    {
        return str_replace('public', '/storage', $this->attributes["file_path"]);
    }
    public static function uploadRemoteMic($file_path)
    {
        $ar_folder = explode('/', $file_path);
        unset($ar_folder[count($ar_folder) - 1]);
        $folder = implode('/', $ar_folder);
        $folder = str_replace('public', 'storage', $folder);


        $url = env("URL_MIC_EXPORT", "");
        $path = storage_path("app".DIRECTORY_SEPARATOR.$file_path);

        try {
            $client = new Client(['verify' => false]);
            $r = $client->request('POST', $url, [
                'multipart' => [
                    [
                        'name'     => 'file_name',
                        'contents' => Psr7\Utils::tryFopen($path, 'r'),
                    ],
                    [
                        'name'     => 'folder',
                        'contents' => "/".$folder."/",
                    ],
                ]
            ]);

            $code = $r->getStatusCode();
            $body = $r->getBody();
            $remainingBytes = $body->getContents();
            //dd($remainingBytes);
        } catch (\Error $error) {
            var_dump($error);
        }
    }
}
