<?php

namespace App\Http\Controllers\Eventmie;

use Classiebit\Eventmie\Http\Controllers\VenueController as BaseVenueController;
use App\Models\Venue;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Carbon\Carbon;
use Auth;

class VenueController extends BaseVenueController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function show(\Classiebit\Eventmie\Models\Venue $venue)
    {   
        $path       = null;
        $venue_path = "/venues/{$venue->slug}"; 
        return view('vendor.eventmie-pro.venues.show', compact('venue', 'path', 'venue_path'));
        
    }

    /**
     * Handle API venue requests with slug parameter
     */
    public function showBySlug($slug)
    {
        // Find venue by slug
        $venue = Venue::where('slug', $slug)->first();
        
        if (!$venue) {
            return response()->json([
                'status' => false,
                'message' => 'Venue not found'
            ], Response::HTTP_NOT_FOUND);
        }
        
        return $this->showApi($venue);
    }

    /**
     * Show venue data as JSON for API requests
     *
     * @param \Classiebit\Eventmie\Models\Venue $venue
     * @return \Illuminate\Http\JsonResponse
     */
    public function showApi($venue)
    {
        // Load relationships
        $venue->load(['country', 'events.user', 'events.category', 'events.tickets']);

        $venue = Venue::with(['events' => function($query){
             $query->with(['tickets', 'venues'])->leftJoin("categories", "categories.id", '=', "events.category_id")
                ->select(["events.*", "categories.name as category_name"]);
                // CUSTOM;
            $query->selectRaw("(SELECT CN.country_name FROM countries CN WHERE CN.id = events.country_id) country_name")
                    ->selectRaw("(SELECT SD.repetitive_type  FROM schedules SD WHERE SD.event_id = events.id limit 1 ) repetitive_type");
        }])->where('id', $venue->id)->first();

        foreach($venue->events as $key => $event){
            // online event - yes or no
            $event                  = $event->makeVisible('online_location');
            // check event is online or not
            $event->online_location    = (!empty($event->online_location)) ? 1 : 0; 

            $venue->events[$key] = $event;

        }
        
        // Format venue data for API response
        $venueData = [
            'id' => $venue->id,
            'title' => $venue->title,
            'slug' => $venue->slug,
            'description' => $venue->description,
            'venue_type' => $venue->venue_type,
            'address' => $venue->address,
            'city' => $venue->city,
            'state' => $venue->state,
            'zipcode' => $venue->zipcode,
            'latitude' => $venue->glat,
            'longitude' => $venue->glong,
            'images' => $venue->images ? json_decode($venue->images, true) : [],
            'status' => (bool) $venue->status,
            'organizer_id' => $venue->organizer_id,
            'country' => $venue->country ? [
                'id' => $venue->country->id,
                'name' => $venue->country->country_name,
                'code' => $venue->country->country_code,
            ] : null,
            'events_count' => $venue->events->count(),
            'events' => $venue->events->map(function($event) {
                return $event;
            }),
        ];

        return response()->json([
            'data' => $venueData,
            'status' => true,
        ], Response::HTTP_OK);
    }
   
}
