<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Models\Statement;
use App\Repositories\ResponseRepository;

class StatementController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = [
            'header' => 'required',
            'description' => 'required',
        ];
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Statement', 'store'), Statement::create($request->all()), 200);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Statement', 'show'), $this->findStatement($id), 200);
    }

    public function update(Request $request, $id) {
        $validate = $this->response->validate($request->all(), [
            'header' => ['required'],
            'description' => ['required'],
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Statement', 'update'), $this->findStatement($id)->update($request->all()), 200);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findStatement($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Statement', 'destroy'), $size->delete(), 200);
        }
        return $this->response->jsonResponse(true, 'Statement Not Exists', [], 201);
    }

    public function findStatement($id) {
        return Statement::find($id);
    }

    public function getAllStatement() {
        return $this->response->jsonResponse(false, $this->response->message('Statement', 'index'), Statement::orderBy('id', 'desc')->get(), 200);
    }

    public function StatementSwitch($id) {
        $size = $this->findStatement($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Statement '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 200);
        }
        return $this->response->jsonResponse(true, 'Statement Not Exists', [], 201);
    }
}
