<?php
declare(strict_types=1);
/**
 * Cart model for GForce delivery integration
 * @category    PortalPeople
 * @package     GForce
 * @author      Portal People Development Team
 */
namespace PortalPeople\gforce\models;

use Yii;
use yii\helpers\{Inflector, Json};
use \backend\models\{ActionLog, Accounts};

class Cart extends \backend\models\Cartsbe
{
    public static function markDelivered(Cart $cart, ?string $date)
    {
        if ($cart) {
            $cart->data->gforce->status = 'Delivered';
            
            $date = \DateTime::createFromFormat('Y/m/d H:i:s', $date);
            $cart->data->gforce->arrived_date = $date->format('Y-m-d H:i:s');
            if ($cart->save()) {
                ActionLog::create(['class' => self::class, 'action' => 'markDelivered', 'type' => 'Info', 'relatedid' => $cart->id, 'message' => "GForce - Marked cart #{$cart->orderref} as Delivered"]);
                Yii::$app->trigger('netsuite.delivered', new \common\events\UserDataEvent(['model' => $cart, 'customData' => ['Delivered']]));
            }
        }
    }
    
    public static function Dispatched(Cart $cart, ?string $date)
    {
        if ($cart) {
            $cart->data->gforce->status = 'Dispatched';
            
            $date = \DateTime::createFromFormat('Y/m/d H:i:s', $date);
            $cart->data->gforce->despatch_date = $date->format('Y-m-d H:i:s');
            if ($cart->save()) {
                ActionLog::create(['class' => self::class, 'action' => 'markDispatched', 'type' => 'Info', 'relatedid' => $cart->id, 'message' => "GForce - Marked cart #{$cart->orderref} as Dispatched"]);
                Yii::$app->trigger('cart-dispatched-'.$cart->account_code, new \common\events\UserDataEvent(['model' => $cart, 'customData' => ['new' => true]]));
         //       Yii::$app->trigger('cart-dispatch-email-'.$cart->account_code, new \common\events\UserDataEvent(['model' => $cart, 'customData' => ['new' => true]]));
                Yii::$app->trigger('netsuite.dispatched', new \common\events\UserDataEvent(['model' => $cart, 'customData' => ['Dispatched']]));
            }
        }
    }

    public static function markInProgress(Cart $cart, ?string $date)
    {
        if ($cart) {
            $cart->data->gforce->status = 'Out for Delivery';
            if ($date) {
                $date = \DateTime::createFromFormat('Y/m/d H:i:s', $date);
                $cart->data->gforce->arrived_date = $date->format('Y-m-d H:i:s');
            }
            echo $cart->save() . PHP_EOL;
            Yii::$app->trigger('netsuite.inProgress', new \common\events\UserDataEvent(['model' => $cart, 'customData' => ['Out for Delivery']]));
        }
    }

    public static function findTodaysConsignments() {
        $json = new yii\db\Expression("json_value(data, '$.gforce.status')");
        return self::find()
            ->where(['delivery_date' => '2026-06-11 00:00:00'])
            //->andWhere(['or', ['not like', $json, 'Delivered'], ['=', $json, null]])
            ->all();
    }
    public static function findConsignmentsDays(int $days) {
        $json = new yii\db\Expression("json_value(data, '$.gforce.status')");
        return self::find()
            ->where(['<=', 'delivery_date', date('Y-m-d H:i:s', strtotime("+$days days"))])
            ->andWhere(['or', ['not like', $json, 'Delivered'], ['=', $json, null]])
            ->all();
    }
}
