<?php
require '../Helpers.php';

header("Access-Control-Allow-Origin: https://abhishek-sir.myshopify.com");


header("Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE");

header("Access-Control-Allow-Headers: Content-Type, Authorization");

header('Content-Type: application/json');

$data = file_get_contents("php://input");

// Decode the JSON data into a PHP array
$customerData = json_decode($data, true);

if (isset($customerData['id'])) {
    $customer_id = $customerData['id'];
    $client = graphql();

    $query = <<<QUERY
        query {
            customer(id: "gid://shopify/Customer/$customer_id") {
            id
            tags
        }

    }

    QUERY;
    $response = $client->query(['query' => $query]);

    $result = $response->getDecodedBody();



    if (isset($result['data']['customer']) && !empty($result['data']['customer'])) {

        if (in_array('audio_listened', $result['data']['customer']['tags'])) {
            $query = <<<QUERY
            mutation MyMutation {
                tagsRemove(id: "gid://shopify/Customer/$customer_id", tags: "audio_listened") {
                        node {
                        id
                        }
                    }
            }

            QUERY;

            $response = $client->query(['query' => $query]);
            $deleteTagResult = $response->getDecodedBody();
            // dd($deleteTagResult);
        }

        
        $query = <<<QUERY
        query {
             customer(id: "gid://shopify/Customer/$customer_id") {
                metafield(key: "custom.audio_listened") {
                id
                key
                description
                createdAt
                namespace
                }
            }
    
        }
    
        QUERY;
        $response = $client->query(['query' => $query]);

        $getMetafield = $response->getDecodedBody();
        if (isset($getMetafield['data']['customer']['metafield']) && !empty($getMetafield['data']['customer']['metafield'])) {
            $metafield_id = $getMetafield['data']['customer']['metafield']['id'];
            $query = <<<QUERY
                mutation MyMutation {
                metafieldDelete(input: {id: "$metafield_id"}) {
                    deletedId
                }
            }
    
           QUERY;

            $response = $client->query(['query' => $query]);
            $deleteResult = $response->getDecodedBody();
            // dd($deleteResult);
            if (isset($deleteResult['data']['metafieldDelete']['deletedId']) && !empty($deleteResult['data']['metafieldDelete']['deletedId'])) {


                $response = [
                    'status' => true,
                    'message' => 'Metafields and Tags Are Deleted Successfully'
                ];

                echo json_encode($response);
            }
        }
    } else {
        $response = [
            'status' => false,
            'message' => 'Customer Not Found'
        ];
        echo json_encode($response);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Customer ID not provided']);
}
