<?php

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\I18n\Time;

class InvoicesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('Timestamp');
        $this->belongsTo('Users');
    }

    public function successPayment($invoice = null)
    {
        if (!$invoice) {
            return false;
        }

        $user = $this->Users->find()->contain(['Plans'])->where(['Users.id' => $invoice->user_id])->first();

        // Plans
        if ($invoice->type === 1) {
            $this->Users = TableRegistry::get('Users');

            if ($invoice->status === 1) {
                $plan_expiration = new Time($user->expiration);
                if ($plan_expiration->isPast()) {
                    $plan_expiration = Time::now();
                }

                $payment_period = unserialize($invoice->data)['payment_period'];
                $expiration = $plan_expiration->addYear();
                if ($payment_period === 'm') {
                    $expiration = $plan_expiration->addMonth();
                }
                $user->expiration = $expiration;
                $user->plan_id = $invoice->rel_id;

                $this->Users->save($user);

                if (isset($_SESSION['Auth']['User']['id'])) {
                    if ($_SESSION['Auth']['User']['id'] === $user->id) {
                        $data = $this->Users->find()->contain(['Plans'])
                            ->where(['Users.id' => $user->id])
                            ->first()
                            ->toArray();
                        unset($data['password']);
                        //\Cake\Log\Log::write('debug', $data);
                        //$this->Auth->setUser($data);
                        $_SESSION['Auth']['User'] = $data;
                    }
                }
            }
        }

        // Campaigns
        if ($invoice->type === 2) {
        }

        // Wallet
        if ($invoice->type === 3) {
        }
        return true;
    }
}
