<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\REQUESTNAME;
use App\Models\MODELNAME;
use App\Repositories\REPOSITORYNAME;
use App\Repositories\NotificationRepository;
use Illuminate\Http\JsonResponse;
use App\Services\EmailService;
use App\Services\FileService;

class CONTROLLERNAME extends Controller
{
    /**
     * VARREPOSITORYNAME
     *
     * @var REPOSITORYNAME
     */
    private REPOSITORYNAME $VARREPOSITORYNAME;

    /**
     * NotificationRepository
     *
     * @var NotificationRepository
     */
    private NotificationRepository $NotificationRepository;

    /**
     * file service
     *
     * @var FileService
     */
    private FileService $fileService;

    /**
     * email service
     *
     * @var FileService
     */
    private EmailService $emailService;

    /**
     * constructor method
     *
     * @return void
     */
    public function __construct()
    {
        $this->VARREPOSITORYNAME      = new REPOSITORYNAME;
        $this->fileService            = new FileService;
        $this->emailService           = new EmailService;
        $this->NotificationRepository = new NotificationRepository;

        $this->middleware('can:TITLE');
        $this->middleware('can:TITLE Tambah')->only(['create', 'store']);
        $this->middleware('can:TITLE Ubah')->only(['edit', 'update']);
        $this->middleware('can:TITLE Hapus')->only(['destroy']);
    }

    /**
     * get data as pagination
     *
     * @return JsonResponse
     */
    public function index()
    {
        $data = $this->VARREPOSITORYNAME->getPaginate();
        $successMessage = successMessageLoadData("TITLE");
        return response200($data, $successMessage);
    }

    /**
     * get detail data
     *
     * @param MODELNAME $VARMODELNAME
     * @return JsonResponse
     */
    public function show(MODELNAME $VARMODELNAME)
    {
        $successMessage = successMessageLoadData("TITLE");
        return response200($VARMODELNAME, $successMessage);
    }

    /**
     * save new data to db
     *
     * @param REQUESTNAME $request
     * @return JsonResponse
     */
    public function store(REQUESTNAME $request)
    {
        $data = $request->only([
COLUMNS
        ]);

        // bisa digunakan jika ada upload file dan ganti methodnya
        // if ($request->hasFile('file')) {
        //     $data['file'] = $this->fileService->uploadCrudExampleFile($request->file('file'));
        // }

        // use this if you want to create notification data
        // $title = 'Notify Title';
        // $content = 'lorem ipsum dolor sit amet';
        // $userId = 2;
        // $notificationType = 'transaksi masuk';
        // $icon = 'bell'; // font awesome
        // $bgColor = 'primary'; // primary, danger, success, warning
        // $this->NotificationRepository->createNotif($title,  $content, $userId,  $notificationType, $icon, $bgColor);

        // bisa digunakan jika ingim kirim email dan ganti methodnya
        // $this->emailService->methodName($params);

        $result = $this->VARREPOSITORYNAME->create($data);
        logCreate('TITLE', $result);

        $successMessage = successMessageCreate("TITLE");
        return response200($result, $successMessage);
    }

    /**
     * update data to db
     *
     * @param REQUESTNAME $request
     * @param MODELNAME $VARMODELNAME
     * @return JsonResponse
     */
    public function update(REQUESTNAME $request, MODELNAME $VARMODELNAME)
    {
        $data = $request->only([
COLUMNS
        ]);

        // bisa digunakan jika ada upload file dan ganti methodnya
        // if ($request->hasFile('file')) {
        //     $data['file'] = $this->fileService->uploadCrudExampleFile($request->file('file'));
        // }

        $result = $this->VARREPOSITORYNAME->update($data, $VARMODELNAME->id);

        // use this if you want to create notification data
        // $title = 'Notify Title';
        // $content = 'lorem ipsum dolor sit amet';
        // $userId = 2;
        // $notificationType = 'transaksi masuk';
        // $icon = 'bell'; // font awesome
        // $bgColor = 'primary'; // primary, danger, success, warning
        // $this->NotificationRepository->createNotif($title,  $content, $userId,  $notificationType, $icon, $bgColor);

        // bisa digunakan jika ingim kirim email dan ganti methodnya
        // $this->emailService->methodName($params);

        logUpdate('TITLE', $VARMODELNAME, $result);

        $successMessage = successMessageUpdate("TITLE");
        return response200($result, $successMessage);
    }

    /**
     * delete data from db
     *
     * @param MODELNAME $VARMODELNAME
     * @return JsonResponse
     */
    public function destroy(MODELNAME $VARMODELNAME)
    {
        $deletedRow = $this->VARREPOSITORYNAME->delete($VARMODELNAME->id);

        // use this if you want to create notification data
        // $title = 'Notify Title';
        // $content = 'lorem ipsum dolor sit amet';
        // $userId = 2;
        // $notificationType = 'transaksi masuk';
        // $icon = 'bell'; // font awesome
        // $bgColor = 'primary'; // primary, danger, success, warning
        // $this->NotificationRepository->createNotif($title,  $content, $userId,  $notificationType, $icon, $bgColor);

        // bisa digunakan jika ingim kirim email dan ganti methodnya
        // $this->emailService->methodName($params);

        logDelete('TITLE', $VARMODELNAME);

        $successMessage = successMessageDelete("TITLE");
        return response200($deletedRow, $successMessage);
    }
}
