<?php

namespace App\Http\Controllers\Admin\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CreateListingRequest;
use App\Http\Requests\Admin\UpdateListingRequest;
use App\Http\Requests\Admin\UploadListingFileRequest;
use App\libs\Response\GlobalApiResponseCodeBook;
use App\Models\Listing;
use App\libs\Response\GlobalApiResponse;
use App\Services\Admin\ListingService;
use Illuminate\Http\Request;
use File;

class ListingsController extends Controller
{
    private $listingService;

    public function __construct(ListingService $listingService)
    {
        $this->listingService = $listingService;
    }

    public function listingList(Request $request)
    {
        $data = $this->listingService->listingList($request);
        return (new GlobalApiResponse())->success('Listings list', 1, $data);
    }

    public function publicListingList(Request $request)
    {
        $data = $this->listingService->publicListingList($request);
        return (new GlobalApiResponse())->success('Public Listings list', 1, $data);
    }

    public function listingReqsList(Request $request)
    {
        $data = $this->listingService->listingReqsList($request);
        return (new GlobalApiResponse())->success('Listing Reqs list', 1, $data);
    }

    public function listingsSimpleList()
    {
        $data = [];
        $data['listingsList'] = $this->listingService->listingsSimpleList();
        return (new GlobalApiResponse())->success('Listings list', 1, $data);
    }

    public function getListingDetail($id)
    {
        $data = $this->listingService->getListingDetail($id);
        return (new GlobalApiResponse())->success('Listing Details', 1, $data);
    }

    public function getPublicListingDetail($id)
    {
        $data = $this->listingService->getPublicListingDetail($id);
        return (new GlobalApiResponse())->success('Public Listing Details', 1, $data);
    }

    public function uploadListingFile(Request $request)
    {
        $tempPublicImage = $this->listingService->uploadListingFile($request->file('file'));
        return (new GlobalApiResponse())->success('File successfully saved', 1, $tempPublicImage);
    }

    public function uploadListingFiles(Request $request)
    {
        $tempPublicImage = $this->listingService->uploadListingFiles($request->file('file'));
        return (new GlobalApiResponse())->success('File successfully saved', 1, $tempPublicImage);
    }

    public function deleteUploadedListingFile(Request $request)
    {
        $this->listingService->deleteUploadedListingFile($request->all());
        return (new GlobalApiResponse())->success('File successfully deleted', 1);
    }

    public function createListing(Request $request)
    {
        $this->listingService->createListing($request->all());
        if ($this->listingService->hasError())
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_CREDENTIALS, 'Oops, an error occurred', $this->listingService->getErrors());
        return (new GlobalApiResponse())->success('Listing successfully created ', 1);
    }

    public function updateListing(Request $request, $id)
    {
        $this->listingService->updateListing($request->all(), $id);
        if ($this->listingService->hasError())
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_CREDENTIALS, 'Oops, an error occurred', $this->listingService->getErrors());
        return (new GlobalApiResponse())->success('Listing successfully updated ', 1);
    }

    public function deleteListing(Request $request)
    {
        if (auth()->user()->type == 1) {
            if (empty($request->listing_ids)) {
                return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "Listing IDs not found!");
            }
            $listings = Listing::whereIn('id', $request->listing_ids)->get();
            if ($listings) {
                foreach ($listings as $listing) {
                    $listing->delete();
                    if (File::exists($listing->thumb_image) && $listing->thumb_image !== 'public/images/banner-placeholder.jpg') {
                        File::delete($listing->thumb_image);
                    }
                }
                return (new GlobalApiResponse())->success("Listing successfully deleted");
            } else {
                return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Listing not found!");
            }
        }
    }

    public function updateListingStatus(Request $request)
    {
        if (empty($request->listing_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        $listings = Listing::whereIn('id', $request->listing_ids)->get();
        if ($listings) {
            foreach ($listings as $listing) {
                $listing->update([$request->column => $request->status]);
            }
            return (new GlobalApiResponse())->success("Status successfully updated");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Listing not found!");
        }
    }
}
