<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Section;
use App\Models\Classes;
use Illuminate\Support\Facades\Validator;
use Exception;

class SectionController extends Controller
{
    /**
     * Display All Sections related to Classes without Trashed.
    */
    public function index()
    {
        try {
            $sections = Section::select('id','section_name','is_active','class_id')->with('classRelation:id,class_name')->latest()->get();
            if ($sections->isEmpty()) {
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['sections' => $sections];
            return response()->json([
                'data' => $data,
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Display All Sections related to Classes with Trashed.
    */
    public function withTrashed()
    {
        try {
            $sections = Section::select('id','section_name','is_active','class_id')->with('classRelation:id,class_name')->withTrashed()->latest()->get();
            if ($sections->isEmpty()) {
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['sections' => $sections];
            return response()->json([
                'data' => $data,
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Display Only Trashed Sections.
    */
    public function trash()
    {
        try {
            $sections = Section::select('id','section_name','is_active','class_id')->with('classRelation:id,class_name')->onlyTrashed()->latest()->get();
            if ($sections->isEmpty()) {
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['sections' => $sections];
            return response()->json([
                'data' => $data,
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Display Restore Trashed Section.
    */
    public function restore(string $id)
    {
        try {
            $section = Section::select('id','section_name','is_active','class_id')->with('classRelation:id,class_name')->onlyTrashed()->find($id);
            if(!$section){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $section->restore();
            $data = ['section' => $section];
            return response()->json([
                'data' => $data,
                'response_message' => ['success' => ['Class Restored Successfully!']],
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Store a newly created resource in storage.
    */
    public function store(Request $request)
    {
        try {
            $validation = Validator::make($request->all(), [
                'class_id' => 'required|exists:classes,id,deleted_at,NULL',
                'section_name' => 'required|string|unique:sections,section_name,NULL,id,class_id,' . $request->class_id,

            ],[
                'section_name.unique' => 'The Class Name has already been taken',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            $section = Section::create([
                'class_id' => $request->class_id,
                'section_name' => $request->section_name,
            ]);

            $data = ['section' => $section];
            return response([
                'response_status' => 200,
                'response_message' => ['success' => ['Class Created Successfully!']],
                'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Display the specified section with all Classes.
    */
    public function show($id)
    {
        try {
            $section = Section::select('id','section_name','class_id')->with('classRelation:id,class_name')->find($id);
            if(!$section){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            // $classes = Classes::all();
            $data = [
                'section' => $section,
                // 'classes' => $classes,
            ];

            return response()->json([
                'data' => $data,
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Update the Active Section.
    */
    public function activeSection(Request $request, string $id)
    {
        try {

            $validation = Validator::make($request->all(), [
                'id' => 'nullable|exists:sections,id',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            // Find the Section by ID
            $section = Section::select('id','is_active')->find($id);
            if(!$section){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $section->is_active = !$section->is_active;
            $section->save();

            $data = ['section' => $section];
            return response()->json([
                'data' => $data,
                'response_message' => ['success' => ['Class Status Updated Successfully!']],
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Update the specified resource in storage.
    */
    public function update(Request $request, string $id)
    {
        try {
            $validation = Validator::make($request->all(), [
                'class_id' => 'required|exists:classes,id',
                'section_name' => 'required|string|unique:sections,section_name,'. $id .',id,class_id,' . $request->class_id,

            ],[
                'section_name.unique' => 'The Class Name has already been taken',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            $section = Section::find($id);
            if(!$section){
                return response()->json([
                   'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }

            $section->class_id = $request->class_id;
            $section->section_name = $request->section_name;
            $section->save();

            $data = ['section' => $section];
            return response([
                'response_status' => 200,
                'response_message' => ['success' => ['Class Updated Successfully!']],
                'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Temporary Remove from storage.
    */
    public function delete(string $id)
    {
        try {
            $section = Section::find($id);
            if(!$section){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }

            $section->delete();
            $section = Section::onlyTrashed()->latest()->get();
            $data = ['section' => $section];
            return response()->json([
                'response_message' => ['success' => ['Class has been sent to Trash!']],
                'response_status' => 200,
                // 'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Permanent Remove from storage.
    */
    public function destroy(string $id)
    {
        try {
            $section = Section::withTrashed()->find($id);
            if(!$section){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $section->forceDelete();

            return response()->json([
                // 'response_message' => ['success' => ['Class Deleted Permanently!']],
                'response_message' => ['success' => ['Deleted From Trash Successfully!']],
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }
}
