<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
 * NotificationDemoSeeder — populates the in-app notifications bell for a
 * single demo user, matching the mobile Notifications screen mock-up
 * (8 notifications: 5 today, 3 yesterday). Used to demo / screenshot the
 * Notifications screen against real data.
 *
 * Target user: env DEMO_USER_EMAIL (defaults to user@mediacity.co.in).
 *
 * Usage:
 *   php artisan db:seed --class=NotificationDemoSeeder --force
 *
 * Idempotent — re-running adds another batch of 8 rows. To start clean,
 * truncate notifications for the target user first:
 *   DELETE FROM notifications WHERE notifiable_id = <user_id>;
 */
class NotificationDemoSeeder extends Seeder
{
    public function run(): void
    {
        $email = env('DEMO_USER_EMAIL', 'user@mediacity.co.in');

        $user = DB::table('users')->where('email', $email)->first();
        if (!$user) {
            $this->command?->error("User {$email} not found. Create the account first, then re-run.");
            return;
        }

        $userId = (int) $user->id;
        $now = now();

        // Each entry: [type-class, data-array, created_at offset, is_read]
        $rows = [
            // ── TODAY ────────────────────────────────────────────────
            [
                'data' => [
                    'type'            => 'new_email',
                    'title'           => 'Sarah Johnson replied to your thread',
                    'body'            => 'Thanks for getting back so fast! I think the proposal looks great. Just one note on the pricing tier...',
                    'action_url'      => '/inbox?conversation=902',
                    'icon'            => 'mail',
                    'icon_color'      => '#3b82f6',
                    'avatar_initials' => 'SJ',
                ],
                'created_at' => $now->copy()->subMinutes(2),
                'read_at'    => null,
            ],
            [
                'data' => [
                    'type'         => 'ai_escalation',
                    'title'        => 'AI flagged a thread for escalation',
                    'body'         => 'Tom Chen · Customer sentiment dropped — likely needs a human reply.',
                    'action_url'   => '/inbox?conversation=915',
                    'icon'         => 'sparkles',
                    'icon_color'   => '#8b5cf6',
                ],
                'created_at' => $now->copy()->subMinutes(14),
                'read_at'    => null,
            ],
            [
                'data' => [
                    'type'            => 'new_email',
                    'title'           => 'New WhatsApp from Marco Khan',
                    'body'            => 'Got the invoice — paying via Stripe today. Could you confirm receipt?',
                    'action_url'      => '/inbox?conversation=914&channel=whatsapp',
                    'icon'            => 'message-circle',
                    'icon_color'      => '#22c55e',
                    'avatar_initials' => 'MK',
                ],
                'created_at' => $now->copy()->subMinutes(38),
                'read_at'    => null,
            ],
            [
                'data' => [
                    'type'       => 'deal_stage_changed',
                    'title'      => 'Deal moved to Negotiation',
                    'body'       => 'Enterprise migration — TechHub. Value updated to $12,500.',
                    'action_url' => '/deals/55',
                    'icon'       => 'trending-up',
                    'icon_color' => '#ef4444',
                ],
                'created_at' => $now->copy()->subHour(),
                'read_at'    => $now->copy()->subMinutes(50),
            ],
            [
                'data' => [
                    'type'            => 'mention',
                    'title'           => 'Priya Rao mentioned you in a note',
                    'body'            => '@claire can you take this one? Customer is asking about API rate limits.',
                    'action_url'      => '/inbox?conversation=908&note=234',
                    'icon'            => 'at-sign',
                    'icon_color'      => '#8b5cf6',
                    'avatar_initials' => 'PR',
                ],
                'created_at' => $now->copy()->subHours(2),
                'read_at'    => $now->copy()->subHours(1)->subMinutes(30),
            ],
            // ── YESTERDAY ────────────────────────────────────────────
            [
                'data' => [
                    'type'       => 'campaign_completed',
                    'title'      => 'New campaign sent',
                    'body'       => 'May Newsletter — 2,481 recipients. Live performance available.',
                    'action_url' => '/campaigns/74/report',
                    'icon'       => 'settings',
                    'icon_color' => '#6b7280',
                ],
                'created_at' => $now->copy()->subDay()->setTime(16, 30),
                'read_at'    => $now->copy()->subDay()->setTime(17, 0),
            ],
            [
                'data' => [
                    'type'       => 'channel_connected',
                    'title'      => 'Twilio SMS connected',
                    'body'       => 'Your SMS channel is live. Ready to send and receive.',
                    'action_url' => '/settings/channels',
                    'setup_url'  => '/settings/channels?setup=auto-reply&channel=sms',
                    'icon'       => 'message-square',
                    'icon_color' => '#f59e0b',
                ],
                'created_at' => $now->copy()->subDay()->setTime(14, 5),
                'read_at'    => null,
            ],
            [
                'data' => [
                    'type'       => 'backup_completed',
                    'title'      => 'Backup completed successfully',
                    'body'       => 'Daily backup · 142 MB · Encrypted S3 destination.',
                    'action_url' => '/admin/backups',
                    'icon'       => 'settings',
                    'icon_color' => '#6b7280',
                ],
                'created_at' => $now->copy()->subDay()->setTime(2, 5),
                'read_at'    => $now->copy()->subDay()->setTime(9, 15),
            ],
        ];

        $inserted = 0;
        foreach ($rows as $row) {
            DB::table('notifications')->insert([
                'id'              => (string) Str::uuid(),
                'type'            => 'App\\Notifications\\InAppNotification',
                'notifiable_type' => 'App\\Models\\User',
                'notifiable_id'   => $userId,
                'data'            => json_encode($row['data']),
                'read_at'         => $row['read_at'],
                'created_at'      => $row['created_at'],
                'updated_at'      => $row['created_at'],
            ]);
            $inserted++;
        }

        $this->command?->info("Inserted {$inserted} demo notifications for {$email} (user#{$userId}).");
        $this->command?->info("Open GET /api/v1/mobile/notifications to see them in the mobile API.");
    }
}
