from api_library.date_time_actions.date_functions import system_arrow
from databases.no_sql_models.mongo_database import MongoQuery
from databases.no_sql_models.identity import MongoQueryIdentity


"""

        found_user: Users = Users.check_user_exits(
            access_key=data.access_key, domain=data.domain
        )
        access_token = found_user.generate_access_token()
        query_engine = MongoQueryIdentity(company_uuid=found_user.related_company)
        if found_user.check_password(password=data.password):
            access_object_to_redis = RedisActions.save_access_token_to_redis(
                request=request,
                found_user=found_user,
                domain=data.domain,
                access_token=access_token,
            )
            refresher_token = found_user.generate_refresher_token(
                domain=data.domain, remember_me=data.remember_me
            )
            headers_request = request.headers
            headers_request = dict(headers_request)
            headers_request["evyos-user-agent"] = headers_request.get("user-agent")
            headers_request["evyos-platform"] = headers_request.get("user-agent")
            headers_request["evyos-ip-ext"] = "94.54.68.158"
            # found_user.last_agent = headers_request.get("evyos-user-agent", None)
            # found_user.last_platform = headers_request.get("evyos-platform", None)
            # found_user.last_remote_addr = headers_request.get("evyos-ip-ext", None)
            # found_user.last_seen = str(system_arrow.now())
            if ext_ip := headers_request.get("evyos-ip-ext"):
                agent = headers_request.get("evyos-user-agent", "")
                platform = headers_request.get("evyos-platform", "")
                address = requests.get(f"http://ip-api.com/json/{ext_ip}").json()
                address_package = {
                    "city": address["city"],
                    "zip": address["zip"],
                    "country": address["country"],
                    "countryCode": address["countryCode"],
                    "region": address["region"],
                    "regionName": address["regionName"],
                }
                mongo_db = MongoQueryIdentity(
                    company_uuid=str(found_user.related_company).replace(" ", ""),
                    storage_reasoning="AccessHistory",
                )
                filter_query = {
                    "agent": agent,
                    "platform": platform,
                    "address": address_package,
                    "user_id": found_user.id,
                }
                already_exits = mongo_db.mongo_engine.filter_by(filter_query) or None
                no_address_validates = mongo_db.mongo_engine.get_all()[0] == 0
                record_id = uuid.uuid4().__str__()
                notice_link = ApiStatic.blacklist_login(record_id=record_id)
                found_people = People.filter_one(People.id == found_user.person_id).data
                access_via_user = query_engine.update_access_history_via_user(
                    AccessHistoryViaUser(
                        **{
                            "user_uu_id": found_user.uu_id.__str__(),
                            "access_history": {
                                "record_id": record_id,
                                "agent": agent,
                                "platform": platform,
                                "address": address_package,
                                "ip": ext_ip,
                                "access_token": access_token,
                                "created_at": system_arrow.now().timestamp(),
                                # "is_confirmed": True if no_address_validates else False,
                                # "is_first": True if no_address_validates else False,
                            },
                        }
                    )
                )
                if already_exits:
                    update_mongo = mongo_db.mongo_engine.table.update_one(
                        filter=filter_query,
                        update={
                            "$set": {
                                "ip": ext_ip,
                                "access_token": access_token,
                                "created_at": system_arrow.now().timestamp(),
                            }
                        },
                    )
                else:
                    mongo_db.mongo_engine.insert(
                        payload={
                            "user_id": found_user.id,
                            "record_id": record_id,
                            "agent": agent,
                            "platform": platform,
                            "address": address_package,
                            "ip": ext_ip,
                            "access_token": access_token,
                            "created_at": system_arrow.now().timestamp(),
                            "is_confirmed": True if no_address_validates else False,
                            "is_first": True if no_address_validates else False,
                        }
                    )
            found_user.remember_me = bool(data.remember_me)
            found_user.save()
            return {
                "access_token": access_token,
                "refresher_token": refresher_token,
                "user": found_user,
                "access_object": access_object_to_redis,
            }
"""

query_engine = MongoQueryIdentity(
    company_uuid="5f5f4d2b-0b3f-4b1f-8f8a-3b3b7f3b7f3b",
)
query_engine_mongo = MongoQuery(
    table_name="5f5f4d2b-0b3f-4b1f-8f8a-3b3b7f3b7f3b*Domain",
    database_name="mongo_database",
)

payload={
    "user_uu_id": "5f5f4d2b-0b3f-4b1f-8f8a-3b3b7f3b7f3b",
    "other_domains_list": ["www.gluglu.com"],
    "main_domain": "www.gluglu.com",
    "modified_at": system_arrow.to_timestamp(system_arrow.now()),
}
query_engine.use_collection("PasswordHistory")
or_insert =query_engine.mongo_engine.find_or_insert(
    payload={
        "user_uu_id": str(payload.get("user_uu_id")),
        "password_history": [],
    },
    field="user_uu_id",
)
print('or_insert', or_insert.data)
quit()
get_all = query_engine_mongo.get_all()
print('get_all', get_all.data)
