<?php

namespace App\Models;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;

class PropertyPhoto extends BaseModel
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'property_photo';
    protected $appends = ['photoUrl'];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    protected $dateFormat = 'U';
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];


    public function getPhotoUrlAttribute()
    {
        $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $this->property_id . '/' . $this->photo_name . '_1024x768.' . $this->file_ext;
        $photoUrlThumbFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $this->property_id . '/' . $this->photo_name . '_200x200.' . $this->file_ext;

        if (File::exists($photoUrlFilePath)) {
            $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $this->property_id . '/' . $this->photo_name .'_1024x768.' . $this->file_ext;
        }else {
            $photoUrlFilePath = Config::get('app.imageUrl') . '/property-photos/' . $this->property_id . '/' . $this->photo_name .'_medium.' . $this->file_ext;
        }

        if (File::exists($photoUrlThumbFilePath)) {
            $photoUrlThumbFilePath = Config::get('app.imageUrl') . '/property-photos/' . $this->property_id . '/' . $this->photo_name .'_200x200.'. $this->file_ext;
        }else {
            $photoUrlThumbFilePath = Config::get('app.imageUrl') . '/property-photos/' . $this->property_id . '/' . $this->photo_name .'_thumbnail.'. $this->file_ext;
        }
        $photoTypes = [
            'default' => Config::get('app.imageUrl') . '/property-photos/' . $this->property_id . '/' . $this->photo_name . '.' . $this->file_ext,
            'thumb' => $photoUrlThumbFilePath,
            'fixed' => $photoUrlFilePath,

        ];

        return $photoTypes;
    }

    // Config::get('app.imageUrl') .  '/property-photos/' . $params['property_id'] . '/' .$value['property_photo']['photo_name'] . '.' . $value['property_photo']['file_ext']

    public function propertyRooms()
    {
        return $this->hasMany('App\Models\PropertyRoomPhotoMapping', 'photo_id', 'id');
    }

    public function propertyPlaces()
    {
        return $this->hasMany('App\Models\PropertyPlacePhotoMapping', 'photo_id', 'id');
    }
}
