import yaml
from eveal.database import engine, sqlite_file_name
from sqlmodel import Session, SQLModel
from eveal import models_sde

import os
try:
    os.remove(sqlite_file_name)
except:
    pass
SQLModel.metadata.create_all(engine)


print("Importing SDE data...")
print("Importing icons...")
with open("static_eve/sde/fsd/iconIDs.yaml", "r", encoding="utf-8") as f:
    icons = yaml.safe_load(f)

with Session(engine) as db:
    for id, icon in icons.items():
        db.add(models_sde.SDEIcon(id=id, **icon))
    db.commit()


print("Importing categories...")
with open("static_eve/sde/fsd/categoryIDs.yaml", "r", encoding="utf-8") as f:
    categories = yaml.safe_load(f)

with Session(engine) as db:
    for id, category in categories.items():
        if category["published"] == False:
            continue
        db.add(models_sde.SDECategory(id=id,
                                      icon_id=category['iconID'] if 'iconID' in category else None,
                                      name=category['name']['en'],
                                      published=category['published']))
    db.commit()


print("Importing groups...")
with open("static_eve/sde/fsd/groupIDs.yaml", "r", encoding="utf-8") as f:
    groups = yaml.safe_load(f)

with Session(engine) as db:
    for id, group in groups.items():
        if group["published"] == False:
            continue
        db.add(models_sde.SDEGroup(id=id,
                                   anchorable=group['anchorable'],
                                   anchored=group['anchored'],
                                   category_id=group['categoryID'],
                                   fittableNonSingletion=group['fittableNonSingleton'],
                                   icon_id=group['iconID'] if 'iconID' in group else None,
                                   name=group['name']['en'],
                                   published=group['published'],
                                   useBasePrice=group['useBasePrice']
                                   ))
    db.commit()


print("Importing marketgroups...")
with open("static_eve/sde/fsd/marketGroups.yaml", "r", encoding="utf-8") as f:
    marketgroups = yaml.safe_load(f)

with Session(engine) as db:
    for id, marketgroup in marketgroups.items():
        db.add(models_sde.SDEMarketGroup(id=id,
                                         description=marketgroup['descriptionID']['en'] if 'descriptionID' in marketgroup else None,
                                         hasTypes=marketgroup['hasTypes'],
                                         icon_id=marketgroup['iconID'] if 'iconID' in marketgroup else None,
                                         name=marketgroup['nameID']['en'],
                                         parent_marketgroup_id=marketgroup['parentGroupID'] if 'parentGroupID' in marketgroup else None,
                                         ))
    db.commit()


print("Importing types...")
with open("static_eve/sde/fsd/typeIDs.yaml", "r", encoding="utf-8") as f:
    types = yaml.safe_load(f)

with Session(engine) as db:
    for id, type in types.items():
        if type["published"] == False:
            continue
        db.add(models_sde.SDEType(id=id,
                                  group_id=type['groupID'],
                                  marketgroup_id=type['marketGroupID'] if 'marketGroupID' in type else None,
                                  name=type['name']['en'],
                                  published=type['published'],
                                  basePrice=type['basePrice'] if 'basePrice' in type else None,
                                  description=type['description']['en'] if 'description' in type else None,
                                  icon_id=type['iconID'] if 'iconID' in type else None,
                                  portionSize=type['portionSize'],
                                  volume=type['volume'] if 'volume' in type else None,
                                  ))
    db.commit()


print("Importing materials...")
with open("static_eve/sde/fsd/typeMaterials.yaml", "r", encoding="utf-8") as f:
    materials = yaml.safe_load(f)

with Session(engine) as db:
    for id, material in materials.items():
        for mat in material['materials']:
            db.add(models_sde.SDETypeMaterial(type_id=id,
                                              material_id=mat['materialTypeID'],
                                              quantity=mat['quantity']
                                              ))
    db.commit()


print("DONE!")