<?php

namespace App\Http\Controllers\Api\V1;

use App\Domain\Notification\Models\PushAnnouncement;
use App\Domain\Notification\Services\AnnouncementBroadcastService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class BroadcastController extends Controller
{
    public function __construct(private AnnouncementBroadcastService $broadcastService) {}

    public function send(Request $request): JsonResponse
    {
        $request->validate([
            'title' => 'required|string|max:200',
            'body' => 'required|string|max:1000',
            'target_type' => 'required|in:all,group,branch,program',
            'target_ids' => 'required_unless:target_type,all|array',
            'target_ids.*' => 'integer',
            'push_data' => 'nullable|array',
            'scheduled_at' => 'nullable|date|after:now',
        ]);

        $user = $request->user();

        $data = $request->only(['title', 'body', 'target_type', 'target_ids', 'push_data', 'scheduled_at']);

        if ($request->scheduled_at) {
            $announcement = $this->broadcastService->create($data, $user);

            return response()->json([
                'success' => true,
                'message' => 'تم جدولة الإشعار بنجاح',
                'announcement' => [
                    'uuid' => $announcement->uuid,
                    'status' => $announcement->status,
                    'scheduled_at' => $announcement->scheduled_at->toIso8601String(),
                ],
            ], 201);
        }

        $announcement = $this->broadcastService->sendImmediate($data, $user);

        return response()->json([
            'success' => true,
            'message' => 'تم إرسال الإشعار بنجاح',
            'announcement' => [
                'uuid' => $announcement->uuid,
                'status' => $announcement->status,
                'sent_count' => $announcement->sent_count,
                'failed_count' => $announcement->failed_count,
                'sent_at' => $announcement->sent_at?->toIso8601String(),
            ],
        ]);
    }

    public function index(Request $request): JsonResponse
    {
        $announcements = PushAnnouncement::where('academy_id', $request->user()->academy_id)
            ->orderByDesc('created_at')
            ->paginate(20);

        return response()->json([
            'data' => $announcements->map(fn ($a) => [
                'uuid' => $a->uuid,
                'title' => $a->title,
                'body' => $a->body,
                'target_type' => $a->target_type,
                'status' => $a->status,
                'sent_count' => $a->sent_count,
                'failed_count' => $a->failed_count,
                'scheduled_at' => $a->scheduled_at?->toIso8601String(),
                'sent_at' => $a->sent_at?->toIso8601String(),
                'created_at' => $a->created_at->toIso8601String(),
            ]),
            'meta' => [
                'current_page' => $announcements->currentPage(),
                'last_page' => $announcements->lastPage(),
                'total' => $announcements->total(),
            ],
        ]);
    }
}
