<?php

require_once __DIR__ . '/../src/server/server/config/boot_global.php';
require_once __DIR__ . '/../src/server/server/config/boot_local.php';

Francis_Utils_Config::set('db.tbcore.host', 'v034011.kasserver.com');
Francis_Utils_Config::set('db.tbcore.name', 'd02e620e');          // probudy core
Francis_Utils_Config::set('db.tbcore.user', 'd02e620e');
Francis_Utils_Config::set('db.tbcore.pass', 'DUXScbxJ2bDStv7P');

Francis_Utils_Config::set('db.tbteamdata.host', 'v034011.kasserver.com');
Francis_Utils_Config::set('db.tbteamdata.name', 'd02e620f');
Francis_Utils_Config::set('db.tbteamdata.user', 'd02e620f');
Francis_Utils_Config::set('db.tbteamdata.pass', 'NT7uSbtaebV9Gedq');

$text = file_get_contents( 'data.json' );
$json = json_decode( $text, true );
$teamData = $json[ 'data' ];

foreach( $teamData as $td )
{
    $team = TB_Shared_Ent_TeamData_Team::get(  $td[ 'id' ] );
    if ( null === $team )
    {
        continue;
    }

    $teamProfiles = TB_Shared_Ent_TeamData_Profile::getProfilesByTeamId( $team->id );
    if ( $teamProfiles )
    {

        if ( count( $teamProfiles ) > 1 )
        {
            print count( $teamProfiles ) . " Profile für das Team: " . $team->display_name . ' (ID: ' . $team->id . ')' . "\n";
        }

        //print "Updating team profiles...\n";
        /** @var TB_Shared_Ent_TeamData_Profile $teamProfile */
        /*
        foreach ( $teamProfiles as $teamProfile )
        {
            print "Leave team for profile " . $teamProfile->getName() . " ... \n";
            //$teamProfile->leaveTeam($team->id);
            //$teamProfile->save();
        }
        */
    }
    //print "Deleting team " . $team->display_name . '(' . $team->id . ' ) ' . " ... \n";
    //$team->delete();
}

return false;

die("ARE YOU SURE - THIS READS STUFF FROM data.json???");
$emailOfUserToDelete = 'nstaub@web.de';

/** @var TB_Shared_Ent_Core_Account $acc */
$account = TB_Shared_Ent_Core_Account::getByEmail($emailOfUserToDelete);
if (is_null($account)) {
    die("Account not found.");
}

$accountId = $account->id;
/** @var TB_Shared_Ent_TeamData_Profile $profile */
$profile = TB_Shared_Ent_TeamData_Profile::getByAccountId($accountId);

if (is_null($profile)) {
    die("No Profile found.");
}

print "Processing profile: " . $profile->getName() . " ... \n";

$profileId = $profile->id;
$teams = TB_Shared_Ent_TeamData_Team::getByProfile($profile);
if ($teams) {
    /** @var TB_Shared_Ent_TeamData_Team $team */
    foreach ($teams as $team) {
        if ($profile->isOwnerOfTeam($team->id)) {
            $teamProfiles = TB_Shared_Ent_TeamData_Profile::getProfilesByTeamId($team->id);
            if ($teamProfiles) {
                print "Updating team profiles...\n";
                /** @var TB_Shared_Ent_TeamData_Profile $teamProfile */
                foreach ($teamProfiles as $teamProfile) {
                    if ($teamProfile->id == $profileId) {
                        continue;
                    }
                    print "Leave team for profile " . $teamProfile->getName() . " ... \n";
                    $teamProfile->leaveTeam($team->id);
                    $teamProfile->save();
                }
            }
            print "Deleting team " . $team->display_name . " ... \n";
            $team->delete();
        }
    }
}

print "Deleting user information... ";
$profile->delete();
$account->delete();
