<?php

namespace App\Http\Controllers\Admin\Api;

use App\Http\Controllers\Controller;
use App\Models\Offplan;
use App\libs\Response\GlobalApiResponse;
use App\libs\Response\GlobalApiResponseCodeBook;
use App\Services\Admin\OffplanService;
use Carbon\Carbon;
use Illuminate\Http\Request;

class OffplanController extends Controller
{
    private $offplanService;

    public function __construct(OffplanService $offplanService) {
        $this->offplanService = $offplanService;
    }

    public function offplansList(Request $request){
        $data = $this->offplanService->offplansList($request);
        return (new GlobalApiResponse())->success('Offplans list',1,$data);
    }

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

    public function getOffplanDetail($id){
        $data = $this->offplanService->getOffplanDetail($id);
        return (new GlobalApiResponse())->success('Offplans Details',1,$data);
    }

    public function createOffplan(Request $request){
        $this->offplanService->createOffplan($request->all());
        if($this->offplanService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->offplanService->getErrors()[0]['message'], $this->offplanService->getErrors());
        }
        return (new GlobalApiResponse())->success('Successfully created');
    }

    public function updateOffplan(Request $request, $id){
        $this->offplanService->updateOffplan($request->all(), $id);
        if($this->offplanService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->offplanService->getErrors()[0]['message'], $this->offplanService->getErrors());
        }
        return (new GlobalApiResponse())->success('Successfully updated');
    }

    public function deleteOffplan(Request $request){
        if (empty($request->offplan_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        auth()->user()->offplans()->whereIn('id', $request->offplan_ids)->delete();
        return (new GlobalApiResponse())->success("Successfully deleted");
    }

    public function updateOffplanStatus(Request $request)
    {
        if (empty($request->offplan_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        $mods = Offplan::whereIn('id', $request->offplan_ids)->get();
        if ($mods) {
            foreach ($mods as $mod) {
                $mod->update([$request->column => $request->status, 'created_at' => Carbon::now()]);
            }
            return (new GlobalApiResponse())->success("Successfully updated");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Offplan not found!");
        }
    }
}
