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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
"""
AI Tool Hub - Main Application
Integrates all components into a single FastAPI application
"""
import os
import json
import uuid
import secrets
import logging
from datetime import datetime, timedelta
from typing import Dict, Any, List, Optional, Union

# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv()

from fastapi import FastAPI, Request, Response, Depends, HTTPException, Form, Cookie, status
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.security import APIKeyHeader, HTTPBearer, HTTPAuthorizationCredentials
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Import provider modules
from providers import get_provider, HuggingFaceProvider, OpenAIProvider, DeepSeekProvider, OpenRouterProvider

# Import prompt modules
from prompts import PromptTemplate, PromptTemplateManager, PromptMarketplace

# Import ads module
from ads import GoogleAdsManager

# Import additional modules
import random
import time
import hashlib

# Import the Base from models.py
from models import Base

# Import the TOOLS from tools.py
from tools import TOOLS

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("app")

# Database setup
DATABASE_URL = "sqlite:///./magicAi.db"  # Path to your SQLite database
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Create the database tables
Base.metadata.create_all(bind=engine)

# Create the FastAPI app
app = FastAPI(
    title="AI Tool Hub",
    description="A platform for using AI models with various tools and prompt templates",
    version="1.0.0"
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")

# Set up Jinja2 templates
templates = Jinja2Templates(directory="templates")

# Initialize components
template_manager = PromptTemplateManager()
marketplace = PromptMarketplace()
ads_manager = GoogleAdsManager()

# Security
API_KEY_NAME = "X-API-Key"
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
security = HTTPBearer(auto_error=False)

# In-memory store of API keys for demo purpose
# In production, these would be stored in a database
API_KEYS = {}

# In-memory session store for demo purpose
# In production, these would be stored in a database or Redis
SESSIONS = {}

# Add new models for ad rewards
class AdRewardRequest(BaseModel):
    ad_type: str  # 'daily' or 'special'
    impression_id: Optional[str] = None

class DailyReward(BaseModel):
    user_id: str
    date: str
    count: int = 0
    last_reward: Optional[str] = None

class SpecialReward(BaseModel):
    user_id: str
    date: str
    claimed: bool = False
    claimed_at: Optional[str] = None

# In-memory stores for rewards (in production, use a database)
DAILY_REWARDS = {}
SPECIAL_REWARDS = {}

# Models
class CreditUpdateRequest(BaseModel):
    user_id: str
    amount: float
    reason: str = "manual"

class GenerateRequest(BaseModel):
    prompt: str
    model: str
    provider: str
    system_message: Optional[str] = None
    max_tokens: Optional[int] = 1000
    temperature: Optional[float] = 0.7
    prompt_template_id: Optional[str] = None
    template_variables: Optional[Dict[str, Any]] = None

class ImageGenerateRequest(BaseModel):
    prompt: str
    provider: str = "openai"
    model: Optional[str] = None
    size: Optional[str] = "1024x1024"
    prompt_template_id: Optional[str] = None
    template_variables: Optional[Dict[str, Any]] = None

class UserInfo(BaseModel):
    id: str
    username: str
    email: str
    credits: float = 10.0  # New users start with some free credits
    created_at: str = Field(default_factory=lambda: datetime.now().isoformat())
    last_login: str = Field(default_factory=lambda: datetime.now().isoformat())
    role: str = "user"

# Models for prompt system
class Prompt(BaseModel):
    id: str
    tool_id: str
    title: str
    content: str
    description: Optional[str] = None
    creator_id: Optional[str] = None
    is_default: bool = False
    usage_count: int = 0
    avg_rating: float = 0.0
    created_at: str = None
    updated_at: str = None
    tags: List[str] = []

class PromptRating(BaseModel):
    prompt_id: str
    user_id: str
    rating: int  # 1-5
    created_at: str

class UserPromptHistory(BaseModel):
    user_id: str
    prompt_id: str
    tool_id: str
    used_at: str
    was_modified: bool = False
    modifications: Optional[str] = None

# Mock database (replace with actual DynamoDB integration)
PROMPTS = {}
PROMPT_RATINGS = []
USER_PROMPT_HISTORY = []

# Initialize default prompts
def init_default_prompts():
    default_prompts = [
        {
            "id": "text-summary-default",
            "tool_id": "text-summary",
            "title": "Concise Text Summary",
            "content": "Summarize the following text in 3-5 bullet points highlighting the main ideas: {{input}}",
            "description": "Creates a bulleted summary of any text",
            "is_default": True,
            "tags": ["summary", "concise", "bullets"]
        },
        {
            "id": "image-portrait-default",
            "tool_id": "image-generator",
            "title": "Professional Portrait",
            "content": "Create a professional portrait photo of {{subject}}, high quality, studio lighting, detailed features, professional attire",
            "description": "Generates professional-looking portrait images",
            "is_default": True,
            "tags": ["portrait", "professional", "photo"]
        },
        {
            "id": "text-blog-default",
            "tool_id": "text-generator",
            "title": "Blog Post Generator",
            "content": "Write a comprehensive blog post about {{topic}}. Include an introduction, 3-5 main sections with subheadings, and a conclusion. Use a conversational tone and include practical examples where appropriate.",
            "description": "Creates complete blog posts with proper structure",
            "is_default": True,
            "tags": ["blog", "writing", "content"]
        },
        {
            "id": "code-python-default",
            "tool_id": "code-generator",
            "title": "Python Function",
            "content": "Write a Python function that {{task}}. Include docstrings, type hints, error handling, and comments explaining your logic. Provide a small example of how to use the function.",
            "description": "Generates well-structured Python functions",
            "is_default": True,
            "tags": ["python", "function", "code"]
        }
    ]
    
    for prompt in default_prompts:
        prompt_obj = Prompt(
            id=prompt["id"],
            tool_id=prompt["tool_id"],
            title=prompt["title"],
            content=prompt["content"],
            description=prompt["description"],
            is_default=prompt["is_default"],
            tags=prompt["tags"],
            created_at=datetime.now().isoformat(),
            updated_at=datetime.now().isoformat()
        )
        PROMPTS[prompt["id"]] = prompt_obj

# Call initialization at startup
init_default_prompts()

# Helper functions for prompt system
def get_prompts_for_tool(tool_id: str) -> List[Prompt]:
    """Get all prompts for a specific tool"""
    return [p for p in PROMPTS.values() if p.tool_id == tool_id]

def get_default_prompts_for_tool(tool_id: str) -> List[Prompt]:
    """Get default prompts for a specific tool"""
    return [p for p in PROMPTS.values() if p.tool_id == tool_id and p.is_default]

def get_trending_prompts(limit: int = 5) -> List[Prompt]:
    """Get trending prompts based on usage count and ratings"""
    sorted_prompts = sorted(
        PROMPTS.values(), 
        key=lambda p: (p.usage_count * 0.7 + p.avg_rating * 0.3), 
        reverse=True
    )
    return sorted_prompts[:limit]

def get_personalized_prompts(user_id: str, tool_id: str, limit: int = 3) -> List[Prompt]:
    """Get personalized prompt recommendations for a user and tool"""
    # Get user history for this tool
    user_tool_history = [h for h in USER_PROMPT_HISTORY 
                        if h.user_id == user_id and h.tool_id == tool_id]
    
    if not user_tool_history:
        # If no history, return default prompts
        return get_default_prompts_for_tool(tool_id)
    
    # Count prompt usage
    prompt_usage = {}
    for history in user_tool_history:
        prompt_usage[history.prompt_id] = prompt_usage.get(history.prompt_id, 0) + 1
    
    # Get most used prompts
    most_used_prompt_ids = sorted(prompt_usage.items(), key=lambda x: x[1], reverse=True)
    most_used_prompts = [PROMPTS.get(pid) for pid, _ in most_used_prompt_ids[:limit]]
    
    # If we don't have enough, add some default or trending ones
    if len(most_used_prompts) < limit:
        defaults = get_default_prompts_for_tool(tool_id)
        for default in defaults:
            if default not in most_used_prompts and len(most_used_prompts) < limit:
                most_used_prompts.append(default)
    
    return most_used_prompts

def record_prompt_usage(user_id: str, prompt_id: str, tool_id: str, was_modified: bool = False, modifications: str = None):
    """Record that a user used a particular prompt"""
    # Update prompt usage count
    if prompt_id in PROMPTS:
        PROMPTS[prompt_id].usage_count += 1
        PROMPTS[prompt_id].updated_at = datetime.now().isoformat()
    
    # Add to history
    history_entry = UserPromptHistory(
        user_id=user_id,
        prompt_id=prompt_id,
        tool_id=tool_id,
        used_at=datetime.now().isoformat(),
        was_modified=was_modified,
        modifications=modifications
    )
    USER_PROMPT_HISTORY.append(history_entry)

def rate_prompt(user_id: str, prompt_id: str, rating: int):
    """Add or update a rating for a prompt"""
    # Check if user already rated this prompt
    existing_rating = next((r for r in PROMPT_RATINGS if r.user_id == user_id and r.prompt_id == prompt_id), None)
    
    if existing_rating:
        # Update existing rating
        existing_rating.rating = rating
        existing_rating.created_at = datetime.now().isoformat()
    else:
        # Add new rating
        rating_obj = PromptRating(
            prompt_id=prompt_id,
            user_id=user_id,
            rating=rating,
            created_at=datetime.now().isoformat()
        )
        PROMPT_RATINGS.append(rating_obj)
    
    # Update average rating on prompt
    if prompt_id in PROMPTS:
        prompt_ratings = [r.rating for r in PROMPT_RATINGS if r.prompt_id == prompt_id]
        if prompt_ratings:
            PROMPTS[prompt_id].avg_rating = sum(prompt_ratings) / len(prompt_ratings)
            PROMPTS[prompt_id].updated_at = datetime.now().isoformat()

# Dependency to get the database session
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

# User authentication and session management
def get_session_user(session_id: Optional[str] = Cookie(None)) -> Optional[UserInfo]:
    """Get the user from the session"""
    if not session_id or session_id not in SESSIONS:
        return None
    
    session = SESSIONS[session_id]
    if datetime.now() > session["expires"]:
        # Session expired
        del SESSIONS[session_id]
        return None
    
    # Update session expiration
    session["expires"] = datetime.now() + timedelta(hours=24)
    
    return session["user"]

def require_session_user(admin_required: bool = False, session_user: Optional[UserInfo] = Depends(get_session_user)):
    """Require a logged-in user, optionally requiring admin role"""
    if not session_user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Authentication required",
            headers={"WWW-Authenticate": "Bearer"},
        )
    
    if admin_required and getattr(session_user, "role", "") != "admin":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Admin access required",
        )
    
    return session_user

def verify_api_key(api_key: str = Depends(api_key_header), 
                 credentials: HTTPAuthorizationCredentials = Depends(security)) -> UserInfo:
    """Verify API key from header or Bearer token"""
    # Try to get the API key from the header
    key = api_key
    
    # If not found, try to get it from the Bearer token
    if not key and credentials:
        key = credentials.credentials
    
    # Check if the key is valid
    if not key or key not in API_KEYS:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid API key",
            headers={"WWW-Authenticate": "Bearer"},
        )
    
    return API_KEYS[key]["user"]

# Credit packages configuration
CREDIT_PACKAGES = [
    {
        "id": "starter",
        "name": "Starter Pack",
        "description": "Perfect for trying out our AI tools",
        "credits": 100,
        "price": 49.99
    },
    {
        "id": "pro",
        "name": "Pro Pack",
        "description": "Most popular choice for regular users",
        "credits": 500,
        "price": 199.99
    },
    {
        "id": "enterprise",
        "name": "Enterprise Pack",
        "description": "Best value for power users",
        "credits": 2000,
        "price": 690.99
    }
]

# Mock user data (replace with database in production)
users = {}

# Routes
@app.get("/", response_class=HTMLResponse)
async def home(request: Request, session_user: Optional[UserInfo] = Depends(get_session_user)):
    """Home page with tools list"""
    return templates.TemplateResponse(
        "index.html", 
        {
            "request": request, 
            "app_name": "AI Tool Hub",
            "tools": TOOLS,
            "session_user": session_user,
            "user": session_user,  # Add user for consistency with other templates
            "user_credits": session_user.credits if session_user else 0
        }
    )

@app.get("/tool/{tool_id}", response_class=HTMLResponse)
async def tool_page(
    tool_id: str,
    request: Request,
    session_user: Optional[UserInfo] = Depends(get_session_user)
):
    """Tool-specific page to execute a specific AI tool"""
    # Get the tool
    tool = next((t for t in TOOLS if t.id == tool_id), None)
    
    if not tool:
        return templates.TemplateResponse(
            "error.html",
            {
                "request": request,
                "app_name": "AI Tool Hub",
                "error_title": "Tool Not Found",
                "error_description": "The requested tool does not exist.",
                "user": session_user,
                "user_credits": session_user.credits if session_user else 0,
                "tools": TOOLS  # Include for consistent sidebar navigation
            }
        )
    
    # Get example prompts for this tool
    suggestions = tool.example_prompts if hasattr(tool, 'example_prompts') else []
    
    # Check if the user has enough credits
    has_enough_credits = session_user and session_user.credits >= tool.cost if session_user else False
    
    # Get available providers directly from the tool
    providers = tool.providers
    
    return templates.TemplateResponse(
        "tool.html",
        {
            "request": request,
            "app_name": "AI Tool Hub",
            "tool": tool,
            "user": session_user,
            "user_credits": session_user.credits if session_user else 0,
            "has_enough_credits": has_enough_credits,
            "suggestions": suggestions,
            "providers": providers,
            "tools": TOOLS  # Include all tools for sidebar navigation
        }
    )

@app.get("/result/{result_id}", response_class=HTMLResponse)
async def result_page(
    request: Request, 
    result_id: str,
    session_user: Optional[UserInfo] = Depends(get_session_user)
):
    """Result display page"""
    # In a real app, you'd fetch the result from a database
    # Here, we'll use a mock result for demonstration
    result = {
        "id": result_id,
        "type": "text",  # or "image" or "code"
        "provider": "openai",
        "model": "gpt-4",
        "prompt": "Write a short poem about AI",
        "result": """
        Silicon dreams in neural space,
        Learning patterns, quickening pace.
        Algorithms dance with grace divine,
        In zeros and ones, intelligence shine.
        
        Human-made yet growing beyond,
        Of our creations, we grow fond.
        Partners in progress, AI and we,
        Crafting together what's yet to be.
        """,
        "created_at": datetime.now().isoformat(),
        "response_time": 2.3  # seconds
    }
    
    return templates.TemplateResponse(
        "result.html", 
        {
            "request": request, 
            "app_name": "AI Tool Hub",
            "result": result,
            "user": session_user,
            "user_credits": session_user.credits if session_user else 0
        }
    )

@app.get("/marketplace", response_class=HTMLResponse)
async def marketplace_page(
    request: Request,
    category: Optional[str] = None,
    sort_by: str = "popular",
    session_user: Optional[UserInfo] = Depends(get_session_user)
):
    """Prompt marketplace page"""
    prompts = marketplace.list_marketplace_prompts(category=category, sort_by=sort_by)
    
    return templates.TemplateResponse(
        "marketplace.html", 
        {
            "request": request, 
            "app_name": "AI Tool Hub",
            "prompts": prompts,
            "category": category,
            "sort_by": sort_by,
            "user": session_user,
            "user_credits": session_user.credits if session_user else 0
        }
    )

@app.get("/ad", response_class=HTMLResponse)
async def ad_page(
    request: Request,
    session_user: Optional[UserInfo] = Depends(get_session_user)
):
    """Ad reward page"""
    return templates.TemplateResponse(
        "ad_reward.html", 
        {
            "request": request, 
            "app_name": "AI Tool Hub",
            "user": session_user,
            "user_credits": session_user.credits if session_user else 0
        }
    )

@app.get("/login", response_class=HTMLResponse)
async def login_page(request: Request):
    """Login page"""
    return templates.TemplateResponse(
        "login.html", 
        {
            "request": request, 
            "app_name": "AI Tool Hub"
        }
    )

@app.post("/login")
async def login(
    request: Request,
    username: str = Form(...),
    password: str = Form(...)
):
    """Process login"""
    # Check if admin login
    if username == "admin":
        # Check admin credentials
        admin_key = API_KEYS.get("admin_key")
        if not admin_key or admin_key.get("password") != password:
            # Admin login failure
            return templates.TemplateResponse(
                "login.html",
                {
                    "request": request,
                    "app_name": "AI Tool Hub",
                    "error": "Invalid admin credentials"
                },
                status_code=400
            )
        
        # Admin login success
        user = admin_key["user"]
    else:
        # For demo purposes, any username/password combo works
        # In production, you'd verify against a database
        
        # Create or update user
        user_id = f"user_{username.lower().replace(' ', '_')}"
        user = UserInfo(
            id=user_id,
            username=username,
            email=f"{username.lower().replace(' ', '.')}@example.com",
            last_login=datetime.now().isoformat()
        )
        
        # Create session
        session_id = secrets.token_urlsafe(32)
        SESSIONS[session_id] = {
            "user": user,
            "expires": datetime.now() + timedelta(hours=24)
        }
        
        # Create redirect response
        response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)
        
        # Set session cookie
        response.set_cookie(
            key="session_id",
            value=session_id,
            max_age=86400,  # 24 hours
            httponly=True,
            samesite="lax"
        )
        
        return response

@app.get("/logout")
async def logout(
    response: Response,
    session_id: Optional[str] = Cookie(None)
):
    """Process logout"""
    if session_id and session_id in SESSIONS:
        del SESSIONS[session_id]
    
    response.delete_cookie(key="session_id")
    
    return RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)

@app.get("/credits", response_class=HTMLResponse)
async def credits_page(request: Request, session_user: Optional[UserInfo] = Depends(get_session_user)):
    """Credits management page"""
    if not session_user:
        return RedirectResponse(url="/login")
    
    # Get credit history (mock data - replace with database in production)
    credit_history = [
        {
            "date": "2024-03-20 14:30",
            "type": "Daily Reward",
            "amount": 5,
            "details": "Watched daily ad"
        },
        {
            "date": "2024-03-19 15:45",
            "type": "Tool Usage",
            "amount": -5,
            "details": "Used Image Generation"
        }
    ]
    
    return templates.TemplateResponse(
        "credits.html",
        {
            "request": request,
            "app_name": "AI Tool Hub",
            "user": session_user,
            "user_credits": session_user.credits,
            "credit_packages": CREDIT_PACKAGES,
            "credit_history": credit_history,
            "referral_link": f"{request.base_url}?ref={session_user.id}"
        }
    )

@app.get("/api/ads/status")
async def get_ads_status(request: Request, session_user: Optional[UserInfo] = Depends(get_session_user)):
    """Get user's ad reward status"""
    if not session_user:
        return JSONResponse(
            status_code=status.HTTP_401_UNAUTHORIZED,
            content={"success": False, "error": "Authentication required"}
        )
    
    # Get daily reward status
    daily_reward = DAILY_REWARDS.get(f"{session_user.id}_{datetime.now().date().isoformat()}")
    can_claim_daily = True
    cooldown_remaining = 0
    
    if daily_reward and daily_reward.last_reward:
        last_reward_time = datetime.fromisoformat(daily_reward.last_reward)
        if (datetime.now() - last_reward_time).total_seconds() < 3600:  # 1 hour cooldown
            can_claim_daily = False
            cooldown_remaining = 3600 - (datetime.now() - last_reward_time).total_seconds()
    
    # Get special reward status
    special_reward = SPECIAL_REWARDS.get(f"{session_user.id}_{datetime.now().date().isoformat()}")
    can_claim_special = True
    
    if special_reward and special_reward.claimed:
        can_claim_special = False
    
    return JSONResponse(
        content={
            "success": True,
            "daily_rewards": {
                "count": daily_reward.count if daily_reward else 0,
                "can_claim": can_claim_daily and (not daily_reward or daily_reward.count < 3),
                "cooldown_remaining": int(cooldown_remaining)
            },
            "special_reward": {
                "claimed": special_reward.claimed if special_reward else False,
                "can_claim": can_claim_special
            }
        }
    )

@app.post("/api/ads/claim")
async def claim_ad_reward(
    request: Request,
    session_user: Optional[UserInfo] = Depends(get_session_user)
):
    """Process ad reward claim"""
    if not session_user:
        return JSONResponse(
            status_code=status.HTTP_401_UNAUTHORIZED,
            content={"success": False, "error": "Authentication required"}
        )
    
    data = await request.json()
    reward_type = data.get("type")
    
    if reward_type not in ["daily", "special"]:
        return JSONResponse(
            status_code=status.HTTP_400_BAD_REQUEST,
            content={"success": False, "error": "Invalid reward type"}
        )
    
    today = datetime.now().date().isoformat()
    
    if reward_type == "daily":
        # Get or create daily reward record
        daily_key = f"{session_user.id}_{today}"
        if daily_key not in DAILY_REWARDS:
            DAILY_REWARDS[daily_key] = DailyReward(
                user_id=session_user.id,
                date=today,
                count=0
            )
        
        daily_reward = DAILY_REWARDS[daily_key]
        
        # Check if user has already claimed maximum daily rewards
        if daily_reward.count >= 3:
            return JSONResponse(
                status_code=status.HTTP_400_BAD_REQUEST,
                content={"success": False, "error": "Daily reward limit reached"}
            )
        
        # Check cooldown period
        if daily_reward.last_reward:
            last_reward_time = datetime.fromisoformat(daily_reward.last_reward)
            if (datetime.now() - last_reward_time).total_seconds() < 3600:
                return JSONResponse(
                    status_code=status.HTTP_400_BAD_REQUEST,
                    content={"success": False, "error": "Please wait 1 hour between rewards"}
                )
        
        # Award credits
        reward_amount = 10
        session_user.credits += reward_amount
        daily_reward.count += 1
        daily_reward.last_reward = datetime.now().isoformat()
        
        return JSONResponse(
            content={
                "success": True,
                "credits_earned": reward_amount,
                "current_credits": session_user.credits,
                "daily_progress": daily_reward.count
            }
        )
    
    else:  # special reward
        # Get or create special reward record
        special_key = f"{session_user.id}_{today}"
        if special_key not in SPECIAL_REWARDS:
            SPECIAL_REWARDS[special_key] = SpecialReward(
                user_id=session_user.id,
                date=today,
                claimed=False
            )
        
        special_reward = SPECIAL_REWARDS[special_key]
        
        # Check if user has already claimed special reward
        if special_reward.claimed:
            return JSONResponse(
                status_code=status.HTTP_400_BAD_REQUEST,
                content={"success": False, "error": "Special reward already claimed"}
            )
        
        # Award credits
        reward_amount = 25
        session_user.credits += reward_amount
        special_reward.claimed = True
        special_reward.claimed_at = datetime.now().isoformat()
        
        return JSONResponse(
            content={
                "success": True,
                "credits_earned": reward_amount,
                "current_credits": session_user.credits
            }
        )

@app.get("/create-admin")
async def create_admin(
    request: Request,
    response: Response,
):
    """Create an admin user (only for development)"""
    # Check if admin already exists
    admin_exists = False
    admin_user = None
    
    for session in SESSIONS.values():
        if session.get("user") and session["user"].username == "admin":
            admin_exists = True
            admin_user = session["user"]
            break
    
    if admin_exists:
        # Update admin credits
        admin_user.credits = 10000
        admin_user.role = "admin"
        return JSONResponse(
            content={
                "success": True,
                "message": "Admin user updated with 10000 credits",
                "user_id": admin_user.id
            }
        )
    
    # Create admin user
    user_id = "admin"
    user = UserInfo(
        id=user_id,
        username="admin",
        email="admin@example.com",
        credits=10000,
        role="admin",
        last_login=datetime.now().isoformat()
    )
    
    # In production, you would hash the password
    # For simplicity, we'll store it in a way that the login function can use it
    # This is for development only, never do this in production!
    admin_info = {
        "user": user,
        "password": "admin123",  # Plaintext for demo only
        "created_at": datetime.now().isoformat()
    }
    
    # Store admin info for login
    API_KEYS["admin_key"] = admin_info
    
    return JSONResponse(
        content={
            "success": True,
            "message": "Admin user created with 10000 credits",
            "user_id": user_id,
            "username": "admin",
            "password": "admin123"
        }
    )

@app.get("/admin", response_class=HTMLResponse)
async def admin_dashboard(
    request: Request,
    user: UserInfo = Depends(lambda: require_session_user(admin_required=True))
):
    """Admin dashboard"""
    # Get all users
    users = []
    for session_id, session in SESSIONS.items():
        if "user" in session:
            users.append({
                "id": session["user"].id,
                "username": session["user"].username,
                "email": session["user"].email,
                "credits": session["user"].credits,
                "role": getattr(session["user"], "role", "user"),
                "session_id": session_id,
                "expires": session["expires"].isoformat() if "expires" in session else None
            })
    
    # Sort users by credits (descending)
    users.sort(key=lambda x: x["credits"], reverse=True)
    
    return templates.TemplateResponse(
        "admin_dashboard.html",
        {
            "request": request,
            "app_name": "AI Tool Hub",
            "user": user,
            "user_credits": user.credits,
            "users": users,
            "total_users": len(users),
            "total_credits": sum(u["credits"] for u in users),
            "admin_count": sum(1 for u in users if u["role"] == "admin")
        }
    )

@app.post("/api/admin/update-credits")
async def admin_update_credits(
    request: Request,
    user: UserInfo = Depends(lambda: require_session_user(admin_required=True))
):
    """Admin endpoint to update user credits"""
    data = await request.json()
    user_id = data.get("user_id")
    new_credits = data.get("credits")
    
    if not user_id or new_credits is None:
        return JSONResponse(
            status_code=status.HTTP_400_BAD_REQUEST,
            content={"success": False, "error": "User ID and credits are required"}
        )
    
    # Find the user
    target_user = None
    for session in SESSIONS.values():
        if "user" in session and session["user"].id == user_id:
                target_user = session["user"]
                break
    
    if not target_user:
        return JSONResponse(
            status_code=status.HTTP_404_NOT_FOUND,
            content={"success": False, "error": "User not found"}
        )
    
    # Update credits
    try:
        target_user.credits = float(new_credits)
    except ValueError:
        return JSONResponse(
            status_code=status.HTTP_400_BAD_REQUEST,
            content={"success": False, "error": "Invalid credit amount"}
        )
    
    return JSONResponse(
        content={
            "success": True,
            "user_id": user_id,
            "new_credits": target_user.credits,
            "message": f"Credits updated for user {target_user.username}"
        }
    )

@app.post("/process-request", response_class=HTMLResponse)
async def process_request(
    request: Request,
    tool_id: str = Form(...),
    prompt: str = Form(...),
    provider: str = Form("openai"),
    model: str = Form("default"),
    session_user: Optional[UserInfo] = Depends(get_session_user)
):
    """Process a tool request from a form"""
    # Get the tool
    tool = next((t for t in TOOLS if t.id == tool_id), None)
    
    if not tool:
        return templates.TemplateResponse(
            "error.html",
            {
                "request": request,
                "app_name": "AI Tool Hub",
                "error_title": "Tool Not Found",
                "error_description": "The requested tool does not exist.",
                "user": session_user,
                "user_credits": session_user.credits if session_user else 0,
                "tools": TOOLS  # Include for consistent sidebar navigation
            }
        )
    
    # Confirm that user is logged in
    if not session_user:
        return templates.TemplateResponse(
            "error.html",
            {
                "request": request,
                "app_name": "AI Tool Hub",
                "error_title": "Login Required",
                "error_description": "You must be logged in to use AI tools.",
                "user": None,
                "user_credits": 0,
                "tools": TOOLS  # Include for consistent sidebar navigation
            }
        )
    
    # Check if the user has enough credits
    if session_user.credits < tool.cost:
        return templates.TemplateResponse(
            "ad_reward.html",
            {
                "request": request,
                "app_name": "AI Tool Hub",
                "user": session_user,
                "user_credits": session_user.credits,
                "tool": tool,
                "reward_ad": ads_manager.get_ad_code("reward_video"),
                "sidebar_ad": ads_manager.get_ad_code("sidebar"),
                "return_url": f"/tool/{tool_id}",
                "tools": TOOLS  # Include for consistent sidebar navigation
            }
        )
    
    # Get the provider
    try:
        provider_instance = get_provider(provider)
        if not provider_instance:
            return templates.TemplateResponse(
                "error.html",
                {
                    "request": request,
                    "app_name": "AI Tool Hub",
                    "error_title": "Provider Not Supported",
                    "error_description": f"The provider '{provider}' is not supported.",
                    "user": session_user,
                    "user_credits": session_user.credits if session_user else 0,
                    "tools": TOOLS  # Include for consistent sidebar navigation
                }
            )
        
        result = None
        result_type = "text"  # Default type
        
        # Determine result type based on tool ID
        if "image" in tool.id or "logo" in tool.id or "avatar" in tool.id:
            result_type = "image"
        elif "code" in tool.id or "debugging" in tool.id:
            result_type = "code"
        
        # Process the request based on tool type
        try:
            if result_type == "text":
                # Text generation tool
                result = provider_instance.generate_text(
                    prompt=prompt,
                    model=model if model != "default" else "mistralai/Mistral-7B-Instruct-v0.2",
                    max_tokens=1000,
                    temperature=0.7
                )
            elif result_type == "image":
                # Image generation tool
                try:
                    result = provider_instance.generate_image(
                        prompt=prompt,
                        model=model if model != "default" else "stabilityai/stable-diffusion-xl-base-1.0"
                    )
                except AttributeError:
                    # Provider doesn't support image generation
                    return templates.TemplateResponse(
                        "error.html",
                        {
                            "request": request,
                            "app_name": "AI Tool Hub",
                            "error_title": "Provider Not Supported",
                            "error_description": f"The provider '{provider}' does not support image generation.",
                            "user": session_user,
                            "user_credits": session_user.credits if session_user else 0,
                            "tools": TOOLS  # Include for consistent sidebar navigation
                        }
                    )
            elif result_type == "code":
                # Code generation tool
                result = provider_instance.generate_text(
                    prompt=prompt,
                    model=model if model != "default" else "gpt2",
                    max_tokens=1500,
                    temperature=0.5
                )
            else:
                return templates.TemplateResponse(
                    "error.html",
                    {
                        "request": request,
                        "app_name": "AI Tool Hub",
                        "error_title": "Tool Type Not Supported",
                        "error_description": f"The tool type '{result_type}' is not supported.",
                        "user": session_user,
                        "user_credits": session_user.credits if session_user else 0,
                        "tools": TOOLS  # Include for consistent sidebar navigation
                    }
                )
            
            # Deduct credits
            session_user.credits -= tool.cost
            
            # Return the result
            return templates.TemplateResponse(
                "result.html",
                {
                    "request": request,
                    "app_name": "AI Tool Hub",
                    "user": session_user,
                    "user_credits": session_user.credits,
                    "tool": tool,
                    "prompt": prompt,
                    "result": result,
                    "result_type": result_type,
                    "tools": TOOLS,  # Include all tools for sidebar navigation
                    "json_data": result.get("raw_response", {}) if isinstance(result, dict) else {}
                }
            )
        except Exception as generate_error:
            return templates.TemplateResponse(
                "error.html",
                {
                    "request": request,
                    "app_name": "AI Tool Hub",
                    "error_title": "Generation Error",
                    "error_description": f"Error generating content: {str(generate_error)}",
                    "user": session_user,
                    "user_credits": session_user.credits,
                    "tools": TOOLS  # Include all tools for sidebar navigation
                }
            )
    except Exception as provider_error:
        return templates.TemplateResponse(
            "error.html",
            {
                "request": request,
                "app_name": "AI Tool Hub",
                "error_title": "Provider Error",
                "error_description": f"Error with provider: {str(provider_error)}",
                "user": session_user,
                "user_credits": session_user.credits,
                "tools": TOOLS  # Include all tools for sidebar navigation
            }
        )

@app.post("/register")
async def register(
    request: Request,
    response: Response,
    username: str = Form(...),
    email: str = Form(...),
    password: str = Form(...),
    session_id: Optional[str] = Cookie(None)
):
    """Process registration"""
    # Check if username or email already exists (in production use a database)
    for session in SESSIONS.values():
        if session.get("user"):
            if session["user"].username.lower() == username.lower():
                return templates.TemplateResponse(
                    "register.html",
                    {
                        "request": request,
                        "app_name": "AI Tool Hub",
                        "error": "Username already exists",
                        "form_data": {"username": username, "email": email}
                    },
                    status_code=400
                )
            if session["user"].email.lower() == email.lower():
                return templates.TemplateResponse(
                    "register.html",
                    {
                        "request": request,
                        "app_name": "AI Tool Hub",
                        "error": "Email already exists",
                        "form_data": {"username": username, "email": email}
                    },
                    status_code=400
                )
    
    # Create user
    user_id = f"user_{username.lower().replace(' ', '_')}"
    
    # Check if there's an existing temporary session to convert
    is_converting_temp = False
    temp_credits = 0
    
    if session_id and session_id in SESSIONS:
        session = SESSIONS[session_id]
        if session.get("is_temporary", False):
            # Convert temporary session to permanent
            is_converting_temp = True
            temp_credits = session["user"].credits
            # Delete the temporary session
            del SESSIONS[session_id]
    
    # Create new user with starting credits (more if converting from temp)
    start_credits = 10.0 + temp_credits
    user = UserInfo(
        id=user_id,
        username=username,
        email=email,
        credits=start_credits,
        last_login=datetime.now().isoformat()
    )
    
    # In production, you would hash the password and store in database
    
    # Create new session
    new_session_id = secrets.token_urlsafe(32)
    SESSIONS[new_session_id] = {
        "user": user,
        "expires": datetime.now() + timedelta(hours=24)
    }
    
    # Set session cookie
    response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)
    response.set_cookie(
        key="session_id",
        value=new_session_id,
        max_age=86400,  # 24 hours
        httponly=True,
        samesite="lax"
    )
    
    return response

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8006)