// src/stores/userSyncStore.js
import { defineStore } from 'pinia'

export const useUserSyncStore = defineStore('userSync', {
  state: () => ({
    pgUsers: [], // Postgre kullanıcıları
    msUsers: [], // MSSQL kullanıcıları
    loading: false,
    selectedPg: null
  }),

  actions: {
    // 🧩 Dummy veri ile başlat
    async loadDummy() {
      this.loading = true
      await new Promise(r => setTimeout(r, 600)) // küçük gecikme
      this.pgUsers = [
        { id: 1, code: 'mehmetk', full_name: 'Mehmet Keçeci', email: 'm@b.com', mssql_username: 'MKECECI', sync_status: 'synced', is_active: true },
        { id: 2, code: 'ayse', full_name: 'Ayşe Yılmaz', email: 'a@y.com', mssql_username: null, sync_status: 'pending', is_active: true },
        { id: 3, code: 'ali', full_name: 'Ali Demir', email: 'a@d.com', mssql_username: 'ALI.D', sync_status: 'blocked', is_active: false }
      ]
      this.msUsers = [
        { username: 'MKECECI', first_name: 'Mehmet', last_name: 'Keçeci', email: 'm@b.com', is_blocked: 0 },
        { username: 'ALI.D', first_name: 'Ali', last_name: 'Demir', email: 'a@d.com', is_blocked: 1 },
        { username: 'YENIUSR', first_name: 'Yeni', last_name: 'Kullanıcı', email: 'y@b.com', is_blocked: 0 }
      ]
      this.loading = false
    },

    async syncNow() {
      this.loading = true
      console.log('🔄 Dummy sync tetiklendi...')
      await new Promise(r => setTimeout(r, 800))
      this.loading = false
    },

    async map(pgId, msUsername) {
      const user = this.pgUsers.find(u => u.id === pgId)
      if (user) {
        user.mssql_username = msUsername
        user.sync_status = 'manual'
      }
    },

    async unmap(pgId) {
      const user = this.pgUsers.find(u => u.id === pgId)
      if (user) {
        user.mssql_username = null
        user.sync_status = 'pending'
      }
    }
  }
})
