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

use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Sanitize;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Hermes\Functions\Route as Routes;

class MembershipCustomers extends DatabaseModel {
    public static $stripe;
    public $tableName = 'membership_customers';

    public $databaseMatrix = [
        [ 'stripe_customer', 'varchar', '155' ],
        [ 'local_user', 'varchar', '155' ],
        // renews?
        //     does this renew periodically?
        // renewal period?
        //     if periodic, how frequent?
        // renewal type?
        //     automatic, manual review, paid
    ];

    public function __construct() {
        parent::__construct();
    }
    
    public function findByUserID( $d ) {
        $data = self::$db->get( $this->tableName, [ 'local_user', '=', $d ] );
        if ( ! $data->count() ) {
            return false;
        }
        return $data->first();
    }
    
    public function findByCustomerID( $d ) {
        $data = self::$db->get( $this->tableName, [ 'stripe_customer', '=', $d ] );
        if ( ! $data->count() ) {
            return false;
        }
        return $data->first();
    }
    
    public function create( $user_id ) {
        $data = self::$db->get( 'users', ['ID', '=', $user_id] );
        if ( !$data->count() ) {
            Debug::warn( "customer cannot be created, user not found" );
            return false;
        }
        $user = $data->first();
        $user_email = $user->email;
        $user_name = $user->name;

        $api_key = Config::getValue( 'memberships/stripeSecret' );
        if ( $api_key == 'sk_xxxxxxxxxxxxxxx' || empty($api_key) ) {
            Debug::error( "MembershipCustomers:create No Stripe Key found" );
            return false;
        }
        self::$stripe = new \Stripe\StripeClient( $api_key );

        $customer = self::$stripe->customers->create([
          'name' => $user_name,
          'email' => $user_email,
        ]);

        $fields = [
            'stripe_customer' => $customer->id,
            'local_user' => $user_id,
        ];
        
        if ( !self::$db->insert( $this->tableName, $fields ) ) {
            Debug::error( "Membership Customer: $data not added: $fields" );
            new customException( 'membershipCustomerCreate' );
            return false;
        }

        return $customer;
    }

    public function findOrCreate( $user_id ) {
        $user =  $this->findByUserID( $user_id );
        if ( ! empty( $user ) ) {
            return $user->stripe_customer;
        }
        $user = $this->create( $user_id );
        if ( ! empty( $user ) ) {
            return $user->id;
        }
    }
}