File size: 16,109 Bytes
304bdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5480d48
 
304bdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5480d48
304bdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5480d48
 
 
 
 
 
 
 
304bdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5480d48
 
 
 
304bdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState, useTransition } from "react";
import { YoutubeVideo, DetectedObject, MarketplaceMatch } from "@/lib/db/schema";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle, CardFooter, CardDescription } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import {
    Play,
    Plus,
    CheckCircle2,
    XCircle,
    ArrowLeft,
    ChevronRight,
    Copy,
    ExternalLink,
    Video,
    Package,
    History,
    Save
} from "lucide-react";
import Link from "next/link";
import { CrossVaultSuggestions } from "./cross-vault-suggestions";
import { VideoEmbed } from "@/features/vault/components/video-embed";
import { AnalysisHistory } from "./analysis-history";
import {
    triggerAIAnalysisAction,
    approveDetectionAction,
    rejectDetectionAction,
    completeVideoReviewAction,
    addManualProductAction
} from "../actions/analysis";
import { toast } from "sonner";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
    Dialog,
    DialogContent,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
    DialogFooter
} from "@/components/ui/dialog";

type ExtendedVideo = YoutubeVideo & {
    channel?: { channelName: string };
    detections: (DetectedObject & { marketplaceMatches: MarketplaceMatch[] })[];
};

export function AnalysisInterface({ video }: { video: ExtendedVideo }) {
    const [isPending, startTransition] = useTransition();
    const [activeTab, setActiveTab] = useState("review");
    const [isAddProductOpen, setIsAddProductOpen] = useState(false);

    const handleTriggerAI = async () => {
        startTransition(async () => {
            const result = await triggerAIAnalysisAction(video.id);
            if (result.success) {
                toast.success("AI Analysis triggered");
            } else {
                toast.error("Failed to trigger analysis");
            }
        });
    };

    const handleApprove = async (id: string) => {
        startTransition(async () => {
            await approveDetectionAction(id);
            toast.success("Product approved");
        });
    };

    const handleReject = async (id: string) => {
        startTransition(async () => {
            await rejectDetectionAction(id);
            toast.success("Product rejected");
        });
    };

    const handleComplete = async () => {
        startTransition(async () => {
            await completeVideoReviewAction(video.id);
            toast.success("Video review completed");
        });
    };

    return (
        <div className="space-y-6">
            <div className="flex items-center justify-between">
                <div className="flex flex-col gap-1">
                    <div className="flex items-center gap-2 text-xs text-muted-foreground mb-1">
                        <Link href="/admin/dashboard" className="hover:text-primary transition-colors">Dashboard</Link>
                        <span>/</span>
                        <Link href="/admin/analysis-queue" className="hover:text-primary transition-colors">Video Hub</Link>
                    </div>
                    <div className="flex items-center gap-4">
                        <Button variant="ghost" size="icon" className="h-8 w-8" asChild>
                            <Link href="/admin/analysis-queue">
                                <ArrowLeft className="h-4 w-4" />
                            </Link>
                        </Button>
                        <div>
                            <h1 className="text-2xl font-bold line-clamp-1">{video.title}</h1>
                            <p className="text-sm text-muted-foreground">{video.channel?.channelName}</p>
                        </div>
                    </div>
                </div>
                <div className="flex items-center gap-2">
                    <Button
                        variant="secondary"
                        onClick={handleTriggerAI}
                        disabled={isPending || video.scanStatus === 'in_progress'}
                    >
                        <Play className="mr-2 h-4 w-4" />
                        {video.scanStatus === 'pending_analysis' ? 'Run AI Analysis' : 'Re-Run AI'}
                    </Button>
                    <Button
                        variant="default"
                        className="bg-emerald-600 hover:bg-emerald-700"
                        onClick={handleComplete}
                        disabled={isPending}
                    >
                        <CheckCircle2 className="mr-2 h-4 w-4" />
                        Mark as Done
                    </Button>
                </div>
            </div>

            <div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
                {/* Left Side: Video Player & Overview */}
                <div className="lg:col-span-2 space-y-6">
                    <VideoEmbed
                        videoId={video.videoId}
                        title={video.title}
                        platform={video.platform as any}
                        url={video.url}
                        width={video.width}
                        height={video.height}
                    />

                    <Tabs value={activeTab} onValueChange={setActiveTab}>
                        <TabsList className="grid w-full grid-cols-3">
                            <TabsTrigger value="review">Review Objects</TabsTrigger>
                            <TabsTrigger value="suggestions">Cross-Vault suggestions</TabsTrigger>
                            <TabsTrigger value="history">History</TabsTrigger>
                        </TabsList>

                        <TabsContent value="review" className="space-y-4 pt-4">
                            <div className="flex items-center justify-between">
                                <h3 className="text-lg font-semibold">Detected Objects ({video.detections.length})</h3>
                                <Dialog open={isAddProductOpen} onOpenChange={setIsAddProductOpen}>
                                    <DialogTrigger asChild>
                                        <Button size="sm">
                                            <Plus className="h-4 w-4 mr-2" />
                                            Add Manual Product
                                        </Button>
                                    </DialogTrigger>
                                    <AddProductDialog
                                        videoId={video.id}
                                        onSuccess={() => setIsAddProductOpen(false)}
                                    />
                                </Dialog>
                            </div>

                            <div className="grid gap-4">
                                {video.detections.map((obj) => (
                                    <Card key={obj.id} className="bg-card/40 backdrop-blur-md border-border/40">
                                        <div className="p-4 flex gap-4">
                                            <div className="w-24 h-24 bg-muted rounded-md overflow-hidden flex-shrink-0">
                                                {obj.thumbnailUrl ? (
                                                    <img src={obj.thumbnailUrl} className="w-full h-full object-cover" />
                                                ) : (
                                                    <Package className="w-full h-full p-6 text-muted-foreground opacity-20" />
                                                )}
                                            </div>
                                            <div className="flex-1 flex flex-col gap-1">
                                                <div className="flex items-center justify-between">
                                                    <h4 className="font-semibold">{obj.objectName}</h4>
                                                    <Badge variant={obj.moderationStatus === 'APPROVED' ? 'default' : obj.moderationStatus === 'REJECTED' ? 'destructive' : 'outline'}>
                                                        {obj.moderationStatus}
                                                    </Badge>
                                                </div>
                                                <p className="text-xs text-muted-foreground">Category: {obj.category}</p>

                                                {obj.marketplaceMatches?.[0] && (
                                                    <div className="mt-2 text-sm flex items-center gap-2 text-emerald-500">
                                                        <ExternalLink className="h-3 w-3" />
                                                        <span className="line-clamp-1">{obj.marketplaceMatches[0].affiliateUrl}</span>
                                                    </div>
                                                )}

                                                <div className="mt-4 flex items-center gap-2">
                                                    <Button
                                                        size="sm"
                                                        variant="outline"
                                                        className="h-8 border-emerald-500/50 text-emerald-500 hover:bg-emerald-500/10"
                                                        onClick={() => handleApprove(obj.id)}
                                                        disabled={isPending || obj.moderationStatus === 'APPROVED'}
                                                    >
                                                        Approve
                                                    </Button>
                                                    <Button
                                                        size="sm"
                                                        variant="outline"
                                                        className="h-8 border-rose-500/50 text-rose-500 hover:bg-rose-500/10"
                                                        onClick={() => handleReject(obj.id)}
                                                        disabled={isPending || obj.moderationStatus === 'REJECTED'}
                                                    >
                                                        Reject
                                                    </Button>
                                                </div>
                                            </div>
                                        </div>
                                    </Card>
                                ))}
                            </div>
                        </TabsContent>

                        <TabsContent value="suggestions" className="pt-4">
                            <CrossVaultSuggestions videoId={video.videoId} videoInternalId={video.id} />
                        </TabsContent>

                        <TabsContent value="history" className="pt-4">
                            <AnalysisHistory videoInternalId={video.id} />
                        </TabsContent>
                    </Tabs>
                </div>

                {/* Right Side: Metadata & Status */}
                <div className="space-y-6">
                    <Card>
                        <CardHeader>
                            <CardTitle>Video Details</CardTitle>
                        </CardHeader>
                        <CardContent className="space-y-4">
                            <div className="space-y-1">
                                <Label className="text-xs text-muted-foreground">Platform</Label>
                                <p className="text-sm font-medium uppercase">{video.platform}</p>
                            </div>
                            <div className="space-y-1">
                                <Label className="text-xs text-muted-foreground">Status</Label>
                                <p className="text-sm font-medium capitalize">{video.scanStatus.replace('_', ' ')}</p>
                            </div>
                            <div className="space-y-1">
                                <Label className="text-xs text-muted-foreground">Video URL</Label>
                                <div className="flex items-center gap-2">
                                    <Input value={video.url || ''} readOnly className="h-8 text-xs" />
                                    <Button variant="ghost" size="icon" className="h-8 w-8">
                                        <ExternalLink className="h-4 w-4" />
                                    </Button>
                                </div>
                            </div>
                        </CardContent>
                    </Card>

                    <Card className="bg-primary/5 border-primary/20">
                        <CardHeader>
                            <CardTitle className="text-sm">Admin Instructions</CardTitle>
                        </CardHeader>
                        <CardContent className="text-xs space-y-2 text-muted-foreground">
                            <p>1. Run AI Analysis if not already done.</p>
                            <p>2. Approve items that are correctly identified.</p>
                            <p>3. Add any missing products manually.</p>
                            <p>4. Use the Suggestions tab to reuse products from other creators who shared this video.</p>
                            <p>5. Mark as Done to notify the creator.</p>
                        </CardContent>
                    </Card>
                </div>
            </div>
        </div>
    );
}

function AddProductDialog({ videoId, onSuccess }: { videoId: string, onSuccess: () => void }) {
    const [isPending, startTransition] = useTransition();
    const [name, setName] = useState("");
    const [category, setCategory] = useState("Apparel");
    const [url, setUrl] = useState("");

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault();
        startTransition(async () => {
            const result = await addManualProductAction(videoId, {
                objectName: name,
                category: category as any,
                affiliateUrl: url
            });
            if (result.success) {
                toast.success("Product added manually");
                onSuccess();
            } else {
                toast.error("Failed to add product");
            }
        });
    };

    return (
        <DialogContent>
            <form onSubmit={handleSubmit}>
                <DialogHeader>
                    <DialogTitle>Add Manual Product</DialogTitle>
                </DialogHeader>
                <div className="grid gap-4 py-4">
                    <div className="grid gap-2">
                        <Label htmlFor="name">Product Name</Label>
                        <Input id="name" value={name} onChange={e => setName(e.target.value)} placeholder="e.g. Sony WH-1000XM4" required />
                    </div>
                    <div className="grid gap-2">
                        <Label htmlFor="category">Category</Label>
                        <Input id="category" value={category} onChange={e => setCategory(e.target.value)} required />
                    </div>
                    <div className="grid gap-2">
                        <Label htmlFor="url">Affiliate/Product URL</Label>
                        <Input id="url" value={url} onChange={e => setUrl(e.target.value)} placeholder="https://amazon.com/..." />
                    </div>
                </div>
                <DialogFooter>
                    <Button type="submit" disabled={isPending}>
                        {isPending ? "Adding..." : "Add Product"}
                    </Button>
                </DialogFooter>
            </form>
        </DialogContent>
    );
}