package com.example.kasirapp

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.kasirapp.data.AppDatabase
import com.example.kasirapp.data.TransactionWithItems
import kotlinx.coroutines.launch
import java.text.NumberFormat
import java.text.SimpleDateFormat
import java.util.*

class TransactionDetailActivity : AppCompatActivity() {

    private val db by lazy { AppDatabase.getDatabase(this) }
    private var transactionId: Long = -1

    private lateinit var tvTransactionId: TextView
    private lateinit var tvCustomerName: TextView
    private lateinit var tvDate: TextView
    private lateinit var tvPaymentMethod: TextView
    private lateinit var tvTotal: TextView
    private lateinit var llTransactionItems: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transaction_detail)

        transactionId = intent.getLongExtra("TRANSACTION_ID", -1)
        if (transactionId == -1L) {
            finish() // Close activity if no ID is provided
            return
        }

        initializeViews()
        loadTransactionDetails()
    }

    private fun initializeViews() {
        tvTransactionId = findViewById(R.id.tv_detail_transaction_id)
        tvCustomerName = findViewById(R.id.tv_detail_customer_name)
        tvDate = findViewById(R.id.tv_detail_date)
        tvPaymentMethod = findViewById(R.id.tv_detail_payment_method)
        tvTotal = findViewById(R.id.tv_detail_total)
        llTransactionItems = findViewById(R.id.ll_transaction_items)
    }

    private fun loadTransactionDetails() {
        lifecycleScope.launch {
            // This assumes you will add a function in your DAO to get a single transaction by ID
            val transactionWithItems = db.transactionDao().getTransactionWithItemsById(transactionId)
            transactionWithItems?.let { displayTransaction(it) }
        }
    }

    private fun displayTransaction(trx: TransactionWithItems) {
        val transaction = trx.transaction
        tvTransactionId.text = "ID: ${transaction.id}"
        tvCustomerName.text = "Nama: ${transaction.customerName}"
        tvPaymentMethod.text = "Metode: ${transaction.paymentMethod}"
        tvTotal.text = "TOTAL: ${formatCurrency(transaction.total)}"

        val sdf = SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault())
        tvDate.text = "Tanggal: ${sdf.format(Date(transaction.timestamp))}"

        llTransactionItems.removeAllViews()
        trx.items.forEach { item ->
            val itemText = "- ${item.productName} x${item.quantity}  |  ${formatCurrency(item.price * item.quantity)}"
            val textView = TextView(this).apply {
                text = itemText
                textSize = 16f
                setPadding(0, 4, 0, 4)
            }
            llTransactionItems.addView(textView)
        }
    }

    private fun formatCurrency(amount: Int): String {
        return NumberFormat.getCurrencyInstance(Locale("id", "ID")).format(amount.toLong())
    }
}
