<?php

namespace App\Http\Controllers\Api\Front;

use App\Http\Controllers\Controller;
use App\Http\Requests\Front\ChangePasswordRequest;
use App\Http\Requests\Front\ContractRequest;
use App\Http\Requests\Front\FollowRequest;
use App\Http\Requests\Front\MarketerContractRequest;
use App\Http\Transformers\Front\AdTransformer;
use App\Http\Transformers\Front\ContractTransformer;
use App\Http\Transformers\Front\MarketerTransformer;
use App\Http\Transformers\Front\UserTransformer;
use App\Models\Ad;
use App\Models\Followers;
use App\Models\MarketerContract;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class ProfileController extends Controller
{
    public function profile(Request $request)
    {
        $user = $request->user();
        $fractal = fractal()
            ->item($user)
            ->transformWith(new UserTransformer())
            ->toArray();
        $ads = Ad::where([
            ['status', '!=', Ad::Pending], ['user_id', auth()->id()], ['featured_ad', Ad::NotFeaturedAd]
        ])->OrderBy('created_at', 'desc')->get();
        $ads = fractal()
            ->collection($ads)
            ->transformWith(new AdTransformer())
            ->toArray();
        $featuredAds = Ad::where([
            ['status', '!=', Ad::Pending], ['user_id', auth()->id()], ['featured_ad', Ad::FeaturedAd]
        ])->OrderBy('created_at', 'desc')->get();
        $featuredAds = fractal()
            ->collection($featuredAds)
            ->transformWith(new AdTransformer())
            ->toArray();
        return response()->json([
            'userData' => $fractal,
            'ads' => $ads,
            'featuredAds' => $featuredAds,
            'status' => true,
        ], 200);

    }

    public function updateProfile(Request $request)
    {
        $user = $request->user();
        $user->update([
            'name' => $request->input('name') ?? $user->name,
            'email' => $request->input('email') ?? $user->email,
            'phone' => $request->input('phone') ?? $user->phone,
            'area_id' => $request->input('area_id') ?? $user->area_id,
            'realEstateAdvertiserNumber' => $request->input('realEstateAdvertiserNumber') ?? $user->realEstateAdvertiserNumber,
            'facilityName' => $request->input('facilityName') ?? $user->facilityName,
        ]);
        if ($request->file('img')) {
            $user->addMediaFromRequest('img')->toMediaCollection('profile');
        }
        return $this->dataResponse(null, trans('messages.update_profile'), 200);
    }

    public function changePassword(ChangePasswordRequest $request)
    {
        $user = $request->user();
        $request->validated();
        if (!Hash::check($request->current, $user->password)) {
            return $this->errorResponse(trans('messages.password_not_changed'), null, 401);
        }
        if (Hash::check($request->current, $request->password)) {
            return $this->errorResponse(trans('messages.change_pass'), null, 401);
        }
        $user->password = $request->password;
        $user->save();
        return $this->dataResponse(null, trans('messages.password_changed'), 200);
    }

    public function marketers()
    {
        $marketers = User::role('marketed')->get();
        $fractal = fractal()
            ->collection($marketers)
            ->transformWith(new MarketerTransformer())
            ->toArray();
        return $this->dataResponse($fractal, null, 200);

    }

    public function marketedProfile($id)
    {
        $marketer = User::findOrFail($id);
        $fractal = fractal()
            ->item($marketer)
            ->transformWith(new UserTransformer())
            ->toArray();
        $ads = Ad::where([
            ['status', '!=', Ad::Pending], ['user_id', $id], ['featured_ad', Ad::NotFeaturedAd]
        ])->OrderBy('created_at', 'desc')->get();
        $ads = fractal()
            ->collection($ads)
            ->transformWith(new AdTransformer())
            ->toArray();
        $featuredAds = Ad::where([
            ['status', '!=', Ad::Pending], ['user_id', $id], ['featured_ad', Ad::FeaturedAd]
        ])->OrderBy('created_at', 'desc')->get();
        $featuredAds = fractal()
            ->collection($featuredAds)
            ->transformWith(new AdTransformer())
            ->toArray();
        return response()->json([
            'marketerData' => $fractal,
            'ads' => $ads,
            'featuredAds' => $featuredAds,
            'status' => true,
        ], 200);
    }

    public function follow(FollowRequest $request)
    {
        Followers::updateOrCreate(
            [
                'user_id' => auth()->id(),
                'follower_id' => $request->follower_id,
            ]
            ,
            ['follower_id' => $request->follower_id]
        );
        return $this->dataResponse(null, 'success follow', 200);
    }

    public function viewContract()
    {
        $user = auth()->user();
        $file = $user->getFirstMediaUrl('contract');
        return $this->dataResponse($file, null, 200);
    }

    public function submitContract(ContractRequest $request)
    {
        $user = auth()->user();
        if ($user && $request->file('file')) {
            $user->clearMediaCollection();
            $user->addMediaFromRequest('file')->toMediaCollection('contract');
        }
        return $this->dataResponse(null, ' contract has been added successfully ', 200);
    }

    public function submitMarketerContract(MarketerContractRequest $request)
    {
        $contractData = $request->validated();
        $contractData['marketer_id'] = auth()->id();
        $marketerContract = MarketerContract::create($contractData);
        if ($request->file('file')) {
            $marketerContract->clearMediaCollection();
            $marketerContract->addMediaFromRequest('file')->toMediaCollection();
        }
        return $this->dataResponse(null, ' contract has been added successfully ', 200);

    }

    public function viewMarketerContract($id)
    {
        $contract = MarketerContract::findOrFail($id);
        $file = $contract->getFirstMediaUrl();
        return $this->dataResponse($file, null, 200);
    }

    public function listMarketerContract()
    {
        $contracts = MarketerContract::where('marketer_id', auth()->id())->get();
        $contracts = fractal()
            ->collection($contracts)
            ->transformWith(new ContractTransformer())
            ->toArray();
        return $this->dataResponse($contracts, null, 200);

    }

}
