<?php


/**
 * Class TB_Shared_Ent_TeamData_PremiumLog
 * @property integer $id
 * @property string $braintree_customer_id
 * @property string $braintree_subscription_id
 * @property integer $account_id
 * @property integer $team_id
 * @property string $package_id
 * @property float $package_price
 * @property float $package_tax
 * @property DateTime $valid_from_dt
 * @property DateTime $valid_until_dt
 * @property string|null $billing_company_name
 * @property string|null $billing_company_tax_id
 * @property string $billing_first_name
 * @property string $billing_last_name
 * @property string $billing_street
 * @property string $billing_zip
 * @property string $billing_city
 * @property string $billing_country
 * @property string $billing_email
 * @property DateTime|null $status
 * @property string $comment
 * @property DateTime $last_update_dt
 * @property DateTime $created_dt
 *
 */
class TB_Shared_Ent_TeamData_PremiumLog extends Francis_Db_Row
{
    const STATUS_PENDING = 'PENDING';
    const STATUS_ACTIVE = 'ACTIVE';
    const STATUS_CANCELLED = 'CANCELLED';       // Cancdelled subscription while it is already billed and running
    const STATUS_DELETED = 'DELETED';           // Deleted subscription before first billing
    const STATUS_CLOSED = 'CLOSED';             // outdated log which has been processed


    /**
     * @param TB_Shared_Ent_Core_Account $account
     * @param TB_Shared_Ent_TeamData_Team $team
     * @return array
     */
    public static function getValidPremiumLogsForTeam( TB_Shared_Ent_TeamData_Team $team )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'team_id = :team_id AND ';
        $sql .= "( status = '" . TB_Shared_Ent_TeamData_PremiumLog::STATUS_ACTIVE . "' OR status = '" . TB_Shared_Ent_TeamData_PremiumLog::STATUS_PENDING . "' ) AND ";
        $sql .= '( valid_until_dt > NOW() OR valid_until_dt IS NULL )';

        return self::findMany( $sql, array( ':team_id' => $team->id ) );
    }

    /**
     * @param TB_Shared_Ent_TeamData_Team $team
     * @return NULL|static
     */
    public static function getCurrentPremiumLogForTeam( TB_Shared_Ent_TeamData_Team $team )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'team_id = :team_id AND ';
        $sql .= "( status = '" . TB_Shared_Ent_TeamData_PremiumLog::STATUS_ACTIVE . "' OR status = '" . TB_Shared_Ent_TeamData_PremiumLog::STATUS_PENDING . "' ) AND ";
        $sql .= 'valid_from_dt < NOW() AND ';
        $sql .= '( valid_until_dt > NOW() OR valid_until_dt IS NULL ) ';
        $sql .= 'LIMIT 1';

        return self::findOne( $sql, array( ':team_id' => $team->id ) );
    }

    /**
     * @param $braintreeSubscriptionId
     * @return NULL|static
     */
    public static function getByBraintreeSubscriptionId( $braintreeSubscriptionId )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'braintree_subscription_id = :braintreeSubscriptionId LIMIT 1';
        return self::findOne( $sql, array( ':braintreeSubscriptionId' => $braintreeSubscriptionId ) );
    }


    /**
     * @param TB_Shared_Ent_Core_Account $account
     * @param TB_Shared_Ent_TeamData_Team $team
     * @return array
     */
    public static function getValidPremiumLogsForAccountAndTeam( TB_Shared_Ent_Core_Account $account, TB_Shared_Ent_TeamData_Team $team )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'team_id = :team_id AND account_id = :account_id AND ';
        $sql .= "( status = '" . TB_Shared_Ent_TeamData_PremiumLog::STATUS_ACTIVE . "' OR status = '" . TB_Shared_Ent_TeamData_PremiumLog::STATUS_PENDING . "' ) AND ";
        $sql .= '( valid_until_dt > NOW() OR valid_until_dt IS NULL )';

        return self::findMany( $sql, array( ':team_id' => $team->id, ':account_id' => $account->id ) );
    }


    public static function getAllPremiumLogsForAccount( TB_Shared_Ent_Core_Account $account )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'account_id = :account_id ORDER BY created_dt DESC';
        return self::findMany( $sql, array( ':account_id' => $account->id ) );
    }

    public static function getLastPremiumLogForTeam( TB_Shared_Ent_TeamData_Team $team )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'status != "' . self::STATUS_DELETED . '" AND ';
        $sql .= 'team_id = :team_id ';
        $sql .= 'ORDER BY -valid_until_dt ASC LIMIT 0,1';

        return self::findOne( $sql, array( ':team_id' => $team->id ) );
    }

    public static function getAllPremiumLogsForAccountWithStatus( TB_Shared_Ent_Core_Account $account, $status )
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'account_id = :account_id AND status = :status';

        return self::findMany( $sql, array( ':account_id' => $account->id, ':status' => $status ) );
    }

    /**
     * @return array
     */
    public static function getAllOutdated()
    {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ';
        $sql .= 'valid_until_dt IS NOT NULL AND ';
        $sql .= 'valid_until_dt < NOW() AND ';
        $sql .= '( status = "' . self::STATUS_ACTIVE . '" OR status = "' . self::STATUS_CANCELLED . '")';

        return self::findMany( $sql );
    }

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

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