import { SocketService } from '../services/socketService.js'
import { GeneralService } from '../share/generalservice.js'
import { Interface } from '../interfaces/Interface.js'
// import { sendError } from '../utils/response.js' // Socket ส่ง error กลับคนละแบบ แต่ import ไว้ได้

export class SocketController {

  constructor() {
    this.generalService = new GeneralService()
    this.socketService = new SocketService()
    this.Interface = new Interface()
  }

  // =========================================================
  //  FEATURE: NOTIFICATION
  // =========================================================
  async onSendNotification(io, socket, data) {
    this.generalService.devhint(1, 'socketController.js', 'onSendNotification() start')
    let idx = -1
    let database = socket.organization
    
    try {
      // Data: { targetUserId, title, message, type }
      const { targetUserId, title, message, type } = data
      
      // 1. บันทึกลง Database (ใช้ Interface Pattern ถ้ามี Table รองรับ เช่น 'notimst')
      // สมมติว่ามีตาราง notimst
      /*
      let arysave = {
        methods: 'post',
        notusrseq: targetUserId,
        nottitle: title,
        notmsg: message,
        notread: 'N',
        notdtm: this.socketService.getCurrentDTM() // function ใน service
      }
      // await this.Interface.saveInterface('notimst', arysave, { headers: { authorization: ... } }) 
      // *หมายเหตุ: Interface.js ต้องการ req.headers ซึ่ง socket ไม่มี ต้อง Mock หรือแก้ Interface
      */
     
      // หรือเรียก Service ตรงๆ เพื่อบันทึก
      await this.socketService.saveNotificationLog(database, socket.user.id, targetUserId, title, message)

      // 2. ส่ง Realtime หา Target
      io.to(targetUserId.toString()).emit('receive_notification', {
        from: socket.user.usrnam,
        title,
        message,
        type,
        timestamp: new Date()
      })

      this.generalService.devhint(2, 'socketController.js', `Sent notify to ${targetUserId}`)

    } catch (error) {
      idx = 1
      console.error(error)
    } finally {
      if (idx === 1) {
        socket.emit('error', { message: 'Failed to send notification' })
      }
    }
  }

  // =========================================================
  // FEATURE: VOIP (WebRTC Signaling)
  // =========================================================
  
  // A โทรหา B
  async onCallUser(io, socket, data) {
    this.generalService.devhint(1, 'socketController.js', 'onCallUser() start')
    let idx = -1
    try {
      const { userToCall, signalData } = data
      
      // ส่ง Event 'call_incoming' ไปหาห้องของ userToCall
      io.to(userToCall.toString()).emit('call_incoming', {
        signal: signalData,
        from: socket.user.id,
        fromName: socket.user.usrnam
      })
      
    } catch (error) {
      idx = 1
    } finally {
      if (idx === 1) socket.emit('error', { message: 'Call failed' })
    }
  }

  // B รับสาย A
  async onAnswerCall(io, socket, data) {
    this.generalService.devhint(1, 'socketController.js', 'onAnswerCall() start')
    try {
      const { to, signal } = data
      io.to(to.toString()).emit('call_accepted', { signal })
    } catch (error) {
        console.error('VoIP Error:', error)
    }
  }

  // แลกเปลี่ยน Network Info (ICE Candidate)
  async onIceCandidate(io, socket, data) {
    try {
      const { targetUserId, candidate } = data
      io.to(targetUserId.toString()).emit('receive_ice_candidate', { candidate })
    } catch (error) {
        // silent fail for ICE
    }
  }

  // วางสาย
  async onEndCall(io, socket, data) {
      const { targetUserId } = data
      if(targetUserId) {
          io.to(targetUserId.toString()).emit('call_ended', { from: socket.user.id })
      }
  }
}