<?php

namespace App\Http\Controllers\Employer;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Announcement;

class AnnouncementController extends Controller
{
    public function index(Request $request)
    {
        $sess = session('user');
        $sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null);

        $dbAnnouncements = Announcement::where('status', 'approved')
            ->orWhere('employer_id', $sessId)
            ->latest()
            ->get();

        $announcements = $dbAnnouncements->map(function ($ann) {
            $isCharity = true;
            $charityDetails = null;
            $content = $ann->body;

            // Try decoding JSON if formatted as charity announcement
            if (strpos($ann->body, '{"type":"Charity"') === 0) {
                $decoded = json_decode($ann->body, true);
                if ($decoded) {
                    $charityDetails = $decoded;
                    $content = $decoded['content'] ?? $ann->body;
                }
            } else {
                // Fallback structured details if plain text existed previously
                $charityDetails = [
                    'type' => 'Charity',
                    'provided_items' => 'Free Medical Checks & Food Supplies',
                    'event_date' => $ann->created_at->addDays(2)->format('Y-m-d'),
                    'event_time' => '9:00 AM - 3:00 PM',
                    'location_details' => 'Al Quoz Community Center, Dubai',
                    'location_pin' => 'https://maps.google.com',
                    'content' => $ann->body,
                ];
            }

            return [
                'id' => $ann->id,
                'title' => $ann->title,
                'content' => $content,
                'audience' => 'Charity',
                'isCharity' => $isCharity,
                'charityDetails' => $charityDetails,
                'status' => $ann->status ?? 'pending',
                'remarks' => $ann->remarks,
                'created_at' => $ann->created_at->diffForHumans(),
            ];
        })->toArray();

        return Inertia::render('Employer/Announcements', [
            'initialAnnouncements' => $announcements,
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'content' => 'required|string',
            'provided_items' => 'required|string',
            'event_date' => 'required|string',
            'event_time' => 'required|string',
            'location_details' => 'required|string',
            'location_pin' => 'required|string|url',
        ]);

        $body = json_encode([
            'type' => 'Charity',
            'provided_items' => $request->provided_items,
            'event_date' => $request->event_date,
            'event_time' => $request->event_time,
            'location_details' => $request->location_details,
            'location_pin' => $request->location_pin,
            'content' => $request->content,
        ]);

        $sess = session('user');
        $sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null);

        Announcement::create([
            'title' => $request->title,
            'body' => $body,
            'type' => 'Charity',
            'employer_id' => $sessId,
            'status' => 'pending',
        ]);

        return back()->with('success', 'Charity Event posted successfully.');
    }
}
