<?php
require_once('Model/Queries.php');
require_once('Model/Constant.php');
require_once('Utility/Utility.php');
require_once('Object/DataObject.php');
require_once('Object/ResponseObject.php');
require_once('QueryBuilder/QueryDecorator.php');
require_once('QueryBuilder/CustomQuery.php');
require_once('../../config.php');

class OverView extends Constant implements Queries
{
    private $dtObject;
    private $actionType = "";
    private $functionalityType = "";
    public $postData = [];


    function __construct(DataObject $dtObject)
    {

        parent::__construct();
        $this->dtObject = $dtObject;
        $this->actionType = $dtObject->getActionType();
        $this->functionalityType = $dtObject->getFunctionalityType();
        $this->postData = $dtObject->getPostData();


    }

    public function create(){
        $listOfData = [];
        $postdata = $this->postData;
//        Utility::DebugObject($postdata);
        if ($this->functionalityType == "insert_about_us") {
            $subjectname = "About Us";
            $Detail = isEmpty($postdata['ckeditor']);

            $queryDecorator = new QueryDecorator("INSERT INTO AboutUs(subject,detail)VALUES('$subjectname','$Detail')", new CustomQuery("0", ""));

            $result = queryRunner($queryDecorator->getQuery());
            if ($result != null) {
                session_start();
                header("location:../admin_panel/index.php");

            }

            return $this->getResponseObject("202", "Data insert Successfully", "1", "0", $listOfData);


        }
        else if ($this->functionalityType == "insert_Privacy_policy") {
            $subjectname = "Privacy Policy";
            $Detail = isEmpty($postdata['ckeditor']);

            $queryDecorator = new QueryDecorator("INSERT INTO PrivacyPolicy(subject,detail)VALUES('$subjectname','$Detail')", new CustomQuery("0", ""));

            $result = queryRunner($queryDecorator->getQuery());
            if ($result != null) {
                session_start();
                header("location:../admin_panel/index.php");

            }

            return $this->getResponseObject("202", "Data insert Successfully", "1", "0", $listOfData);


        }

    }

    public function retrieve(){
        $listOfData = [];
        $postdata = $this->postData;
        if ($this->functionalityType == "retrieve_ten_brands") {


            $queryDecorator = new QueryDecorator("SELECT  order_meta.id as order_id, brand.*,brand.name as brand_name,brand.image as brand_image , sales.total_sales as t_sale , rating.avg_rating as t_avg
            , SUM(CASE
                WHEN order_meta.discounted_amount <= '0' THEN order_meta.quantity * order_meta.price
                ELSE order_meta.discounted_amount
                END
             ) as total_amount , (SELECT meta_value_02 FROM Configuration ) as currency_symbol
         FROM OrderMeta as order_meta
         INNER JOIN Brand as brand ON brand.id = order_meta.brand_id
         INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id
    
         LEFT JOIN (

            SELECT brand_id  , count(*)
 as total_sales FROM OrderMeta

         ) sales ON sales.brand_id = order_meta.brand_id
         LEFT JOIN (

            SELECT brand_id  , AVG(rating) as avg_rating FROM Rating

         ) rating ON rating.brand_id = order_meta.brand_id

         WHERE DATE(list_of_orders.date_created) >= DATE_SUB(CURDATE(), INTERVAL 2 MONTH)
         GROUP BY brand.id
         LIMIT 10

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['order_id'],
                    'brand_name' => $row['brand_name'],
                    'brand_image' => $this->brand_image_path.$row['brand_image'],
                    'sale' => $row['t_sale'],
                    'total_amount' => ceil($row['total_amount']),
                    'currency_symbol' => $row['currency_symbol'],
                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "retrieve_ten_brands_trending") {


            $queryDecorator = new QueryDecorator("SELECT  order_meta.id as order_id, brand.*,brand.name as brand_name,brand.image as brand_image , sales.total_sales as t_sale , rating.avg_rating as t_avg
            , SUM(CASE
                WHEN order_meta.discounted_amount <= '0' THEN order_meta.quantity * order_meta.price
                ELSE order_meta.discounted_amount
                END
             ) as total_amount , (SELECT meta_value_02 FROM Configuration ) as currency_symbol
         FROM OrderMeta as order_meta
         INNER JOIN Brand as brand ON brand.id = order_meta.brand_id
         INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id
         LEFT JOIN (

            SELECT brand_id  , count(*)
 as total_sales FROM OrderMeta

         ) sales ON sales.brand_id = order_meta.brand_id
         LEFT JOIN (

            SELECT brand_id  , AVG(rating) as avg_rating FROM Rating

         ) rating ON rating.brand_id = order_meta.brand_id

         WHERE DATE(list_of_orders.date_created) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
         GROUP BY brand.id
         LIMIT 10

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['order_id'],
                    'brand_name' => $row['brand_name'],
                    'brand_image' => $this->brand_image_path.$row['brand_image'],
                    'sale' => $row['t_sale'],
                    'total_amount' => ceil($row['total_amount']),
                    'currency_symbol' => $row['currency_symbol'],
                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "retrieve_ten_products") {


            $queryDecorator = new QueryDecorator("SELECT  product.*,product.name as pro_name,product.image as pro_image , sales.total_sales as t_sale , rating.avg_rating as t_avg
         , SUM(CASE
                WHEN order_meta.discounted_amount <= '0' THEN order_meta.quantity * order_meta.price
                ELSE order_meta.discounted_amount
                END
             ) as total_amount , (SELECT meta_value_02 FROM Configuration ) as currency_symbol  
         FROM OrderMeta as order_meta
         INNER JOIN Product as product ON product.id = order_meta.product_id
         INNER JOIN Brand as brand ON brand.id = order_meta.brand_id
         INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id
         LEFT JOIN (

            SELECT brand_id  , count(*) as total_sales FROM OrderMeta

         ) sales ON sales.brand_id = order_meta.brand_id
         LEFT JOIN (

            SELECT product_id  , AVG(rating) as avg_rating FROM Rating

         ) rating ON rating.product_id = order_meta.product_id

         WHERE DATE(list_of_orders.date_created) >= DATE_SUB(CURDATE(), INTERVAL 2 MONTH)
         GROUP BY product.id
         LIMIT 10

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
                    'pro_name' => $row['pro_name'],
                    'pro_image' => $this->products_images.$row['pro_image'],
                    'sale' => $row['t_sale'],
                    'total_amount' => ceil($row['total_amount']),
                    'currency_symbol' => $row['currency_symbol'],
                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "retrieve_ten_trending_products") {


            $queryDecorator = new QueryDecorator(" SELECT  product.*,product.name as pro_name,product.image as pro_image , sales.total_sales as t_sale , rating.avg_rating as t_avg
          , SUM(CASE
                WHEN order_meta.discounted_amount <= '0' THEN order_meta.quantity * order_meta.price
                ELSE order_meta.discounted_amount
                END
             ) as total_amount , (SELECT meta_value_02 FROM Configuration ) as currency_symbol  
         FROM OrderMeta as order_meta
         INNER JOIN Product as product ON product.id = order_meta.product_id
         INNER JOIN Brand as brand ON brand.id = order_meta.brand_id
         INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id
         LEFT JOIN (

            SELECT brand_id  , count(*)
 as total_sales FROM OrderMeta

         ) sales ON sales.brand_id = order_meta.brand_id
         LEFT JOIN (

            SELECT product_id   , AVG(rating) as avg_rating FROM Rating

         ) rating ON rating.product_id = order_meta.product_id

         WHERE DATE(list_of_orders.date_created) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
         GROUP BY product.id
         LIMIT 10

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
                    'pro_name' => $row['pro_name'],
                    'pro_image' => $this->products_images.$row['pro_image'],
                    'sale' => $row['t_sale'],
                    'total_amount' => ceil($row['total_amount']),
                    'currency_symbol' => $row['currency_symbol'],
                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "retrieve_top_customer") {


            $queryDecorator = new QueryDecorator(" SELECT  user.*,user.first_name as user_name,user.picture as user_image , sales.total_sales as t_sale , rating.avg_rating as t_avg,
SUM(CASE
                WHEN list_of_orders.discounted_amount <= '0' THEN list_of_orders.shipping_fee + list_of_orders.total_amount
                ELSE list_of_orders.discounted_amount
                END
             ) as total_amount , (SELECT meta_value_02 FROM Configuration ) as currency_symbol  
         FROM ListOfOrders as list_of_orders 
         INNER JOIN User as user ON user.id = list_of_orders.user_id
         LEFT JOIN (
         
            SELECT user_id  , count(*) as total_sales FROM ListOfOrders
         
         ) sales ON sales.user_id = list_of_orders.user_id
         LEFT JOIN (
         
            SELECT user_id  , AVG(rating) as avg_rating FROM Rating
         
         ) rating ON rating.user_id = list_of_orders.user_id
         
         WHERE DATE(list_of_orders.date_created) >= DATE_SUB(CURDATE(), INTERVAL 2 MONTH) 
         GROUP BY user.id
         LIMIT 10

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
                    'user_name' => $row['user_name'],
                    'user_image' => $this->userimages.$row['user_image'],
                    'sale' => $row['t_sale'],
                    'total_amount' => ceil($row['total_amount']),
                    'currency_symbol' => $row['currency_symbol'],
                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "total_revenue") {


            $queryDecorator = new QueryDecorator(" SELECT  order_meta.*
         , SUM(CASE
                WHEN order_meta.discounted_amount <= '0' THEN order_meta.quantity * order_meta.price
                ELSE order_meta.discounted_amount
                END
             ) as total_amount , (SELECT meta_value_02 FROM Configuration ) as currency_symbol  
         FROM OrderMeta as order_meta
         INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id
        
         WHERE list_of_orders.status= '2'
        
      

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
//                    'user_name' => $row['user_name'],
//                    'user_image' => $this->userimages.$row['user_image'],
//                    'sale' => $row['t_sale'],
                    'total_amount' => ceil($row['total_amount']),
                    'currency_symbol' => $row['currency_symbol'],
//                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "total_order") {


            $queryDecorator = new QueryDecorator(" SELECT  order_meta.*,count(*) as total_order
         
         FROM OrderMeta as order_meta
         INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id
        
         WHERE list_of_orders.status= '2'
        
      

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
//                    'user_name' => $row['user_name'],
//                    'user_image' => $this->userimages.$row['user_image'],
//                    'sale' => $row['t_sale'],
                    'total_order' => ceil($row['total_order']),
//                    'currency_symbol' => $row['currency_symbol'],
//                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "total_brands") {


            $queryDecorator = new QueryDecorator(" SELECT  count(*) as total_brand FROM Brand as brand
         
        
      

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
//                    'user_name' => $row['user_name'],
//                    'user_image' => $this->userimages.$row['user_image'],
//                    'sale' => $row['t_sale'],
                    'total_brand' => ceil($row['total_brand']),
//                    'currency_symbol' => $row['currency_symbol'],
//                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "no_of_users") {


            $queryDecorator = new QueryDecorator(" SELECT  count(*) as total_user FROM User as user
         
        
      

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
//                    'id' => $row['order_id'],
//                    'user_name' => $row['user_name'],
//                    'user_image' => $this->userimages.$row['user_image'],
//                    'sale' => $row['t_sale'],
                    'total_user' => ceil($row['total_user']),
//                    'currency_symbol' => $row['currency_symbol'],
//                    'avg' => $row['t_avg'],




                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }
        else if ($this->functionalityType == "get_order_response") {


            $queryDecorator = new QueryDecorator(" SELECT count(*) as total_order FROM ListOfOrders    WHERE DATE(date_created) >= DATE_SUB(CURDATE(), INTERVAL 5 MINUTE)
        
      

            ", new CustomQuery("0", ""));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'total_order' => ceil($row['total_order'])
                ]);
            }
            return $this->getResponseObject("202", "Data Retrieve Successfully", "1", "0", $listOfData);
        }


    }

    public function update(){
        $listOfData = [];
        $postdata = $this->postData;
        if ($this->functionalityType == "update_enable_data") {
            $id = $postdata['id'];
            $enable = $postdata['enable_val'];


            $queryDecorator = new QueryDecorator("UPDATE BrandMonthlyEarning SET status='$enable'", new CustomQuery("0", " WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(), "Data update Successfully", "1", "0", $listOfData, '');

        }


    }

    public function delete(){



    }

    public function getResponseObject($code, $message, $setId, $itemCount, $data, $pagination = null)
    {

        $responseObject = new ResponseObject();
        $responseObject->setCode($code);
        $responseObject->setMessage($message);
        $responseObject->setId($setId);
        $responseObject->setListOfData($data);
        $responseObject->setTotalCount($itemCount);
        $responseObject->setPagination($pagination);

        return $responseObject;

    }
    public function getRefundDetail($id){

        ///Utility::Logger("Meta Id = ".$id);
        $listOfData = [];

        $query = " SELECT * FROM RefundMeta 
         where term_id = '$id'";
        $result = queryRunner($query);
        while ($row = rowRetriever($result)) {

            array_push($listOfData, [
                'id' => $row['id'],
                'image' => $this->refundimages.$row['image'],
            ]);
        }

        return $listOfData;


    }
}