<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Requests\ClientStatusRequest;
use App\Models\Application\Client;
use App\Models\Application\ClientStatus;
use App\Models\Application\Status;
use Exception;

class ClientStatusController extends Controller
{
    private $MESSAGES = [
        'ASSIGN' => 'WARNING.STATUS_ASSIGN',
        'DUPLICATE_ASSIGN' => 'WARNING.DUPLICATE_STATUS_ASSIGN',
        'WRONG_ASSIGN' => 'WARNING.WRONG_STATUS_ASSIGN',
        'UNASSIGN' => 'WARNING.STATUS_UNASSIGN',
    ];

    public function __construct()
    {
        $this->CHANNEL = 'clientstatus_errors';
    }

    public function assign(ClientStatusRequest $request)
    {
        try {
            $status = Status::where([
                ['is_active', true]
            ])->with(['status_type'])->findOrfail($request->fk_status);

            if (!$status || $status->status_type->statustypename !== 'BEHAVIOR') {
                return $this->responseJson($this->MESSAGES['WRONG_ASSIGN'], null, 501);
            };

            $client = Client::with(['statuses'])->findOrFail($request->fk_client);

            if (in_array($status->statusname, $client->statuses->pluck('statusname')->toArray())) {
                return $this->responseJson($this->MESSAGES['DUPLICATE_ASSIGN'], null, 501);
            }

            $clientstatus = ClientStatus::where([
                ['fk_client', $request->fk_client],
                ['fk_status', $request->fk_status],
                ['is_active', false],
            ]);

            if ($clientstatus->exists()) {
                $clientstatus->first()->update([
                    ['fk_userupdater', $this->user()->id],
                    ['is_active', true]
                ]);
            } else {
                ClientStatus::create([
                    'fk_client' => $request->fk_client,
                    'fk_status' => $request->fk_status,
                    'fk_usercreator' => $this->user()->id
                ]);
            }

            return $this->responseJson(null, 201, true);
        } catch (Exception $ex) {
            $this->generateLog($this->CHANNEL, $ex);
            return $this->responseJson($this->MESSAGES['ASSIGN'], null, 501);
        }
    }

    public function unassign(ClientStatusRequest $request)
    {
        try {
            $clientstatus = ClientStatus::where([
                ['fk_status', $request->fk_status],
                ['fk_client', $request->fk_client],
                ['is_active', true]
            ])->first();

            if ($clientstatus) {
                $clientstatus->update([
                    'fk_userupdater' => $this->user()->id,
                    'is_active' => false
                ]);
            }

            return $this->responseJson(null, 201, true);
        } catch (Exception $ex) {
            $this->generateLog($this->CHANNEL, $ex);
            return $this->responseJson($this->MESSAGES['UNASSIGN'], null, 501);
        }
    }
}
