<?php

namespace App\Http\Controllers\Admin;

use App\Http\Support\ValidationRule;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use App\Models\Kegiatan;
use App\Models\Konferensi;
use App\Models\Registrasi;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AdminController extends Controller
{
    public function index() {
        return view('home');
    }

    public function index_user() {
        $user = User::get();
        $data = [
            'user' => $user,
        ];

        return view('admin.user.index', $data);
    }

    public function store_user(Request $request) {

        $rules = [
            'name' => 'required|string',
            'email' => 'required|email',
            'password' => 'string|min:8',
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $password = Hash::make($request->password);
            $data = [
                'name' => strip_tags($request->name),
                'email' => strip_tags($request->email),
                'password' => $password
            ];

            User::query()->create($data);
            DB::commit();

            return redirect()->back()
            ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to save');
        }
    }

    public function update_user($id, Request $request){

        DB::beginTransaction();

        try{
            if($request->password){
                $password = Hash::make($request->password);
            }
            else{
                $user = User::query()->where('id', $id)->first();
                $password = $user->password;
            }

            $data = [
                'name' => strip_tags($request->name),
                'email' => strip_tags($request->email),
                'password' => $password
            ];
            User::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_user($id){
        DB::beginTransaction();

        try{
            User::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_kegiatan() {
        $kegiatan = Kegiatan::get();
        $data = [
            'kegiatan' => $kegiatan
        ];

        return view('admin.kegiatan.index', $data);
    }

    public function store_kegiatan(Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => $request->nama,
                'harga' => $request->harga
            ];
            Kegiatan::query()->create($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to create');
        }
    }

    public function update_kegiatan($id, Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => $request->nama,
                'harga' => $request->harga
            ];

            Kegiatan::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_kegiatan($id) {
        DB::beginTransaction();
        try{
            Kegiatan::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_konferensi() {
        $konferensi = Konferensi::get();
        $data = [
            'konferensi' => $konferensi
        ];

        return view('admin.konferensi.index', $data);
    }

    public function store_konferensi(Request $request) {
        $rules = [
            'nama' => 'required|string'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => strip_tags($request->nama)
            ];
            Konferensi::query()->create($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function update_konferensi($id, Request $request) {
        $rules = [
            'nama' => 'required|string'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => $request->nama
            ];

            Konferensi::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_konferensi($id) {
        DB::beginTransaction();
        try{
            Konferensi::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_peserta(){
        $peserta = Registrasi::with('pkKegiatan', 'pkKonferensi')->get();
        $data = [
            'peserta' => $peserta
        ];

        return view('admin.peserta.index', $data);
    }

    public function delete_peserta($id) {
        DB::beginTransaction();
        try{
            Registrasi::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }
}
