<?php
header('Content-Type: application/json'); // Set content type to JSON

$response = [];

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $jsonFilePath = '../../database/settings/profile.json';

    // Load existing data
    $existingData = [];
    if (file_exists($jsonFilePath)) {
        $existingData = json_decode(file_get_contents($jsonFilePath), true);
    }

    $logoPath = $existingData['companyLogo'] ?? '';
    $stampPath = $existingData['companyStamp'] ?? '';

    $uploadDir = '../../media/settings/';
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    // ✅ Upload company logo
    if (isset($_FILES['companyLogo']) && $_FILES['companyLogo']['error'] === UPLOAD_ERR_OK) {
        $fileTmpPath = $_FILES['companyLogo']['tmp_name'];
        $fileName = $_FILES['companyLogo']['name'];
        $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
        $uniqueFileName = "logo_" . date('Ymd_His') . "_" . uniqid() . "." . $fileExtension;
        $uploadFilePath = $uploadDir . $uniqueFileName;

        if (move_uploaded_file($fileTmpPath, $uploadFilePath)) {
            $logoPath = "media/settings/" . $uniqueFileName;
        } else {
            $response['status'] = 'error';
            $response['message'] = "Error uploading the logo.";
            echo json_encode($response);
            exit;
        }
    }

    // ✅ Upload company stamp
    if (isset($_FILES['companyStamp']) && $_FILES['companyStamp']['error'] === UPLOAD_ERR_OK) {
        $fileTmpPath = $_FILES['companyStamp']['tmp_name'];
        $fileName = $_FILES['companyStamp']['name'];
        $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
        $uniqueFileName = "stamp_" . date('Ymd_His') . "_" . uniqid() . "." . $fileExtension;
        $uploadFilePath = $uploadDir . $uniqueFileName;

        if (move_uploaded_file($fileTmpPath, $uploadFilePath)) {
            $stampPath = "media/settings/" . $uniqueFileName;
        } else {
            $response['status'] = 'error';
            $response['message'] = "Error uploading the stamp.";
            echo json_encode($response);
            exit;
        }
    }

    // ✅ Collect form data including GST Number
    $profileData = [
        'companyName'     => $_POST['companyName'] ?? '',
        'email'           => $_POST['email'] ?? '',
        'phone'           => $_POST['phone'] ?? '',
        'whatsapp'        => $_POST['whatsapp'] ?? '',
        'billingAddress'  => $_POST['billingAddress'] ?? '',
        'shippingAddress' => $_POST['shippingAddress'] ?? '',
        'gstNumber'       => $_POST['gstNumber'] ?? '',
        'paymentTerms'    => $_POST['paymentTerms'] ?? '',
        'termsAndPolicy'  => $_POST['termsAndPolicy'] ?? '',
        'facebook'        => $_POST['facebook'] ?? '',
        'twitter'         => $_POST['twitter'] ?? '',
        'instagram'       => $_POST['instagram'] ?? '',
        'youtube'         => $_POST['youtube'] ?? '',
        'companyLogo'     => $logoPath,
        'companyStamp'    => $stampPath
    ];

    // Save JSON file
    if (file_put_contents($jsonFilePath, json_encode($profileData, JSON_PRETTY_PRINT))) {
        $response['status'] = 'success';
        $response['message'] = 'Profile data saved successfully.';
    } else {
        $response['status'] = 'error';
        $response['message'] = 'Failed to save profile data.';
    }
} else {
    $response['status'] = 'error';
    $response['message'] = 'Invalid request.';
}

echo json_encode($response);
?>
