<?php

namespace App\Http\Controllers;

use App\Models\Announcement;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class AnnouncementController extends Controller
{
    function index()
    {
        return view('admin.announcement.index');
    }

    function create()
    {
        return view('admin.announcement.create');
    }

    function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'description' => 'required',
        ]);

        $date = Carbon::now();


        Announcement::create([
            'title' => $request->title,
            'description' => $request->description,
            'announcement_date' => $request->announcement_date
        ]);

        return redirect()
            ->route('announcements.index')
            ->with('type', 'success')
            ->with('message', 'Announcement created successfully');
    }

    function edit(Request $request, Announcement $announcement)
    {
        return view('admin.announcement.edit', compact('announcement'));
    }

    function update(Request $request, Announcement $announcement)
    {
        $request->validate([
            'title' => 'required',
            'description' => 'required',
            
        ]);

        $announcement->update([
            'title' => $request->title,
            'description' => $request->description,
            'announcement_date' => $request->announcement_date
        ]);

        return redirect()
            ->route('announcements.index')
            ->with('type', 'success')
            ->with('message', 'Announcement updated successfully');
    }

    function show(Announcement $announcement)
    {
        return view('admin.announcement.edit', compact('announcement'));
    }


    function destroy(Announcement $announcement)
    {
        $announcement->delete();
        return redirect()
            ->route('announcements.index')
            ->with('success', 'Announcement deleted successfully');
    }

    public function list(Request $request)
    {
        $data['draw'] = $request->draw;
        $query = Announcement::query();
        $data['recordsFiltered'] =  $data['recordsTotal'] = $query->count();
        $query->select([
            'id',
            'title',
            'announcement_date',
        ]);

        if ($request->search['value'] != '') {
            $search = $request->search['value'];
            $query->where(function ($query) use ($search) {
                $query->where('title', 'like', "%{$search}%");
            });

            $data['recordsFiltered'] = $query->count();
        }

        $query->offset($request->start ?? 0)
            ->limit($request->length ?? 10)
            ->orderBy('created_at', 'desc');

        $data['data'] = $query->get()->map(function ($e) {
            $routeEdit = route('announcements.edit', ['announcement' => $e->id]);
            $routeDelete = route('announcements.destroy', ['announcement' => $e->id]);

            return [
                'title' => $e->title ?? '',
                'announcement_date' => $e->announcement_date ?? '',
                'action' => "<a href='$routeEdit' class='btn btn-sm btn-warning '>
                                Edit
                            </a>
                                <a href='$routeDelete' class='btn btn-sm btn-danger item_delete'>
                                Delete
                            </a>"
            ];
        });

        return response()->json($data);
    }
}
