<?php

namespace App\Http\Controllers;
use App\Models\Announcement;
use Illuminate\Http\Request;

class AnnouncementController extends Controller
{
    ##STORE
    public function storeannouncement(Request $request){
        $validate   =   $request->validate([
            'announcement_text' => 'required',
        ]);
        $storedata                    = new Announcement();
        $storedata->announcement_text = $request->announcement_text;
        $store                        =  $storedata->save();
        if ($store == true){
            return response()->json(['status' => true,'data' => 'Announcement data added Successfully'],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }

    ##GET 
    public function getannouncement(){
        $getdata     = Announcement::all();
        if ($getdata == true){
            return response()->json(['success' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    ##SWITCH
    public function switchannouncement($id){
        $switchData                  = Announcement::find($id);
        $status                      = $switchData->active_status === 1 ? ['num' => 0, 'message' => 'Deactiveted'] : ['num' => 1, 'message' => 'Activeted'];
        $switchData->active_status   = $status['num'];
        $Switch                      = $switchData->save();
        if ($Switch) {
            return response()->json(['status' => true,'message'=> $status['message']],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed switch '],400);
        }
    }

    ##DESTORY
    public function destroyannouncement($id){
        $destroy  = Announcement::where('announcement_id', $id)->delete();
        if ($destroy == true) {
            return response()->json(['status' => true,'message'=>'destroy Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed destroy'],400);
        }
    }

    ##UPDATE
    public function updateannouncement(Request $request){
        $validate   =   $request->validate([
            'announcement_text' => 'required',
        ]);
        $updatedata                     = Announcement::find($request->id);
        $updatedata->announcement_text  = $request->announcement_text;
        $update                         = $updatedata->save();
        if (!$updatedata){
            return response()->json(['status' => false,'message'=>'update data not found'],404);
        }else if ($update == true) {
            return response()->json(['status' => true,'message'=>'update Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed update'],400);
        }
    }

    
    ##FIND
    public function findannouncement($id){
        $find  = Announcement::where('announcement_id', $id)->orderBy('announcement_id', 'desc')->get();
        if ($find == true) {
            return response()->json(['status' => true,'message'=>'Find Data Successfully','data' =>  $find],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }

    ##SEARCH
    public function searchannouncement($name){
        $searchRecord   = Announcement::where('announcement_text', 'LIKE', '%'.$name.'%')->get();
        if (!empty($searchRecord)) {
            return response()->json(['status' => true,'message'=>'search Record Successfully','data' =>  $searchRecord],200);
        }else{
            return response()->json(['status' => false,'message'=>'searchRecord Record Failed'],400);
        }
    }
}
