<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use DB;
use Classiebit\Eventmie\Models\Tag;
use Classiebit\Eventmie\Models\Booking;
use Classiebit\Eventmie\Models\Commission;
use Classiebit\Eventmie\Models\Country;
use Classiebit\Eventmie\Models\Venue as BaseVenue;
use App\Models\Event;

class Venue extends BaseVenue
{


     /**
     * getImagesAttribute
     *
     * @param  mixed $value
     * @return array
     */
    public function getImagesAttribute($value)
    {
        if(checkPrefix()) {
            $images = collect(json_decode($value))->map(function ($item, $key) {
                            return asset('storage/'.$item);
                        });
            return $images;
        }

        return $value;
    }
    /**
     * Get events with 
     * pagination and custom selection
     * 
     * @return string
     */
    public function venues($params  = [])
    {   
        $query = Venue::query()->with(['country']); 
    
        if(!empty($params['search']))    
        {
            $query
            ->whereRaw("( title LIKE '%".$params['search']."%' 
                 OR state LIKE '%".$params['search']."%' OR city LIKE '%".$params['search']."%')");
        }

        if(!empty($params['city']))
        {
            $query
            ->where('city','LIKE',"%{$params['city']}%");
        }

        if(!empty($params['state']))
        {
            $query
            ->where('state','LIKE',"%{$params['state']}%");
        }

        if(!empty($params['country_id']))
        {
            $query
            ->where('country_id', $params['country_id']);
        }
        
        return $query
        ->where(["status" => 1])->orderBy('updated_at', 'DESC')->paginate(12);
    }

    public function events()
    {
        return $this->belongsToMany(Event::class, 'event_venue', 'venue_id', 'event_id');
    }

     
}
