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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"use client";

import React, { useState } from "react";
import { useRouter } from "next/navigation";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/Card";
import { Button } from "@/components/ui/Button";
import { Select } from "@/components/ui/Select";
import { ProgressBar } from "@/components/ui/ProgressBar";
import { generateTestingMatrix, generateMatrixAd } from "@/lib/api/endpoints";
import { exportAsJSON, exportAsCSV } from "@/lib/utils/export";
import { toast } from "react-hot-toast";
import { Download, FileJson, FileSpreadsheet, Rocket, CheckSquare, Square } from "lucide-react";
import { IMAGE_MODELS } from "@/lib/constants/models";
import type { TestingMatrixResponse, CombinationInfo } from "@/types/api";
import type { Niche } from "@/types/api";

interface TestingMatrixBuilderProps {
  onMatrixGenerated?: (matrix: TestingMatrixResponse) => void;
}

export const TestingMatrixBuilder: React.FC<TestingMatrixBuilderProps> = ({
  onMatrixGenerated,
}) => {
  const router = useRouter();
  const [niche, setNiche] = useState<Niche>("home_insurance");
  const [strategy, setStrategy] = useState<"balanced" | "top_performers" | "diverse">("balanced");
  const [angleCount, setAngleCount] = useState(6);
  const [conceptCount, setConceptCount] = useState(5);
  const [matrix, setMatrix] = useState<TestingMatrixResponse | null>(null);
  const [isGenerating, setIsGenerating] = useState(false);
  const [selectedCombinations, setSelectedCombinations] = useState<Set<string>>(new Set());
  const [isGeneratingAds, setIsGeneratingAds] = useState(false);
  const [generationProgress, setGenerationProgress] = useState({ current: 0, total: 0 });
  const [numVariations, setNumVariations] = useState(1);
  const [imageModel, setImageModel] = useState<string | null>(null);

  const handleGenerate = async () => {
    setIsGenerating(true);
    try {
      const result = await generateTestingMatrix({
        niche,
        angle_count: angleCount,
        concept_count: conceptCount,
        strategy,
      });
      setMatrix(result);
      onMatrixGenerated?.(result);
      toast.success(`Generated ${result.summary.total_combinations} combinations!`);
    } catch (error: any) {
      toast.error(error.message || "Failed to generate testing matrix");
    } finally {
      setIsGenerating(false);
    }
  };

  const handleExportJSON = () => {
    if (!matrix) return;
    exportAsJSON(matrix, `testing-matrix-${niche}-${Date.now()}.json`);
    toast.success("Matrix exported as JSON");
  };

  const handleExportCSV = () => {
    if (!matrix) return;
    const csvData = matrix.combinations.map((combo) => ({
      combination_id: combo.combination_id,
      angle_key: combo.angle.key,
      angle_name: combo.angle.name,
      angle_trigger: combo.angle.trigger,
      concept_key: combo.concept.key,
      concept_name: combo.concept.name,
      compatibility_score: combo.compatibility_score,
    }));
    exportAsCSV(csvData, `testing-matrix-${niche}-${Date.now()}.csv`);
    toast.success("Matrix exported as CSV");
  };

  const toggleCombination = (combinationId: string) => {
    const newSelected = new Set(selectedCombinations);
    if (newSelected.has(combinationId)) {
      newSelected.delete(combinationId);
    } else {
      newSelected.add(combinationId);
    }
    setSelectedCombinations(newSelected);
  };

  const selectAll = () => {
    if (!matrix) return;
    if (selectedCombinations.size === matrix.combinations.length) {
      setSelectedCombinations(new Set());
    } else {
      setSelectedCombinations(new Set(matrix.combinations.map(c => c.combination_id)));
    }
  };

  const handleGenerateAds = async () => {
    if (!matrix) return;
    
    const combinationsToGenerate = selectedCombinations.size > 0
      ? matrix.combinations.filter(c => selectedCombinations.has(c.combination_id))
      : matrix.combinations;

    if (combinationsToGenerate.length === 0) {
      toast.error("Please select at least one combination");
      return;
    }

    setIsGeneratingAds(true);
    setGenerationProgress({ current: 0, total: combinationsToGenerate.length });

    try {
      const generatedAds = [];
      
      for (let i = 0; i < combinationsToGenerate.length; i++) {
        const combo = combinationsToGenerate[i];
        setGenerationProgress({ current: i + 1, total: combinationsToGenerate.length });
        
        try {
          const result = await generateMatrixAd({
            niche,
            angle_key: combo.angle.key,
            concept_key: combo.concept.key,
            num_images: numVariations,
            image_model: imageModel,
          });
          generatedAds.push(result);
          toast.success(`Generated ad ${i + 1}/${combinationsToGenerate.length}`);
        } catch (error: any) {
          console.error(`Failed to generate ad for combination ${combo.combination_id}:`, error);
          toast.error(`Failed to generate ad ${i + 1}/${combinationsToGenerate.length}`);
        }
      }

      toast.success(`Successfully generated ${generatedAds.length} ads!`);
      
      // Navigate to gallery to see the results
      setTimeout(() => {
        router.push("/gallery");
      }, 1500);
    } catch (error: any) {
      toast.error(error.message || "Failed to generate ads");
    } finally {
      setIsGeneratingAds(false);
      setGenerationProgress({ current: 0, total: 0 });
    }
  };

  return (
    <div className="space-y-6">
      <Card variant="glass">
        <CardHeader>
          <CardTitle>Testing Matrix Builder</CardTitle>
          <CardDescription>
            Generate a systematic testing matrix for ad optimization
          </CardDescription>
        </CardHeader>
        <CardContent className="space-y-6">
          <Select
            label="Niche"
            options={[
              { value: "home_insurance", label: "Home Insurance" },
              { value: "glp1", label: "GLP-1" },
            ]}
            value={niche}
            onChange={(e) => setNiche(e.target.value as Niche)}
          />

          <Select
            label="Strategy"
            options={[
              { value: "balanced", label: "Balanced - Mix of top performers and diverse" },
              { value: "top_performers", label: "Top Performers - Focus on proven winners" },
              { value: "diverse", label: "Diverse - Maximum variety" },
            ]}
            value={strategy}
            onChange={(e) => setStrategy(e.target.value as typeof strategy)}
          />

          <div>
            <label className="block text-sm font-medium text-gray-700 mb-2">
              Number of Angles: {angleCount}
            </label>
            <input
              type="range"
              min="1"
              max="10"
              step="1"
              className="w-full"
              value={angleCount}
              onChange={(e) => setAngleCount(Number(e.target.value))}
            />
            <div className="flex justify-between text-xs text-gray-500 mt-1">
              <span>1</span>
              <span>10</span>
            </div>
          </div>

          <div>
            <label className="block text-sm font-medium text-gray-700 mb-2">
              Concepts per Angle: {conceptCount}
            </label>
            <input
              type="range"
              min="1"
              max="10"
              step="1"
              className="w-full"
              value={conceptCount}
              onChange={(e) => setConceptCount(Number(e.target.value))}
            />
            <div className="flex justify-between text-xs text-gray-500 mt-1">
              <span>1</span>
              <span>10</span>
            </div>
          </div>

          <div className="bg-blue-50 border border-blue-200 rounded-lg p-4">
            <p className="text-sm text-blue-800">
              <strong>Total Combinations:</strong> {angleCount} × {conceptCount} = {angleCount * conceptCount}
            </p>
          </div>

          <Button
            variant="primary"
            size="lg"
            className="w-full"
            onClick={handleGenerate}
            isLoading={isGenerating}
          >
            Generate Testing Matrix
          </Button>
        </CardContent>
      </Card>

      {matrix && (
        <>
          <Card variant="glass">
            <CardHeader>
              <div className="flex items-center justify-between">
                <div>
                  <CardTitle>Matrix Summary</CardTitle>
                  <CardDescription>
                    {matrix.summary.total_combinations} combinations ready for testing
                  </CardDescription>
                </div>
                <div className="flex space-x-2">
                  <Button variant="outline" size="sm" onClick={handleExportJSON}>
                    <FileJson className="h-4 w-4 mr-1" />
                    JSON
                  </Button>
                  <Button variant="outline" size="sm" onClick={handleExportCSV}>
                    <FileSpreadsheet className="h-4 w-4 mr-1" />
                    CSV
                  </Button>
                </div>
              </div>
            </CardHeader>
            <CardContent>
              <div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
                <div>
                  <p className="text-sm text-gray-600">Total Combinations</p>
                  <p className="text-2xl font-bold text-gray-900">{matrix.summary.total_combinations}</p>
                </div>
                <div>
                  <p className="text-sm text-gray-600">Unique Angles</p>
                  <p className="text-2xl font-bold text-gray-900">{matrix.summary.unique_angles}</p>
                </div>
                <div>
                  <p className="text-sm text-gray-600">Unique Concepts</p>
                  <p className="text-2xl font-bold text-gray-900">{matrix.summary.unique_concepts}</p>
                </div>
                <div>
                  <p className="text-sm text-gray-600">Avg Compatibility</p>
                  <p className="text-2xl font-bold text-gray-900">
                    {(matrix.summary.average_compatibility * 100).toFixed(0)}%
                  </p>
                </div>
              </div>

              {/* Generate Ads Section */}
              <div className="bg-gradient-to-r from-blue-50 to-cyan-50 border border-blue-200 rounded-xl p-6 mb-6">
                <div className="flex items-center justify-between mb-4">
                  <div>
                    <h3 className="text-lg font-bold text-gray-900 mb-1">Generate Ads from Matrix</h3>
                    <p className="text-sm text-gray-600">
                      Select combinations to generate ads, or generate all
                    </p>
                  </div>
                </div>
                
                <div className="mb-4">
                  <label className="block text-sm font-medium text-gray-700 mb-2">
                    Variations per Combination: {numVariations}
                  </label>
                  <input
                    type="range"
                    min="1"
                    max="3"
                    step="1"
                    className="w-full accent-blue-500"
                    value={numVariations}
                    onChange={(e) => setNumVariations(Number(e.target.value))}
                  />
                  <div className="flex justify-between text-xs text-gray-500 mt-1">
                    <span>1</span>
                    <span>3</span>
                  </div>
                </div>

                <div className="mb-4">
                  <Select
                    label="Image Model"
                    options={IMAGE_MODELS.map(model => ({ value: model.value, label: model.label }))}
                    value={imageModel || ""}
                    onChange={(e) => setImageModel(e.target.value || null)}
                  />
                </div>

                <div className="flex items-center justify-between mb-4">
                  <div className="flex items-center space-x-2">
                    <Button
                      variant="outline"
                      size="sm"
                      onClick={selectAll}
                    >
                      {selectedCombinations.size === matrix.combinations.length ? (
                        <>
                          <CheckSquare className="h-4 w-4 mr-1" />
                          Deselect All
                        </>
                      ) : (
                        <>
                          <Square className="h-4 w-4 mr-1" />
                          Select All
                        </>
                      )}
                    </Button>
                    <span className="text-sm text-gray-600">
                      {selectedCombinations.size > 0 
                        ? `${selectedCombinations.size} selected`
                        : "All combinations will be generated"}
                    </span>
                  </div>
                  <Button
                    variant="primary"
                    size="lg"
                    onClick={handleGenerateAds}
                    isLoading={isGeneratingAds}
                    disabled={isGeneratingAds}
                  >
                    <Rocket className="h-5 w-5 mr-2" />
                    Generate Ads
                  </Button>
                </div>

                {isGeneratingAds && generationProgress.total > 0 && (
                  <div className="mt-4">
                    <div className="flex items-center justify-between mb-2">
                      <span className="text-sm font-medium text-gray-700">
                        Generating ads...
                      </span>
                      <span className="text-sm text-gray-600">
                        {generationProgress.current} / {generationProgress.total}
                      </span>
                    </div>
                    <ProgressBar
                      progress={(generationProgress.current / generationProgress.total) * 100}
                    />
                  </div>
                )}

                <div className="bg-blue-100 border border-blue-300 rounded-lg p-3 mt-4">
                  <p className="text-xs text-blue-800">
                    <strong>Note:</strong> This will generate{" "}
                    {selectedCombinations.size > 0
                      ? `${selectedCombinations.size} × ${numVariations} = ${selectedCombinations.size * numVariations}`
                      : `${matrix.combinations.length} × ${numVariations} = ${matrix.combinations.length * numVariations}`}{" "}
                    total ad variations. This may take several minutes.
                  </p>
                </div>
              </div>

              {/* Combinations List */}
              <div className="max-h-96 overflow-y-auto space-y-2">
                {matrix.combinations.map((combo) => {
                  const isSelected = selectedCombinations.has(combo.combination_id);
                  return (
                    <div
                      key={combo.combination_id}
                      onClick={() => toggleCombination(combo.combination_id)}
                      className={`p-3 border rounded-lg cursor-pointer transition-all ${
                        isSelected
                          ? "border-blue-500 bg-blue-50 shadow-md"
                          : "border-gray-200 hover:bg-gray-50 hover:border-gray-300"
                      }`}
                    >
                      <div className="flex items-start justify-between">
                        <div className="flex items-start space-x-3 flex-1">
                          <div className="mt-0.5">
                            {isSelected ? (
                              <CheckSquare className="h-5 w-5 text-blue-600" />
                            ) : (
                              <Square className="h-5 w-5 text-gray-400" />
                            )}
                          </div>
                          <div className="flex-1">
                            <div className="flex items-center space-x-2 mb-1">
                              <span className="text-sm font-semibold text-gray-900">
                                {combo.angle.name}
                              </span>
                              <span className="text-gray-400">×</span>
                              <span className="text-sm font-semibold text-gray-900">
                                {combo.concept.name}
                              </span>
                            </div>
                            <p className="text-xs text-gray-500">{combo.angle.trigger}</p>
                            <p className="text-xs text-gray-500 mt-1">{combo.concept.structure}</p>
                          </div>
                        </div>
                        <div className="ml-4 text-right">
                          <span className="text-xs font-medium text-blue-600">
                            {(combo.compatibility_score * 100).toFixed(0)}%
                          </span>
                        </div>
                      </div>
                    </div>
                  );
                })}
              </div>
            </CardContent>
          </Card>
        </>
      )}
    </div>
  );
};