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
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
402
403
404
405
406
407
408
409
410
411
"use client"

import React, { useState, useEffect } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { X, Bell, Clock, CheckCircle, AlertCircle } from "lucide-react"

const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"

interface NotificationSubscriptionDialogProps {
  isOpen: boolean
  onClose: () => void
  phoneNumber?: string
}

interface Source {
  name: string
  article_count: number
}

interface Subscription {
  id: number
  phone_number: string
  notification_type: "whatsapp" | "sms" | "both"
  schedule_type: "immediate" | "daily_digest"
  digest_time: string | null
  categories: string[] | null
  sources: string[] | null
  is_active: boolean
  created_at: string
  updated_at: string
}

export default function NotificationSubscriptionDialog({

  isOpen,

  onClose,

  phoneNumber: initialPhoneNumber = ""

}: NotificationSubscriptionDialogProps) {
  const [phoneNumber, setPhoneNumber] = useState(initialPhoneNumber)
  const [notificationType, setNotificationType] = useState<"whatsapp" | "sms" | "both">("whatsapp")
  const [scheduleType, setScheduleType] = useState<"immediate" | "daily_digest">("immediate")
  const [digestTime, setDigestTime] = useState("08:00")
  const [selectedCategories, setSelectedCategories] = useState<string[]>([])
  const [selectedSources, setSelectedSources] = useState<string[]>([])
  const [categories, setCategories] = useState<string[]>([])
  const [sources, setSources] = useState<Source[]>([])
  const [loading, setLoading] = useState(false)
  const [submitting, setSubmitting] = useState(false)
  const [message, setMessage] = useState<{ type: "success" | "error"; text: string } | null>(null)
  const [existingSubscription, setExistingSubscription] = useState<Subscription | null>(null)

  useEffect(() => {
    if (isOpen) {
      fetchCategories()
      fetchSources()
      if (phoneNumber) {
        checkSubscription()
      }
    }
  }, [isOpen])

  // Debounced check for subscription when phone number changes
  useEffect(() => {
    if (!isOpen || !phoneNumber || phoneNumber.length < 10) return

    const timeoutId = setTimeout(() => {
      checkSubscription()
    }, 500)

    return () => clearTimeout(timeoutId)
  }, [phoneNumber, isOpen])

  const fetchCategories = async () => {
    try {
      const response = await fetch(`${API_BASE_URL}/api/v1/notifications/categories`)
      if (response.ok) {
        const data = await response.json()
        setCategories(data.categories || [])
      }
    } catch (error) {
      console.error("Failed to fetch categories:", error)
    }
  }

  const fetchSources = async () => {
    try {
      const response = await fetch(`${API_BASE_URL}/api/v1/notifications/sources`)
      if (response.ok) {
        const data = await response.json()
        setSources(data.sources || [])
      }
    } catch (error) {
      console.error("Failed to fetch sources:", error)
    }
  }

  const checkSubscription = async () => {
    if (!phoneNumber) return
    setLoading(true)
    try {
      const response = await fetch(`${API_BASE_URL}/api/v1/notifications/subscription/${encodeURIComponent(phoneNumber)}`)
      if (response.ok) {
        const data = await response.json()
        setExistingSubscription(data)
        setNotificationType(data.notification_type || "whatsapp")
        setScheduleType(data.schedule_type || "immediate")
        setDigestTime(data.digest_time || "08:00")
        setSelectedCategories(data.categories || [])
        setSelectedSources(data.sources || [])
      } else if (response.status === 404) {
        setExistingSubscription(null)
      }
    } catch (error) {
      console.error("Failed to check subscription:", error)
    } finally {
      setLoading(false)
    }
  }

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    if (!phoneNumber.trim()) {
      setMessage({ type: "error", text: "Please enter a phone number" })
      return
    }

    setSubmitting(true)
    setMessage(null)

    try {
      const payload = {
        phone_number: phoneNumber.trim(),
        notification_type: notificationType,
        schedule_type: scheduleType,
        digest_time: scheduleType === "daily_digest" ? digestTime : null,
        categories: selectedCategories.length > 0 ? selectedCategories : null,
        sources: selectedSources.length > 0 ? selectedSources : null,
      }

      const response = await fetch(`${API_BASE_URL}/api/v1/notifications/subscribe`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(payload),
      })

      if (response.ok) {
        setMessage({ type: "success", text: "Successfully subscribed to news notifications!" })
        setTimeout(() => {
          onClose()
          setMessage(null)
        }, 2000)
      } else {
        const error = await response.json()
        setMessage({ type: "error", text: error.detail || "Failed to subscribe" })
      }
    } catch (error) {
      setMessage({ type: "error", text: "Failed to subscribe. Please try again." })
    } finally {
      setSubmitting(false)
    }
  }

  const handleUnsubscribe = async () => {
    if (!phoneNumber.trim()) return

    setSubmitting(true)
    setMessage(null)

    try {
      const response = await fetch(`${API_BASE_URL}/api/v1/notifications/unsubscribe?phone_number=${encodeURIComponent(phoneNumber.trim())}`, {
        method: "POST",
      })

      if (response.ok) {
        setMessage({ type: "success", text: "Successfully unsubscribed" })
        setExistingSubscription(null)
        setTimeout(() => {
          onClose()
          setMessage(null)
        }, 2000)
      } else {
        const error = await response.json()
        setMessage({ type: "error", text: error.detail || "Failed to unsubscribe" })
      }
    } catch (error) {
      setMessage({ type: "error", text: "Failed to unsubscribe. Please try again." })
    } finally {
      setSubmitting(false)
    }
  }

  const toggleCategory = (category: string) => {
    setSelectedCategories((prev) =>
      prev.includes(category) ? prev.filter((c) => c !== category) : [...prev, category]
    )
  }

  const toggleSource = (source: string) => {
    setSelectedSources((prev) =>
      prev.includes(source) ? prev.filter((s) => s !== source) : [...prev, source]
    )
  }

  if (!isOpen) return null

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">

      <Card className="w-full max-w-2xl max-h-[90vh] overflow-y-auto m-4">

        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-4">

          <CardTitle className="flex items-center text-xl">

            <Bell className="w-5 h-5 mr-2" />

            News Notifications

          </CardTitle>

          <Button variant="ghost" size="sm" onClick={onClose}>

            <X className="w-4 h-4" />

          </Button>

        </CardHeader>

        <CardContent>

          <form onSubmit={handleSubmit} className="space-y-6">

            {/* Phone Number */}

            <div>

              <label className="text-sm font-medium mb-2 block">Phone Number</label>

              <Input

                type="tel"

                placeholder="e.g., 0712345678 or +254712345678"

                value={phoneNumber}

                onChange={(e) => setPhoneNumber(e.target.value)}

                disabled={submitting || loading}

                required

              />

              {phoneNumber && (

                <Button

                  type="button"

                  variant="outline"

                  size="sm"

                  className="mt-2"

                  onClick={checkSubscription}

                  disabled={loading}

                >

                  {loading ? "Checking..." : "Check Subscription"}

                </Button>

              )}

            </div>



            {/* Notification Type */}

            <div>

              <label className="text-sm font-medium mb-2 block">Notification Type</label>

              <div className="flex gap-2">

                <Button

                  type="button"

                  variant={notificationType === "whatsapp" ? "default" : "outline"}

                  size="sm"

                  onClick={() => setNotificationType("whatsapp")}

                  disabled={submitting}

                >

                  WhatsApp

                </Button>

                <Button

                  type="button"

                  variant={notificationType === "sms" ? "default" : "outline"}

                  size="sm"

                  onClick={() => setNotificationType("sms")}

                  disabled={submitting}

                >

                  SMS

                </Button>

                <Button

                  type="button"

                  variant={notificationType === "both" ? "default" : "outline"}

                  size="sm"

                  onClick={() => setNotificationType("both")}

                  disabled={submitting}

                >

                  Both

                </Button>

              </div>

              <p className="text-xs text-muted-foreground mt-1">

                WhatsApp is default, SMS will be used as fallback

              </p>

            </div>



            {/* Schedule Type */}

            <div>

              <label className="text-sm font-medium mb-2 block">Schedule</label>

              <div className="flex gap-2">

                <Button

                  type="button"

                  variant={scheduleType === "immediate" ? "default" : "outline"}

                  size="sm"

                  onClick={() => setScheduleType("immediate")}

                  disabled={submitting}

                >

                  <Clock className="w-4 h-4 mr-1" />

                  Immediate

                </Button>

                <Button

                  type="button"

                  variant={scheduleType === "daily_digest" ? "default" : "outline"}

                  size="sm"

                  onClick={() => setScheduleType("daily_digest")}

                  disabled={submitting}

                >

                  Daily Digest

                </Button>

              </div>

              {scheduleType === "daily_digest" && (

                <div className="mt-2">

                  <Input

                    type="time"

                    value={digestTime}

                    onChange={(e) => setDigestTime(e.target.value)}

                    disabled={submitting}

                    className="w-32"

                  />

                </div>

              )}

            </div>



            {/* Categories Filter */}

            <div>

              <label className="text-sm font-medium mb-2 block">

                Categories (Optional - leave empty for all)

              </label>

              <div className="flex flex-wrap gap-2 max-h-32 overflow-y-auto p-2 border rounded">

                {categories.length > 0 ? (

                  categories.map((category) => (

                    <Badge

                      key={category}

                      variant={selectedCategories.includes(category) ? "default" : "outline"}

                      className="cursor-pointer"

                      onClick={() => toggleCategory(category)}

                    >

                      {category}

                    </Badge>

                  ))

                ) : (

                  <span className="text-sm text-muted-foreground">Loading categories...</span>

                )}

              </div>

            </div>



            {/* Sources Filter */}

            <div>

              <label className="text-sm font-medium mb-2 block">

                Sources (Optional - leave empty for all)

              </label>

              <div className="flex flex-wrap gap-2 max-h-32 overflow-y-auto p-2 border rounded">

                {sources.length > 0 ? (

                  sources.map((source) => (

                    <Badge

                      key={source.name}

                      variant={selectedSources.includes(source.name) ? "default" : "outline"}

                      className="cursor-pointer"

                      onClick={() => toggleSource(source.name)}

                    >

                      {source.name}

                    </Badge>

                  ))

                ) : (

                  <span className="text-sm text-muted-foreground">Loading sources...</span>

                )}

              </div>

            </div>



            {/* Message */}

            {message && (

              <div

                className={`flex items-center gap-2 p-3 rounded ${

                  message.type === "success" ? "bg-green-50 text-green-800" : "bg-red-50 text-red-800"

                }`}

              >

                {message.type === "success" ? (

                  <CheckCircle className="w-4 h-4" />

                ) : (

                  <AlertCircle className="w-4 h-4" />

                )}

                <span className="text-sm">{message.text}</span>

              </div>

            )}



            {/* Actions */}

            <div className="flex gap-2 justify-end">

              {existingSubscription && (

                <Button

                  type="button"

                  variant="destructive"

                  onClick={handleUnsubscribe}

                  disabled={submitting}

                >

                  Unsubscribe

                </Button>

              )}

              <Button type="button" variant="outline" onClick={onClose} disabled={submitting}>

                Cancel

              </Button>

              <Button type="submit" disabled={submitting}>

                {existingSubscription ? "Update Subscription" : "Subscribe"}

              </Button>

            </div>

          </form>

        </CardContent>

      </Card>

    </div>
  )
}