<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    tools:context=".ui.chat.ChatActivity">

    <!-- Toolbar -->
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.Material3.Dark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/ic_arrow_back"
        app:title="@string/chat_title"
        app:titleTextColor="@color/white" />

    <!-- Messages RecyclerView -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/messagesRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="8dp"
        app:layout_constraintBottom_toTopOf="@id/messageInputLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        tools:listitem="@layout/item_message" />

    <!-- Attachment Indicators Container -->
    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/attachmentIndicators"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:elevation="4dp"
            app:layout_constraintBottom_toTopOf="@id/messageInputLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

        <!-- Reply Indicator -->
        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/replyIndicator"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/reply_background"
                android:padding="8dp"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/replySenderText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/text_secondary"
                android:textSize="12sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="John Smith" />

            <TextView
                android:id="@+id/replyMessageText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:maxLines="2"
                android:textColor="@color/text_secondary"
                android:textSize="12sp"
                app:layout_constraintEnd_toStartOf="@id/replyCancelButton"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/replySenderText"
                tools:text="Original message text..." />

            <ImageButton
                android:id="@+id/replyCancelButton"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:background="?android:attr/selectableItemBackgroundBorderless"
                android:contentDescription="Cancel reply"
                android:src="@android:drawable/ic_menu_close_clear_cancel"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tint="@color/text_secondary" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <!-- File Attachment Indicator -->
        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/fileAttachmentIndicator"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/attachment_background"
                android:padding="8dp"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/replyIndicator">

            <TextView
                android:id="@+id/fileAttachmentText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textColor="@color/primary"
                android:textSize="14sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toStartOf="@id/fileCancelButton"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="Отправка: document.pdf" />

            <ImageButton
                android:id="@+id/fileCancelButton"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:background="?android:attr/selectableItemBackgroundBorderless"
                android:contentDescription="Cancel file attachment"
                android:src="@android:drawable/ic_menu_close_clear_cancel"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tint="@color/primary" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- Message Input Layout -->
    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/messageInputLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/surface"
            android:elevation="4dp"
            android:minHeight="56dp"
            android:padding="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

        <!-- Message EditText -->
        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/messageEditText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:background="@drawable/message_input_background"
                android:hint="@string/send_message_hint"
                android:inputType="textMultiLine"
                android:maxLines="4"
                android:minHeight="48dp"
                android:padding="12dp"
                android:textColor="@color/text_primary"
                android:textSize="16sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/attachButton"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        <!-- Attach File Button -->
        <com.google.android.material.button.MaterialButton
                android:id="@+id/attachButton"
                android:layout_width="40dp"
                android:layout_height="48dp"
                android:layout_marginEnd="8dp"
                android:backgroundTint="@color/secondary"
                android:padding="10dp"
                app:icon="@drawable/ic_paperclip"
                app:iconSize="28sp"
                app:iconGravity="start"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/sendButton"
                app:layout_constraintTop_toTopOf="parent"/>

        <!-- Send Button -->
        <com.google.android.material.button.MaterialButton
                android:id="@+id/sendButton"
                android:layout_width="40dp"
                android:layout_height="48dp"
                android:backgroundTint="@color/primary"
                android:paddingStart="32dp"
                android:textColor="@color/white"
                app:icon="@android:drawable/ic_menu_send"
                app:iconSize="40dp"
                app:iconGravity="textStart"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- Scroll to Bottom FAB -->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/scrollToBottomFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:alpha="0.8"
        android:rotation="90"
        android:src="@android:drawable/ic_menu_send"
        android:visibility="gone"
        app:backgroundTint="@color/primary"
        app:layout_constraintBottom_toTopOf="@id/attachmentIndicators"
        app:layout_constraintEnd_toEndOf="parent"
        app:tint="@color/white" />

    <!-- Loading overlay -->
    <ProgressBar
            android:id="@+id/loadingProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
