<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Complaint;
use App\Models\Complaint_Img;
use App\Models\Department;
use Exception;

class LiveRequestController extends Controller
{
    private $folder = "complaint";
    public $common;
    public function __construct()
    {
        $this->common = new Common;
    }

    public function index()
    {
        try {
            $params['data'] = Complaint::whereNotIn('status', [3,4])->with('department', 'service')->get();

            $params['department'] = Department::where('status', 1)->latest()->get();
            foreach($params['department'] as $value) {
                $value['complaint_count'] = Complaint::where('department_id', $value['id'])->whereNotIn('status', [3,4])->count();
            }

            if ($params['data']->isNotEmpty()) {

                $params['locations'] = [];
                foreach ($params['data'] as $value) {
                    if ($value['department'] != null) {
                        $location = [
                            'lat' => (float)$value->latitude,
                            'lng' => (float)$value->longitude,
                            'color' => $value['department']['department_color'] ?? '',
                            'department' => $value['department']['name'],
                            'service' => $value['service']['name'] ?? "",
                            'date' => $value['created_at']->format('Y-m-d'),
                            'before' => asset('assets/imgs/no_img.png'),
                            'after' => asset('assets/imgs/no_img.png'),
                        ];
                
                        // Fetch 'before' image
                        $before_imag = Complaint_Img::where('complaint_id', $value['id'])->where('type', 1)->first();
                        if ($before_imag) {
                            if ($before_imag['content_type'] == 1) {
                                $location['before'] = $this->common->getImage($this->folder, $before_imag['content_url']);
                            } else {
                                $location['before'] = $this->common->getImage($this->folder, $before_imag['thumbnail_image']);
                            }
                        }
                
                        // Fetch 'after' image
                        $after_imag = Complaint_Img::where('complaint_id', $value['id'])->where('type', 2)->first();
                        if ($after_imag) {
                            if ($after_imag['content_type'] == 1) {
                                $location['after'] = $this->common->getImage($this->folder, $after_imag['content_url']);
                            } else {
                                $location['after'] = $this->common->getImage($this->folder, $after_imag['thumbnail_image']);
                            }
                        }
                
                        $params['locations'][] = $location;
                    }
                }
                return view('admin.live_request.index', $params);
            } else {
                return redirect()->back()->with('error', __('label.data_not_found'));
            }
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
}
