<?php


namespace App\Http\Transformers\Front;


use App\Models\Ad;
use App\Models\Area;
use League\Fractal\TransformerAbstract;

class AdTransformer extends TransformerAbstract
{

    public function __construct($type = false)
    {
        $this->type = $type;
    }

    public function transform(Ad $ad): array
    {

        $array = [
            'id' => (integer) $ad->id,
            'img' => $ad->getFirstMediaUrl(),
            'status' => (int) $ad->status,
            'type' => (string) $ad->type->name,
            'price' => (string) $ad->price,
        ];

        if ($this->type == 'type') {
            $array['bathroom'] = (int) $ad->bathroom;
            $array['room'] = (int) $ad->room;
            $array['hall'] = (int) $ad->hall;
            $array['area'] = (string) $ad->area?->name;
            $array['time'] = $ad->created_at->diffForHumans();
            $array['favourite'] = $ad->favourite?->where('user_id',auth()->id())->count();
        }

        if ($this->type == 'map') {
            $array['lat'] = (string) $ad->lat;
            $array['long'] = (string) $ad->long;
            $array['featured_ad'] = (int) $ad->featured_ad;
            $array['favourite'] = $ad->favourite?->where('user_id',auth()->id())->count();
        }

        if ($this->type == 'view') {
            $array['count_view'] = (int) $ad->count_view;
            $array['des'] = (string) $ad->des;
            $array['area'] = (string) $ad->area?->name;
            $array['service'] = $ad->service?->name;
            $array['lat'] = $ad->lat;
            $array['long'] = $ad->long;
            $array['bank'] = (int) $ad->bank;
            $array['bathroom'] = (int) $ad->bathroom;
            $array['coverage'] = (string) $ad->coverage;
            $array['room'] = (int) $ad->room;
            $array['hall'] = (int) $ad->hall;
            $array['interface'] = (string) $ad->interface;
            $array['car_entrance'] = (int) $ad->car_entrance;
            $array['driver_room'] = (int) $ad->driver_room;
            $array['maid_room'] = (int) $ad->maid_room;
            $array['swimming_pool'] = (int) $ad->swimming_pool;
            $array['kitchen'] = (int) $ad->kitchen;
            $array['inner_staircase'] = (int) $ad->inner_staircase;
            $array['appendix'] = (int) $ad->appendix;
            $array['furnished'] = (int) $ad->furnished;
            $array['property_age'] = (int) $ad->property_age;
            $array['street_view'] = (int) $ad->street_view;
            $array['determine_area'] = (int) $ad->determine_area;
            $array['office_name'] = (string) $ad->office_name;
            $array['office_num'] = (string) $ad->office_num;
            $array['office_reach'] = (string) $ad->office_reach;
            $array['featured'] = (int) $ad->featured;
            $array['administration'] = (int) $ad->administration;
            $array['featured_ad'] = (int) $ad->featured_ad;
            $array['user_id'] = (int) $ad->user_id;
            $array['user_name'] = (string) $ad->user?->name;
            $array['user_phone'] = (string) $ad->user?->phone;
            $array['user_img'] = (string) $ad->user?->getFirstMediaUrl('profile');
            $array['favourite'] = $ad->favourite?->where('user_id',auth()->id())->count();
        }

        return $array;

    }


}
