<?php

namespace App\Http\Controllers\Api\Front;

use App\Http\Controllers\Controller;
use App\Http\Requests\Front\AdsRequest;
use App\Http\Requests\Front\FavouriteRequest;
use App\Http\Transformers\Front\AdTransformer;
use App\Http\Transformers\Front\ServiceTransformer;
use App\Http\Transformers\Front\TypeTransformer;
use App\Models\Ad;
use App\Models\Favourite;
use App\Models\Service;
use App\Models\Type;
use Illuminate\Http\Request;

class AdsController extends Controller
{

    public function services()
    {
        $services = new  Service();
        $services = $services->get();
        $services = fractal()
            ->collection($services)
            ->transformWith(new ServiceTransformer())
            ->toArray();
        return $this->dataResponse($services, null, 200);
    }

    public function types()
    {
        $types = new  Type();
        $types = $types->get();
        $types = fractal()
            ->collection($types)
            ->transformWith(new TypeTransformer())
            ->toArray();
        return $this->dataResponse($types, null, 200);
    }

    public function addAd(AdsRequest $request)
    {
        $adData = $request->validated();
        $adData['user_id'] = auth()->id();
        $ad = Ad::create($adData);
        if ($request->file('img')) {
            $ad->addMediaFromRequest('img')->toMediaCollection();
        }
        return $this->dataResponse(null, 'submitted', 200);

    }

    public function editAd(AdsRequest $request, $id)
    {
        $ad = Ad::findOrfail($id);
        $ad->update([
            'area_id' => $request->input('area_id') ?? $ad->area_id,
            'service_id' => $request->input('service_id') ?? $ad->service_id,
            'type_id' => $request->input('type_id') ?? $ad->type_id,
            'marketed_id' => $request->input('marketed_id') ?? $ad->marketed_id,
            'price' => $request->input('price') ?? $ad->price,
            'lat' => $request->input('lat') ?? $ad->lat,
            'long' => $request->input('long') ?? $ad->long,
            'bank' => $request->input('bank') ?? $ad->bank,
            'coverage' => $request->input('coverage') ?? $ad->coverage,
            'room' => $request->input('room') ?? $ad->room,
            'hall' => $request->input('hall') ?? $ad->hall,
            'bathroom' => $request->input('bathroom') ?? $ad->bathroom,
            'car_entrance' => $request->input('car_entrance') ?? $ad->car_entrance,
            'driver_room' => $request->input('driver_room') ?? $ad->driver_room,
            'maid_room' => $request->input('maid_room') ?? $ad->maid_room,
            'swimming_pool' => $request->input('swimming_pool') ?? $ad->swimming_pool,
            'kitchen' => $request->input('kitchen') ?? $ad->kitchen,
            'inner_staircase' => $request->input('inner_staircase') ?? $ad->inner_staircase,
            'appendix' => $request->input('appendix') ?? $ad->appendix,
            'furnished' => $request->input('furnished') ?? $ad->furnished,
            'property_age' => $request->input('property_age') ?? $ad->property_age,
            'street_view' => $request->input('street_view') ?? $ad->street_view,
            'determine_area' => $request->input('determine_area') ?? $ad->determine_area,
            'office_name' => $request->input('area_id') ?? $ad->office_name,
            'office_num' => $request->input('office_num') ?? $ad->office_num,
            'office_reach' => $request->input('office_reach') ?? $ad->office_reach,
            'des' => $request->input('des') ?? $ad->des,
            'featured' => $request->input('featured') ?? $ad->featured,
            'administration' => $request->input('administration') ?? $ad->administration,
            'featured_ad' => $request->input('featured_ad') ?? $ad->featured_ad,
            'interface' => $request->input('interface') ?? $ad->interface,
        ]);
        if ($request->file('img')) {
            $ad->clearMediaCollection();
            $ad->addMediaFromRequest('img')->toMediaCollection();
        }
        return $this->dataResponse(null, 'Ad has been updated successfully', 200);
    }

    public function showAd($id, Request $request)
    {
        $skip = ($request->_start) ?? 0;
        $ad = Ad::findOrFail($id);
        if ($ad) {
            $ad->update(['count_view' => $ad->count_view + 1]);
        }
        $adTransformer = fractal()
            ->item($ad)
            ->transformWith(new AdTransformer('view'))
            ->toArray();

        $ads = Ad::where([['type_id', $ad->type_id], ['id', '!=', $ad->id]])->orderBy('created_at', 'DESC');
        $count = $ads->count();
        $ads = $ads->take(10)->skip($skip)->get();
        $ads = fractal()
            ->collection($ads)
            ->transformWith(new AdTransformer('type'))
            ->toArray();
        return response()->json([
            'ad' => $adTransformer,
            'related_ads' => $ads,
            'count' => $count,
            'status' => 'true',
        ], 200);
    }

    public function repostAd($id)
    {
        $ad = Ad::findOrFail($id);
        if ($ad->status != Ad::Expired) {
            return $this->errorResponse('Ad already active', null, 422);
        }
        $ad->update(['status' => Ad::Active]);
        return $this->dataResponse(null, 'Your Ad has been active now', 200);
    }

    public function featuredAd($id)
    {
        $ad = Ad::findOrFail($id);
        if ($ad->status != Ad::FeaturedAd) {
            return $this->errorResponse('Ad already featured', null, 422);
        }
        $ad->update(['featured_ad' => Ad::FeaturedAd]);
        return $this->dataResponse(null, 'Your Ad has been featured now', 200);

    }

    public function adsByType($typId, Request $request)
    {
        $skip = ($request->_start) ?? 0;
        $ads = Ad::where([['status', Ad::Active], ['type_id', $typId]])->orderBy('created_at', 'DESC');
        $count = $ads->count();
        $ads = $ads->take(10)->skip($skip)->get();
        $ads = fractal()
            ->collection($ads)
            ->transformWith(new AdTransformer('type'))
            ->toArray();
        return response()->json([
            'data' => $ads,
            'count' => $count,
            'status' => 'true',
        ], 200);
    }

    public function featuredAds(Request $request)
    {
        $skip = ($request->_start) ?? 0;
        $ads = Ad::where('featured_ad', Ad::FeaturedAd)->orderBy('created_at', 'DESC');
        $count = $ads->count();
        $ads = $ads->take(10)->skip($skip)->get();
        $ads = fractal()
            ->collection($ads)
            ->transformWith(new AdTransformer('type'))
            ->toArray();
        return response()->json([
            'data' => $ads,
            'count' => $count,
            'status' => 'true',
        ], 200);
    }

    public function adsOnMap(Request $request)
    {
        $skip = ($request->_start) ?? 0;
        $ads = Ad::where('status', Ad::Active)->orderBy('created_at', 'DESC');
        $count = $ads->count();
        $ads = $ads->take(10)->skip($skip)->get();
        $ads = fractal()
            ->collection($ads)
            ->transformWith(new AdTransformer('map'))
            ->toArray();
        return response()->json([
            'data' => $ads,
            'count' => $count,
            'status' => 'true',
        ], 200);
    }

    public function adDetails($id)
    {
        $ad = fractal()
            ->item(Ad::findOrFail($id))
            ->transformWith(new AdTransformer('type'))
            ->toArray();
        return $this->dataResponse($ad, null, 200);
    }

    public function FavouriteAd(FavouriteRequest $request)
    {
        $adData = $request->validated();
        $adData['user_id'] = auth()->id();
        Favourite::create($adData);
        return $this->dataResponse(null,'submitted' , 200);
    }
}
