<?php
/**
 * app/plugins/subscribe/models/subscribe.php
 *
 * This class is used for the manipulation of the subscribers database table.
 *
 * @package TP Subscribe
 * @version 5.0.1
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Models;

use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Code;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;

class Subscribe extends DatabaseModel {
    public $tableName = 'subscribers';
    public $databaseMatrix = [
        [ 'confirmed', 'int', '1' ],
        [ 'subscribed', 'int', '10' ],
        [ 'confirmationCode', 'varchar', '80' ],
        [ 'email', 'varchar', '75' ],
    ];

    /**
     * Adds an email to the subscribers database.
     *
     * @param string $email - the email you are trying to add.
     * @return bool
     */
    public function add( $email ) {
        if ( !Check::email( $email ) ) {
            return false;
        }
        $alreadyExists = self::$db->get( $this->tableName, ['email', '=', $email] );

        if ( $alreadyExists->error() ) {
            Debug::info( 'Error querying database: ' . $alreadyExists->errorMessage() );
            return false;
        }

        if ( $alreadyExists->count() ) {
            Debug::info( 'email already subscribed.' );
            return false;
        }
        $fields = [
            'email' => $email,
            'confirmationCode' => Code::genConfirmation(),
            'confirmed' => 0,
            'subscribed' => time(),
        ];
        self::$db->insert( $this->tableName, $fields );
        return self::$db->lastId();
    }

    /**
     * Removes an email from the subscribers database.
     *
     * @param string $email - The email you are trying to remove.
     * @param string $code - The confirmation code to unsubscribe.
     * @return boolean
     */
    public function unsubscribe( $email, $code ) {
        if ( !Check::email( $email ) ) {
            return false;
        }
        $user = self::$db->get( $this->tableName, ['email', '=', $email, 'AND', 'confirmationCode', '=', $code] );
        if ( !$user->count() ) {
            Debug::info( __METHOD__ . ' - Cannot find subscriber with that email and code' );
            return false;
        }
        self::$db->delete( $this->tableName, ['ID', '=', $user->first()->ID] );
        return true;
    }

    /**
     * Returns a subscriber object for the provided email address.
     *
     * @param  string $email - An email address to look for.
     * @return bool|object - Depending on success.
     */
    public function get( $email ) {
        if ( !Check::email( $email ) ) {
            return false;
        }
        $data = self::$db->get( $this->tableName, ['email', '=', $email] );
        if ( !$data->count() ) {
            Debug::info( __METHOD__ . ' - Email not found' );
            return false;
        }
        return (object) $data->first();
    }
}
