// ui/calllog/CallRecordItem.kt (新增文件)
package com.example.initiateaphonecallapp.ui.calllog

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Call
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import com.example.initiateaphonecallapp.data.model.CallRecord
import com.example.initiateaphonecallapp.enums.CallStatus

@OptIn(ExperimentalMaterial3Api::class)
// ui/calllog/CallRecordItem.kt (更新版本)
@Composable
fun CallRecordItem(
    record: CallRecord,
    onCallRecordClick: () -> Unit,
    onCallBack: () -> Unit,
    onPlayAudio: () -> Unit,
    onAddContact: () -> Unit,
    isPlaying: Boolean = false, // 新增：播放状态
    modifier: Modifier = Modifier
) {
    Card(
        onClick = onCallRecordClick,
        modifier = modifier
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            // 通话状态图标
            CallStatusIcon(
                status = record.status,
                modifier = Modifier.padding(end = 16.dp)
            )

            // 通话信息
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(
                    text = record.phoneNumber,
                    style = MaterialTheme.typography.titleMedium,
                    maxLines = 1
                )

                Row(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    modifier = Modifier.padding(top = 4.dp)
                ) {
                    Text(
                        text = getStatusDisplayText(record.status),
                        style = MaterialTheme.typography.bodySmall,
                        color = getStatusColor(record.status)
                    )

                    Text(
                        text = record.formattedDuration,
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )

                    // 录音标识
                    if (record.audioFileUri != null) {
                        Text(
                            text = "有录音",
                            style = MaterialTheme.typography.bodySmall,
                            color = MaterialTheme.colorScheme.tertiary
                        )
                    }
                }

                // 时间信息
                Text(
                    text = record.formattedStartTime,
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    modifier = Modifier.padding(top = 2.dp)
                )
            }

            // 操作按钮区域
            Column(
                horizontalAlignment = Alignment.End
            ) {
                Text(
                    text = formatTimeOnly(record.startTime),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )

                Row(
                    modifier = Modifier.padding(top = 4.dp)
                ) {
                    // 添加联系人按钮
                    IconButton(
                        onClick = onAddContact
                    ) {
                        Icon(
                            imageVector = Icons.Default.Add,
                            contentDescription = "添加联系人",
                            tint = MaterialTheme.colorScheme.primary
                        )
                    }

                    // 播放录音按钮
                    if (record.audioFileUri != null) {
                        IconButton(
                            onClick = onPlayAudio
                        ) {
                            Icon(
                                imageVector = if (isPlaying) Icons.Default.PlayArrow else Icons.Default.PlayArrow,
                                contentDescription = if (isPlaying) "暂停播放" else "播放录音",
                                tint = if (isPlaying) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary
                            )
                        }
                    }

                    // 回拨按钮
                    IconButton(
                        onClick = onCallBack
                    ) {
                        Icon(
                            imageVector = Icons.Default.Call,
                            contentDescription = "回拨",
                            tint = MaterialTheme.colorScheme.primary
                        )
                    }
                }
            }
        }
    }
}