<?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 Dispute 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_all_diepute") {

            $search = $postdata['search'];

            $queryDecorator = new QueryDecorator("SELECT disputerequest.* ,disputerequest.id as dispute_id,disputerequest.status as dipute_status,orderdetail.*,orderdetail.price as order_price
        ,orderdetail.coupon_id as coupons,orderdetail.discounted_amount as discount,productdata.*,productdata.name as pro_name,productdata.image as pro_image,branddata.*,branddata.name as brand_name
        ,users.*,users.first_name as user_name
            FROM Dispute as disputerequest 
            INNER JOIN OrderMeta as orderdetail ON orderdetail.id = disputerequest.term_id
            INNER JOIN Product as productdata ON productdata.id = orderdetail.product_id
            INNER JOIN Brand as branddata ON branddata.id = orderdetail.brand_id
            INNER JOIN User as users  ON users.id = disputerequest.user_id", new CustomQuery("0", ""));

            if (!empty($search)){

                $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                    new CustomQuery("0", " WHERE productdata.name LIKE '$search%' OR users.first_name LIKE '$search%'"));

            }

            //<editor-fold desc="Total Data Counting functionality">

            $queryDecoratorCount = new QueryDecorator($queryDecorator->getQuery(),new CustomQuery("1", "SELECT count(*) as total_count ,"));
            $resultCount = queryRunner($queryDecoratorCount->getQuery());
            $rowObject = rowRetrieverObject($resultCount);
            $rowCount = $rowObject->total_count;
            if ($rowCount > 10) {
                $rowCount = ceil($rowCount / 10);
            }
            else{
                $rowCount = 1;
            }


            //</editor-fold>

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

                array_push($listOfData, [
                    'id' => $row['dispute_id'],
                    'user_name' => $row['user_name'],
                    'product_name' => $row['pro_name'],
                    'pro_image' => $this->products_images.$row['pro_image'],

                    'brand_name' => $row['brand_name'],
                    'discounted_amount' => ceil($row['discount']),
                    'order_price' => $row['order_price'],
                    'coupons_id' => $row['coupons'],
                    'dipute_status' => $row['dipute_status'],




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

            $search = $postdata['search'];
            $offset = $postdata['offset'];

            $queryDecorator = new QueryDecorator("SELECT disputerequest.* ,disputerequest.id as dispute_id,disputerequest.status as dipute_status,orderdetail.*,orderdetail.price as order_price
        ,orderdetail.coupon_id as coupons,orderdetail.discounted_amount as discount,productdata.*,productdata.name as pro_name,productdata.image as pro_image,branddata.*,branddata.name as brand_name
        ,users.*,users.first_name as user_name
            FROM Dispute as disputerequest 
            INNER JOIN OrderMeta as orderdetail ON orderdetail.id = disputerequest.term_id
            INNER JOIN Product as productdata ON productdata.id = orderdetail.product_id
            INNER JOIN Brand as branddata ON branddata.id = orderdetail.brand_id
            INNER JOIN User as users  ON users.id = disputerequest.user_id", new CustomQuery("0", ""));

            if (!empty($search)){

                $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                    new CustomQuery("0", " WHERE productdata.name LIKE '$search%' OR users.first_name LIKE '$search%'"));

            }



            $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),
                new CustomQuery("0", " LIMIT 10 OFFSET ".$offset));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['dispute_id'],
                    'user_name' => $row['user_name'],
                    'product_name' => $row['pro_name'],
                    'pro_image' => $this->products_images.$row['pro_image'],

                    'brand_name' => $row['brand_name'],
                    'discounted_amount' => ceil($row['discount']),
                    'order_price' => $row['order_price'],
                    'coupons_id' => $row['coupons'],
                    'dipute_status' => $row['dipute_status'],




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

    }

    public function update(){
        $listOfData = [];
        $postdata = $this->postData;
        if ($this->functionalityType == "update_accept_data") {
            $id=$postdata['id'];
            $responce=$postdata['status'];



            $queryDecorator = new QueryDecorator("UPDATE Dispute SET status='1'",new CustomQuery("0"," WHERE id = '$id'"));
            $result = queryRunner($queryDecorator->getQuery());
            
            $queryDecorator = new QueryDecorator("SELECT order_meta.price as product_price, list_of_orders.transaction_token as token
            FROM Dispute as dispute
            INNER JOIN OrderMeta as order_meta ON order_meta.id = dispute.term_id
            INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.id = order_meta.term_id",
                new CustomQuery("0"," WHERE dispute.id = '$id'"));
            $result = queryRunner($queryDecorator->getQuery());
            $row = rowRetrieverArray($result);
            $price = $row['product_price'];
            $token = $row['token'];
            
            create_refund_request_in_stripe($token,$price);

            return $this->getResponseObject($this->getSuccess_code(),"Data update Successfully","1","0",$listOfData,'');
        }
        else if ($this->functionalityType == "update_reject_data") {
            $id=$postdata['id'];
            $responce=$postdata['status'];



            $queryDecorator = new QueryDecorator("UPDATE Dispute SET status='1'",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;


    }
}