from fastapi import APIRouter, HTTPException, Depends from sqlalchemy.orm import Session from app.schemas.company import CompanyProfile from app.database import get_db from app.models.company import CompanyProfileModel import json router = APIRouter() @router.post("/company-profile", response_model=CompanyProfile) def save_company_profile(profile: CompanyProfile, db: Session = Depends(get_db)): print(f"!!! SAVING PROFILE: {profile.name} !!!") # Try to find existing profile (assume only one for now) db_profile = db.query(CompanyProfileModel).first() if not db_profile: print("Creating NEW profile in DB") db_profile = CompanyProfileModel() db.add(db_profile) db_profile.name = profile.name db_profile.industry = profile.industry db_profile.services = json.dumps(profile.services) db_profile.experience = profile.experience db_profile.certifications = json.dumps(profile.certifications) db_profile.regions = json.dumps(profile.regions) db_profile.documents_available = json.dumps(profile.documents_available) db_profile.keywords = json.dumps(profile.keywords) db.commit() print("!!! PROFILE SAVED SUCCESSFULLY !!!") return profile @router.get("/company-profile", response_model=CompanyProfile) def get_company_profile(db: Session = Depends(get_db)): db_profile = db.query(CompanyProfileModel).first() if not db_profile: print("No profile found, returning default") return CompanyProfile( name="Andes Digital", industry="Tecnología", services=["Automatización AI", "Desarrollo Software"], experience="5 años en el sector", certifications=[], regions=["Metropolitana"], documents_available=["RUT"], keywords=["software", "IA", "automatización"] ) # Handle list fields that are stored as JSON strings def safe_json_load(field, default=[]): try: return json.loads(field) if field else default except: return [field] if field else default return CompanyProfile( name=db_profile.name, industry=db_profile.industry, services=safe_json_load(db_profile.services, ["General"]), experience=db_profile.experience, certifications=safe_json_load(db_profile.certifications), regions=safe_json_load(db_profile.regions, ["Nacional"]), documents_available=safe_json_load(db_profile.documents_available), keywords=safe_json_load(db_profile.keywords, ["tecnología"]) )