<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

/**
 * Class TB_Shared_Ent_TeamData_Team
 *
 * @property integer $id
 * @property string|NULL $public_id
 * @property string $display_name
 * @property string $category
 * @property string|NULL $description
 * @property string|NULL $contact_information
 * @property string $emblem_url
 * @property string $emblem_cloud_id
 * @property array|null $course_categories_js
 * @property string $premium_package
 * @property int $num_max_members
 * @property string|null $affiliate_id
 * @property array|null $contractee_master_data_js
 * @property DateTime $last_updated_dt
 * @property DateTime $created_dt
 */
class TB_Shared_Ent_TeamData_Team extends Francis_Db_Row {

    /**
     * @param $publicId
     * @return NULL|static
     */
    public static function getByPublicId( $publicId )
    {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE public_id = :public_id ';
        $sql .= 'AND public_id IS NOT NULL LIMIT 1';
        $params = array( ':public_id' => $publicId );
        return self::findOne( $sql, $params );
    }

    /**
     * Get all used public ids
     * @return array
     */
    public static function getAllPublicIds()
    {
        $dbh = TB_Shared_Db_TeamData::get();
        $sql = 'SELECT public_id FROM team';
        $stmt = $dbh->prepare( $sql );
        $stmt->execute();
        return $stmt->fetchAll( PDO::FETCH_COLUMN, 0 );
    }

    /**
     * @return array
     */
    public static function getAllForJoin()
    {
        $sql = 'SELECT * FROM ' . self::getTable();
        return self::findMany( $sql );
    }

    /**
     * @param TB_Shared_Ent_TeamData_Profile $profile
     * @return array
     */
    public static function getByProfile( TB_Shared_Ent_TeamData_Profile $profile )
    {
        $teams = array();

        $teamIds = $profile->getTeamIds();
        foreach( $teamIds as $teamId )
        {
            array_push( $teams, TB_Shared_Ent_TeamData_Team::get( $teamId ) );
        }

        return $teams;
    }

    /**
     * Return table name
     *
     * @return null|string
     */
    public static function getTable() {
        return 'team';
    }

    /**
     * Return db handler
     *
     * @return Francis_Db_MySql|null
     */
    public static function getDbh() {
        return TB_Shared_Db_TeamData::get();
    }

    /**
     * @param $courseId
     * @return mixed|null
     */
    public function getCourseCategoryDataById( $courseId )
    {
        $courseData = NULL;

        if ( is_array( $this->course_categories_js ) )
        {
            foreach( $this->course_categories_js as $cd )
            {
                if ( $courseId === $cd[ 'id' ] )
                {
                    $courseData = $cd;
                    break;
                }
            }
        }

        return $courseData;
    }

    /**
     * @param $courseId
     */
    public function removeCourseCategoryById( $courseId )
    {
        if ( is_array( $this->course_categories_js ) )
        {
            $cc = array();

            foreach( $this->course_categories_js as $cd )
            {
                if ( $courseId === $cd[ 'id' ] )
                {
                    continue;
                }
                else
                {
                    array_push( $cc, $cd );
                }
            }

            $this->course_categories_js = $cc;
        }
    }

    /**
     * @param $categoryId
     * @param $newCd
     */
    public function updateCourseCategoryById( $categoryId, array $newCd )
    {
        if ( is_array( $this->course_categories_js ) )
        {
            $cc = array();
            $newCd[ 'id' ] = $categoryId;

            foreach( $this->course_categories_js as $cd )
            {
                if ( $categoryId === $cd[ 'id' ] )
                {
                    array_push( $cc, $newCd );
                }
                else
                {
                    array_push( $cc, $cd );
                }
            }

            $this->course_categories_js = $cc;
        }
    }

    /**
     * @param array $courseData [ 'id' => md5, 'name' ]
     */
    public function addCourseCategory( array $courseData )
    {
        $cd = $this->course_categories_js;
        if ( !is_array( $cd ) )
        {
            $cd = array();
        }

        if ( !array_key_exists( 'id', $courseData ) )
        {
            $courseData[ 'id' ] = uniqid( ($this->id . '-') );
        }

        array_push( $cd, $courseData );

        $this->course_categories_js = $cd;
    }

    /**
     * Called during deletion
     */
    public function deleteReferences() {

        // Remaining TeamData Tables:
        // - Appointment
        // - News
        TB_Shared_Ent_TeamData_Appointment::deleteByTeamId( $this->id );

        // Remaining Core Tables:
        // - Email notification
        // - invitation

        TB_Shared_Ent_TeamData_Contract::deleteAllByTeamId( $this->id );

        // Delete image from cloud
        if ( !is_null( $this->emblem_cloud_id ) )
        {
            $cloudinary = TB_Server_Utils_Cloudinary::get();
            $cloudinary->delete( $this->emblem_cloud_id );
        }
    }

    public function getDefaultCategoryId()
    {
        return $this->id . '-0';
    }

    public function setDefaultCourseCategory()
    {
        $this->course_categories_js = array(
            array(
            'id' => $this->getDefaultCategoryId(),
            'name' => 'Allgemein'
            )
        );
    }
}