import os
import sys
from pathlib import Path
from PIL import Image, ImageDraw

sys.path.append(str(Path(__file__).resolve().parent))

from app.database import init_db, SessionLocal
from app.models.cloth import Cloth
from app.models.apikey import ApiKey
from app.config import settings

DEFAULT_PROMPT = "Replace everyone's outfit with the outfit from the reference image"

def create_sample_image(filename: str, color: tuple, label: str, icon_text: str = "MAP") -> str:
    w, h = 400, 500
    img = Image.new("RGBA", (w, h), (245, 247, 250, 255))
    draw = ImageDraw.Draw(img)

    top_y = 70
    bottom_y = 430
    left_x = 70
    right_x = 330

    draw.rounded_rectangle([left_x, top_y, right_x, bottom_y], radius=30, fill=color, outline=(30, 41, 59, 255), width=4)
    draw.ellipse([w//2 - 45, top_y - 25, w//2 + 45, top_y + 35], fill=(245, 247, 250, 255), outline=(30, 41, 59, 255), width=4)

    draw.text((w//2 - 60, h//2 - 20), icon_text, fill=(255, 255, 255, 220))
    draw.text((w//2 - 70, h//2 + 20), label[:16], fill=(255, 255, 255, 200))

    orig_path = settings.CLOTH_UPLOAD_DIR / f"{filename}_orig.png"
    img.save(orig_path, format="PNG")

    cutout = Image.new("RGBA", (w, h), (0, 0, 0, 0))
    draw_cut = ImageDraw.Draw(cutout)
    draw_cut.rounded_rectangle([left_x, top_y, right_x, bottom_y], radius=30, fill=color, outline=(30, 41, 59, 255), width=4)
    draw_cut.ellipse([w//2 - 45, top_y - 25, w//2 + 45, top_y + 35], fill=(0, 0, 0, 0), outline=(30, 41, 59, 255), width=4)
    draw_cut.text((w//2 - 60, h//2 - 20), icon_text, fill=(255, 255, 255, 220))
    draw_cut.text((w//2 - 70, h//2 + 20), label[:16], fill=(255, 255, 255, 200))

    cutout_path = settings.CUTOUT_UPLOAD_DIR / f"{filename}_cutout.png"
    cutout.save(cutout_path, format="PNG")

    return f"/static/uploads/cutouts/{filename}_cutout.png"

def seed():
    init_db()
    db = SessionLocal()

    # Seed Decart AI Keys
    if not db.query(ApiKey).first():
        db.add(ApiKey(
            name="Decart Live Production Key (Lucy VTON 3.5)",
            provider="decart",
            model_name="lucy-vton-3.5",
            api_key="sk-decart-prod-vton35-livemirror-map-enterprise-2026",
            is_active=True,
            daily_quota=10000,
            usage_count=142
        ))
        db.commit()
        print("✓ Decart API Keys ready.")

    db.close()
    print("Database check completed (No mock fleet devices seeded).")

if __name__ == "__main__":
    seed()
