<?php

namespace Flyerimage\App\Services;

use Flyerimage\App\Constants\IRepeatKaaz;
use Flyerimage\App\Constants\ITables;
use Flyerimage\App\Exceptions\AlreadyExistException;
use Flyerimage\App\Exceptions\ServiceException;
use DateTime;
use Exception;
use Flyerimage\Core\Database\QueryBuilder;

/**
 * Repeat kaaz service
 */
class KaazService extends AbstractService
{    

    /**
     * Repeat kaaz service
     * 
     * @var App\Services\RepeatKaazService $repeat_kaaz_service
     */
    protected $repeat_kaaz_service;

    /**
     * Service class constructor
     */
    public function __construct()
    {
        parent::__construct(ITables::$KAAZS);
    }

    /**
     * Generate upcoming kaaz
     * 
     * @param string
     * 
     * @return void
     */
    public function upcoming_kaaz()
    {
        $repeat_kaaz_list = $this->repeat_kaaz_service->get_all();
        foreach($repeat_kaaz_list as $repeat_kaaz) {
            try {
                // create a daily kaaz based on repeat kaaz
                if ($repeat_kaaz->repeat_policy === IRepeatKaaz::$POLICY_DAILY) {
                    $start_time_string = gmdate("Y-m-d");
                    $start_time_string .= " " . $repeat_kaaz->start_time;
                    $start_time_obj = new DateTime($start_time_string);
                    $start_time = date_format($start_time_obj, 'Y-m-d H:i:s');

                    $end_time_string = gmdate("Y-m-d");
                    $end_time_string .= " " . $repeat_kaaz->end_time;
                    $end_time_obj = new DateTime($end_time_string);
                    $end_time = date_format($end_time_obj, 'Y-m-d H:i:s');
                            
                    $n_daily_kaaz = [
                        'name'           => $repeat_kaaz->name,
                        'kaaz_type_id'   => $repeat_kaaz->kaaz_type_id,
                        'repeat_kaaz_id' => $repeat_kaaz->id,
                        'start_time'     => $start_time,
                        'end_time'       => $end_time,
                        'is_completed'   => false
                    ];
                    $this->create($n_daily_kaaz);
                }
            }catch(\Exception $ex) {
                //var_dump($ex->getMessage());
            }
        }
    }

     /**
     * Get all the records of repeat kaaz
     * 
     * @return array|null
     */
    public function get_all()
    {
        return QueryBuilder::table($this->table_name, 'rk')
                ->inner_join(ITables::$KAAZ_TYPES, 'kt', 'kt.id = rk.kaaz_type_id')
                ->select(['rk.*', 'kt.name as kaaz_type_name'])
                ->order_by('rk.created_at desc')
                ->get();
    }

    /**
     * Get the records based on parameters
     * 
     * @param int $page - default 1
     * @param int $limit - default 10
     * 
     * @return array|null
     */
    public function get_by($page = 1, $limit = 10)
    {
        return QueryBuilder::table($this->table_name, 'rk')
                ->inner_join(ITables::$KAAZ_TYPES, 'kt', 'kt.id = rk.kaaz_type_id')
                ->select(['rk.*', 'kt.name as kaaz_type_name'])
                ->order_by('rk.created_at desc')
                ->page($page)
                ->limit($limit)
                ->pagination();
    }
    
    /**
     * Create the repeat_kaazs
     * 
     * @param array $args
     * 
     * @return int
     */
    function create($args = [])
    {
        $exist = QueryBuilder::table($this->table_name)
                ->where(['name' => $args['name']])
                ->first();
        
        if($exist) {
            throw new Exception(esc_html__('Already exist!', 'flyerimage'));
        }

        $defaults = [
            'created_by' => get_current_user_id(),
            'created_at' => current_time('mysql')
        ];

        $data = wp_parse_args($args, $defaults);
        if (isset($data['id'])) {
            unset($data['id']);
        }

        $id = QueryBuilder::table($this->table_name)->create($data);        

        if ($id === null) {
            throw new Exception(esc_html__('Failed to insert data!', 'flyerimage'));
        }
        return $id;
    }

    /**
     * Update the repeat_kaazs
     * 
     * @param array $args
     */
    function update($args = [])
    {
        $exist = QueryBuilder::table($this->table_name, 'rk')
                ->where(['name' => $args['name']])
                ->where(['id', '!=', $args['id']])
                ->first();
        
        if($exist) {
            throw new Exception(esc_html__('Already exist!', 'flyerimage'));
        }

        $defaults = [
            'updated_by' => get_current_user_id(),
            'updated_at' => current_time('mysql')
        ];
        $data = wp_parse_args($args, $defaults);

        QueryBuilder::table($this->table_name)->save($data);
    }

    /**
     * delete the repeat_kaazs
     * 
     * @param integer $id
     *
     */
    function delete($id)
    {
        $exist = QueryBuilder::table($this->table_name)->find_by_id($id);
        if(!$exist) {
            throw new Exception(esc_html__('Not found!', 'flyerimage'));
        }
        QueryBuilder::table($this->table_name)->delete($id);
    }
}
