from pymongo import MongoClient

# Connect to MongoDB databases
client_screener = MongoClient('mongodb://jenya:DJenya$Mongo%40St0ckDB@172.105.59.175:27017/')
db_screener = client_screener['trade_iq']
# collection_listed_nse = db_screener['listed_NSE']
collection_listed_bse = db_screener['listed_BSE']

client_office = MongoClient('mongodb://192.168.31.176:27017/?directConnection=true')
db_office = client_office['zerodha_test']
# collection_nse_data = db_office['nse_data2']
collection_bse_data = db_office['bse_data2']

# listed_BSE to bse_data1 Filds add
for bse_data in collection_bse_data.find():
    symbol = bse_data.get("symbol")

    # Find the corresponding document in listed_BSE by matching Security Id with the symbol
    listed_bse = collection_listed_bse.find_one({"Security Id": {"$regex": f"^{symbol}"}})

    if listed_bse:
        # Fields to add from listed_BSE
        update_fields = {
            "Security Code": listed_bse.get("Security Code"),
            "Issuer Name": listed_bse.get("Issuer Name"),
            "Security Name": listed_bse.get("Security Name"),
            "Status": listed_bse.get("Status"),
            "Group": listed_bse.get("Group"),
            "Face Value": listed_bse.get("Face Value"),
            "ISIN No": listed_bse.get("ISIN No"),
            "Industry": listed_bse.get("Industry"),
            "Instrument": listed_bse.get("Instrument"),
            "Sector Name": listed_bse.get("Sector Name"),
            "Industry New Name": listed_bse.get("Industry New Name"),
            "Igroup Name": listed_bse.get("Igroup Name"),
            "ISubgroup Name": listed_bse.get("ISubgroup Name"),
            "exchange": listed_bse.get("exchange")
        }

        # Update bse_data1 document with the fields from listed_BSE
        collection_bse_data.update_one(
            {"_id": bse_data["_id"]},
            {"$set": update_fields}
        )

        print(f"Updated document with symbol {symbol}")

print("Update process complete.")


# # listed_NSE to nse_data1 Filds add
# for nse_data1_doc in collection_nse_data.find():
#     symbol = nse_data1_doc.get("symbol")

#     # Find the matching document from the listed_NSE collection
#     listed_nse_doc = collection_listed_nse.find_one({"Sym": {"$regex": f"^{symbol}"}})

#     if listed_nse_doc:
#         # Extract fields from listed_NSE to be added to nse_data1
#         update_fields = {
#             "Company Name": listed_nse_doc.get("DispSym"),
#             "Market capitalization": listed_nse_doc.get("Mcap"),
#             "ISIN No": listed_nse_doc.get("Isin"),
#             "Instrument": listed_nse_doc.get("Inst"),
#             "exchange": listed_nse_doc.get("Exch")
#         }

#         # Update the nse_data1 document with the new fields
#         collection_nse_data.update_one(
#             {"_id": nse_data1_doc["_id"]},
#             {"$set": update_fields}
#         )

#         print(f"Updated document with symbol: {symbol}")

# print("Update process complete.")