import SwiftUI

struct SearchBar: View {
    @Binding var text: String
    let placeholder: String

    init(text: Binding<String>, placeholder: String = "Search for a Workspace") {
        self._text = text
        self.placeholder = placeholder
    }

    var body: some View {
        HStack(spacing: 8) {
            Image(systemName: "magnifyingglass")
                .resizable()
                .frame(width: 16, height: 16)
                .foregroundColor(Constants.Colors.Background.Fog.dense)

            ZStack(alignment: .leading) {
                if text.isEmpty {
                    Text(placeholder)
                        .font(.custom("PP Neue Montreal", size: 14))
                        .fontWeight(.medium)
                        .foregroundColor(Constants.Colors.Background.Fog.dense)
                        .tracking(0.28)
                }

                TextField("", text: $text)
                    .font(.custom("PP Neue Montreal", size: 14))
                    .fontWeight(.medium)
                    .foregroundColor(Constants.Colors.Foreground.primary)
                    .tracking(0.28)
            }
        }
        .padding(.horizontal, 12)
        .padding(.vertical, 10)
        .background(Constants.Colors.Background.Fog.thin)
        .overlay(
            RoundedRectangle(cornerRadius: 100)
                .stroke(Constants.Colors.Background.secondary, lineWidth: 1)
        )
        .cornerRadius(100)
    }
}

#Preview {
    VStack(spacing: 20) {
        SearchBar(text: .constant(""), placeholder: "Search for a Workspace")
        SearchBar(text: .constant("My Workspace"), placeholder: "Search for a Workspace")
    }
    .padding()
    .background(Constants.Colors.Background.primary)
}
