<?php

namespace Hura8\Components\Deal\AdminController;

use Hura8\Components\Deal\Controller\bDealController;
use Hura8\Interfaces\iEntityAdminController;
use Hura8\Traits\AdminEntityBaseControllerTraits;

class ADealController extends bDealController  implements iEntityAdminController
{

    use AdminEntityBaseControllerTraits;


    public function autoRenew() {
        $deal_list = $this->objDealModel->getAllAutoRenewableDeal();

        //then renew & log history
        foreach ($deal_list as $info) {
            $this->renewDeal($info['id'], $info['from_time'], $info['to_time'], $info['auto_renew_history']);
        }
    }

    protected function renewDeal($id, $from_time, $to_time, array $auto_renew_history) {
        /*$obj_from_date  = new \DateTime(date("Y-m-d", $from_time));
        $obj_to_date    = new \DateTime(date("Y-m-d", $to_time));
        $day_diff       = $obj_to_date->diff($obj_from_date)->format('%a');

        $obj_to_date->add(new \DateInterval('P'.$day_diff.'D'));
        $new_date = $obj_to_date->format('Y-m-d');

        $current_date       = date("Y-m-d");
        $from_time_minute   = ($from_time > 0) ? date("H:i", $from_time) : "00:00";
        $to_time_minute     = ($to_time > 0) ? date("H:i", $to_time) : "00:00";*/

        $to_time_new = CURRENT_TIME + ($to_time - $from_time);

        $auto_renew_history[] = [
            "renew_time"    => show_datetime_from_unix(CURRENT_TIME),
            "from_time"     => show_datetime_from_unix(CURRENT_TIME),
            "to_time"       => show_datetime_from_unix($to_time_new),
        ];

        return $this->updateFields( $id,
            [
                'from_time' => CURRENT_TIME,
                'to_time' => $to_time_new,
                'auto_renew_history' => serialize($auto_renew_history),
            ]
        );
    }


    protected function deleteFileBeforeDeleteItem($item_id): bool
    {
        return true;
    }
}
