<template>
  <div>
    <h2>PlatformServiceMixin Test</h2>
    <button @click="testInsert">Test Insert</button>
    <button @click="testUpdate">Test Update</button>
    <button
      :class="primaryButtonClasses"
      @click="testUserZeroSettings"
    >
      Test User #0 Settings
    </button>

    <div v-if="userZeroTestResult" class="mt-4 p-4 border border-gray-300 rounded-md bg-gray-50">
      <h4 class="font-semibold mb-2">User #0 Settings Test Result:</h4>
      <pre class="text-sm">{{ JSON.stringify(userZeroTestResult, null, 2) }}</pre>
    </div>
    <pre>{{ result }}</pre>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-facing-decorator";
import { PlatformServiceMixin } from "@/utils/PlatformServiceMixin";

@Component({
  mixins: [PlatformServiceMixin],
})
export default class PlatformServiceMixinTest extends Vue {
  result: string = "";
  userZeroTestResult: any = null;

  testInsert() {
    const contact = {
      name: "Alice",
      age: 30,
      isActive: true,
      tags: ["friend"],
    };
    const { sql, params } = this.$generateInsertStatement(contact, "contacts");
    this.result = `SQL: ${sql}\nParams: ${JSON.stringify(params)}`;
  }

  testUpdate() {
    const changes = { name: "Bob", isActive: false };
    const { sql, params } = this.$generateUpdateStatement(
      changes,
      "contacts",
      "id = ?",
      [42],
    );
    this.result = `SQL: ${sql}\nParams: ${JSON.stringify(params)}`;
  }

  async testUserZeroSettings() {
    try {
      // User #0's DID
      const userZeroDid = "did:ethr:0x0000694B58C2cC69658993A90D3840C560f2F51F";
      
      this.result = "Testing User #0 settings...";
      
      // Test the debug methods
      await this.$debugMergedSettings(userZeroDid);
      
      // Get the actual settings
      const didSettings = await this.$debugDidSettings(userZeroDid);
      const accountSettings = await this.$accountSettings(userZeroDid);
      
      this.userZeroTestResult = {
        didSettings,
        accountSettings,
        isRegistered: accountSettings.isRegistered,
        firstName: accountSettings.firstName,
        timestamp: new Date().toISOString(),
      };
      
      this.result = `User #0 settings test completed. isRegistered: ${accountSettings.isRegistered}`;
    } catch (error) {
      this.result = `Error testing User #0 settings: ${error}`;
      console.error("Error testing User #0 settings:", error);
    }
  }
}
</script>
