File size: 13,958 Bytes
3533fb9
 
 
 
 
 
 
8ad8368
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad8368
 
 
 
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad8368
 
 
 
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad8368
 
 
 
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
Lifecycle email Celery tasks for Monteeq.
Registered in worker.py beat_schedule.
"""
import logging
from datetime import datetime, timedelta, timezone
from celery import shared_task
from sqlalchemy import or_

from app.db.session import SessionLocal
from app.models.models import User, Challenge, Video
from app.services.email_service import (
    send_challenge_announcement_batch,
    send_weekly_digest_email,
    send_monthly_stats_email,
)
from app.services.email_trigger import EmailTrigger

logger = logging.getLogger(__name__)


def _now():
    return datetime.now(timezone.utc)


def is_local_morning(user: User) -> bool:
    """
    Check if the current time is local morning (09:00 to 11:00) for the user.
    Estimates offset using user.country, falls back to UTC.
    """
    offsets = {
        "NG": 1, "NGR": 1, "NIGERIA": 1,
        "GB": 1, "UK": 1, "UNITED KINGDOM": 1,
        "US": -5, "USA": -5, "UNITED STATES": -5,
        "CA": -5, "CANADA": -5,
        "ZA": 2, "SOUTH AFRICA": 2,
        "IN": 5.5, "INDIA": 5.5,
    }
    country = (user.country or "").upper().strip()
    offset = offsets.get(country, 0)
    # Convert timezone offset float/int to hours
    local_hour = (datetime.now(timezone.utc) + timedelta(hours=offset)).hour
    return 9 <= local_hour <= 11



# ── Challenge announcement (existing, kept for compat) ──────────────────────

@shared_task(name="tasks.email.queue_new_challenge_announcement")
def queue_new_challenge_announcement(challenge_id: int):
    """Fetch opted-in users and BCC them a challenge announcement."""
    db = SessionLocal()
    try:
        challenge = db.query(Challenge).filter(Challenge.id == challenge_id).first()
        if not challenge:
            logger.error("Challenge %s not found.", challenge_id)
            return

        subscribers = db.query(User).filter(
            User.email_challenges == True,
            User.email.isnot(None),
        ).all()

        if not subscribers:
            logger.info("No opted-in users. Skipping challenge announcement.")
            return

        emails = [u.email for u in subscribers]
        end_date_str = challenge.end_date.strftime("%B %d, %Y") if challenge.end_date else "TBD"

        for i in range(0, len(emails), 50):
            batch = emails[i:i + 50]
            send_challenge_announcement_batch(
                bcc_emails=batch,
                title=challenge.title,
                prize=challenge.prize,
                end_date=end_date_str,
            )
        logger.info("Challenge '%s' announced to %d users.", challenge.title, len(emails))
    except Exception as e:
        logger.error("queue_new_challenge_announcement failed: %s", e)
    finally:
        db.close()


# ── Inactivity ladder (runs daily at 09:00 UTC) ──────────────────────────────

@shared_task(name="tasks.email.check_inactivity")
def check_inactivity():
    """
    Checks users at each inactivity threshold and queues re-engagement emails.
    Only one tier fires per user per run (the most urgent matching threshold).
    """
    db = SessionLocal()
    try:
        thresholds = [3, 7, 14, 30, 60, 90]
        trigger = EmailTrigger(db)

        for days in thresholds:
            window_end   = _now() - timedelta(days=days)
            window_start = window_end - timedelta(hours=23, minutes=59)

            users = (
                db.query(User)
                .filter(
                    User.is_active == True,
                    User.email_marketing == True,
                )
                .all()
            )

            sent = 0
            for user in users:
                if not is_local_morning(user):
                    continue
                last_active = getattr(user, "last_active", None) or user.created_at
                if last_active and hasattr(last_active, "replace"):
                    last_active = last_active.replace(tzinfo=timezone.utc)
                if last_active and window_start <= last_active <= window_end:
                    if trigger.reengagement(user, days):
                        sent += 1

            if sent:
                logger.info("Inactivity D%d: sent %d re-engagement emails.", days, sent)
    except Exception as e:
        logger.error("check_inactivity failed: %s", e)
    finally:
        db.close()


# ── Onboarding nudge check (runs hourly) ─────────────────────────────────────

@shared_task(name="tasks.email.check_onboarding_nudges")
def check_onboarding_nudges():
    """
    Fire D+3 and D+7 nudges for users who signed up exactly N days ago
    and still have no uploads.
    """
    db = SessionLocal()
    try:
        trigger = EmailTrigger(db)

        for days in [3, 7]:
            window_end   = _now() - timedelta(days=days)
            window_start = window_end - timedelta(hours=1)

            candidates = (
                db.query(User)
                .filter(
                    User.is_active == True,
                    User.created_at >= window_start,
                    User.created_at < window_end,
                    ~db.query(Video.id).filter(
                        Video.owner_id == User.id,
                        or_(Video.status.is_(None), Video.status != "failed"),
                    ).exists(),
                )
                .all()
            )

            sent = 0
            for user in candidates:
                if trigger.onboarding_nudge(user, days):
                    sent += 1

            if sent:
                logger.info("Onboarding D%d: sent %d nudge emails.", days, sent)
    except Exception as e:
        logger.error("check_onboarding_nudges failed: %s", e)
    finally:
        db.close()


# ── Weekly digest (runs Monday 09:00 UTC) ────────────────────────────────────

@shared_task(name="tasks.email.send_weekly_digest_batch")
def send_weekly_digest_batch():
    """
    Pull top 5 approved videos from last 7 days and email all opted-in users.
    Personalisation per user can be added in a later sprint.
    """
    db = SessionLocal()
    try:
        week_ago = _now() - timedelta(days=7)
        top_videos = (
            db.query(Video)
            .filter(
                Video.status == "approved",
                Video.created_at >= week_ago,
            )
            .order_by(Video.views.desc())
            .limit(5)
            .all()
        )

        if not top_videos:
            logger.info("No videos for weekly digest. Skipping.")
            return

        video_dicts = [
            {
                "title":   v.title,
                "creator": f"@{v.owner.username}" if v.owner else "",
                "url":     f"https://monteeq.com/watch/{v.public_id}",
            }
            for v in top_videos
        ]

        trigger = EmailTrigger(db)
        users = db.query(User).filter(
            User.is_active == True,
            User.email_weekly == True,
            User.email.isnot(None),
        ).all()

        sent = 0
        for user in users:
            if not is_local_morning(user):
                continue
            if trigger.weekly_digest(user, video_dicts):
                sent += 1

        logger.info("Weekly digest sent to %d users.", sent)
    except Exception as e:
        logger.error("send_weekly_digest_batch failed: %s", e)
    finally:
        db.close()


# ── Monthly stats (runs 1st of month, 10:00 UTC) ─────────────────────────────

@shared_task(name="tasks.email.send_monthly_stats_batch")
def send_monthly_stats_batch():
    """
    Compute last month's stats per creator and send individual recap emails.
    """
    from calendar import month_name
    from sqlalchemy import func as sqlfunc

    db = SessionLocal()
    try:
        now = _now()
        last_month_start = (now.replace(day=1) - timedelta(days=1)).replace(day=1, tzinfo=timezone.utc)
        last_month_end   = now.replace(day=1, tzinfo=timezone.utc)
        month_label      = month_name[last_month_start.month]

        creators = db.query(User).filter(
            User.is_active == True,
            User.email_weekly == True,
            db.query(Video.id).filter(
                Video.owner_id == User.id,
                or_(Video.status.is_(None), Video.status != "failed"),
            ).exists(),
        ).all()

        trigger = EmailTrigger(db)
        sent = 0
        for user in creators:
            # Aggregate stats for this user's videos in last month
            from app.models.models import View, Like
            views = db.query(sqlfunc.count(View.id)).filter(
                View.user_id == user.id,
                View.created_at >= last_month_start,
                View.created_at < last_month_end,
            ).scalar() or 0

            likes = db.query(sqlfunc.count(Like.id)).join(Video).filter(
                Video.owner_id == user.id,
                Like.created_at >= last_month_start,
                Like.created_at < last_month_end,
            ).scalar() or 0

            from app.models.models import Follow
            new_followers = db.query(sqlfunc.count(Follow.id)).filter(
                Follow.following_id == user.id,
                Follow.created_at >= last_month_start,
                Follow.created_at < last_month_end,
            ).scalar() or 0

            new_uploads = db.query(sqlfunc.count(Video.id)).filter(
                Video.owner_id == user.id,
                Video.created_at >= last_month_start,
                Video.created_at < last_month_end,
            ).scalar() or 0

            stats = {
                "views":     views,
                "likes":     likes,
                "followers": new_followers,
                "uploads":   new_uploads,
            }

            if trigger.monthly_stats(user, month_label, stats):
                sent += 1

        logger.info("Monthly stats sent to %d creators.", sent)
    except Exception as e:
        logger.error("send_monthly_stats_batch failed: %s", e)
    finally:
        db.close()


# ── Creator growth drip (runs Tuesday 10:00 UTC) ──────────────────────────────

@shared_task(name="tasks.email.send_growth_drip")
def send_growth_drip():
    """
    Send weekly creator tips to users who uploaded at least one video,
    calculated based on the week number of their account age (Week 1 to 5).
    """
    db = SessionLocal()
    try:
        trigger = EmailTrigger(db)
        # Find active users with at least 1 upload
        users = db.query(User).filter(
            User.is_active == True,
            User.email_marketing == True,
            db.query(Video.id).filter(
                Video.owner_id == User.id,
                or_(Video.status.is_(None), Video.status != "failed"),
            ).exists(),
        ).all()

        sent = 0
        for user in users:
            if not is_local_morning(user):
                continue
            age_days = (_now() - user.created_at.replace(tzinfo=timezone.utc)).days
            week = (age_days // 7) + 1
            if 1 <= week <= 5:
                if trigger.growth_drip(user, week):
                    sent += 1
        logger.info("Growth drip sent to %d users.", sent)
    except Exception as e:
        logger.error("send_growth_drip failed: %s", e)
    finally:
        db.close()


# ── Social notifications batching (runs every 4 hours or daily) ───────────────

@shared_task(name="tasks.email.flush_social_batch")
def flush_social_batch():
    """
    Gather likes, follows, and comments from the last 24 hours,
    and send a summarized social activity batch email.
    """
    from sqlalchemy import func as sqlfunc
    from app.models.models import Like, Follow, Comment

    db = SessionLocal()
    try:
        trigger = EmailTrigger(db)
        since = _now() - timedelta(hours=24)

        # Find users who have received actions
        users = db.query(User).filter(User.is_active == True).all()
        sent = 0

        for user in users:
            # Likes received on user's videos
            likes_count = db.query(sqlfunc.count(Like.id)).join(Video).filter(
                Video.owner_id == user.id,
                Like.created_at >= since,
            ).scalar() or 0

            # Follows received
            follows_count = db.query(sqlfunc.count(Follow.id)).filter(
                Follow.following_id == user.id,
                Follow.created_at >= since,
            ).scalar() or 0

            # Comments received
            comments_count = db.query(sqlfunc.count(Comment.id)).join(Video).filter(
                Video.owner_id == user.id,
                Comment.created_at >= since,
            ).scalar() or 0

            if likes_count == 0 and follows_count == 0 and comments_count == 0:
                continue

            # Construct simple recap summary
            parts = []
            if follows_count > 0:
                parts.append(f"- {follows_count} new follower{'s' if follows_count > 1 else ''}")
            if likes_count > 0:
                parts.append(f"- {likes_count} new like{'s' if likes_count > 1 else ''} on your edits")
            if comments_count > 0:
                parts.append(f"- {comments_count} new comment{'s' if comments_count > 1 else ''} on your videos")

            summary_text = "\n".join(parts)

            if trigger.social_batch(user, summary_text):
                sent += 1

        logger.info("Social notification recap batch flushed to %d users.", sent)
    except Exception as e:
        logger.error("flush_social_batch failed: %s", e)
    finally:
        db.close()