﻿@extends('layouts.dashboard')

@section('content')
    <div class="md:max-w-[80%] max-w-[90%] mx-auto min-h-screen bg-[#F9FAFB]" x-data="{ showCreatePostModal: false }">

        {{-- 1. Community Hero Header --}}
        <div class="bg-white border-b border-gray-200">
            {{-- Cover Gradient --}}
            <div class="h-32 w-full bg-gradient-to-r from-[#2F3D7E] to-[#4a5bb5]"></div>

            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="relative -mt-12 pb-6">
                    <div class="flex flex-col md:flex-row md:items-end md:space-x-5">
                        {{-- Avatar --}}
                        <div class="flex">
                            <div
                                class="h-24 w-24 rounded-xl ring-4 ring-white bg-white flex items-center justify-center shadow-sm overflow-hidden">
                                @if ($community->logo_path)
                                    <img src="{{ asset('storage/' . $community->logo_path) }}" alt="{{ $community->name }}"
                                        class="h-full w-full object-cover">
                                @else
                                    <span
                                        class="text-3xl font-bold text-[#2F3D7E]">{{ substr($community->name, 0, 1) }}</span>
                                @endif
                            </div>
                        </div>

                        {{-- Info --}}
                        <div class="mt-4 md:mt-0 flex-1 min-w-0">
                            <h1 class="text-2xl font-bold text-gray-900 truncate">{{ $community->name }}</h1>
                            <div class="flex flex-wrap items-center gap-4 text-sm text-gray-500 mt-1">
                                <span class="flex items-center gap-1">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0z" />
                                    </svg>
                                    {{ number_format($community->members_count ?? 0) }} Members
                                </span>
                                @if ($community->category)
                                    <span class="flex items-center gap-1">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" />
                                        </svg>
                                        {{ $community->category }}
                                    </span>
                                @endif
                            </div>
                        </div>

                        {{-- Actions --}}
                        <div class="mt-4 md:mt-0 flex flex-shrink-0 gap-3"> 

                           {{-- 1. Share Button (Only visible if posts exist) --}}
                            @if($posts->count() > 0)
                                <button 
                                    @click="showCreatePostModal = true"
                                    class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-[#2F3D7E] hover:bg-[#232d5e] focus:outline-none transition-colors">
                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
                                    Share Post
                                </button>
                            @endif

                            {{-- 2. Join/Joined Button --}}
                            <div x-data="{ isJoined: {{ $isMember ? 'true' : 'false' }}, isLoading: false }">
                                <template x-if="isJoined">
                                    <button
                                        class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white cursor-default">
                                        <svg class="w-4 h-4 mr-2 text-green-500" fill="none" stroke="currentColor"
                                            viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                d="M5 13l4 4L19 7" />
                                        </svg>
                                        Joined
                                    </button>
                                </template>

                                <template x-if="!isJoined">
                                    <button
                                        @click="isLoading = true; fetch('{{ route('communities.join', $community->id) }}', {method: 'POST', headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}', 'Accept': 'application/json'}}).then(r => { isJoined = true; isLoading = false; })"
                                        :disabled="isLoading"
                                        {{-- If posts exist (Share button is visible), make this button white/secondary to avoid double primary buttons --}}
                                        class="inline-flex items-center px-6 py-2 border shadow-sm text-sm font-medium rounded-md focus:outline-none transition-colors 
                                        {{ $posts->count() > 0
    ? 'border-gray-300 text-gray-700 bg-white hover:bg-gray-50'
    : 'border-transparent text-white bg-[#2F3D7E] hover:bg-[#232d5e]' 
                                        }}">
                                        <span x-show="!isLoading">Join Community</span>
                                        <span x-show="isLoading">Joining...</span>
                                    </button>
                                </template>
                            </div> 

                        </div>
                    </div>
                </div>
            </div>
        </div>

        {{-- 2. Main Content Grid --}}
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">

                {{-- LEFT COLUMN: POST FEED --}}
                <div class="lg:col-span-2 space-y-6">

                    {{-- Search Bar --}}
                    {{-- FIXED: Changed $community->slug to $community->id to match your route definition --}}
                    <form method="GET" action="{{ route('communities.show', $community->id) }}"
                        class="flex items-center gap-4 mb-6">
                        <div class="relative flex-1">
                            <input type="text" name="search" value="{{ request('search') }}"
                                placeholder="Search posts in {{ $community->name }}..."
                                class="w-full pl-10 pr-4 py-2.5 bg-white border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-[#2F3D7E]/20 focus:border-[#2F3D7E] transition-all">
                            <svg class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" fill="none"
                                stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                            </svg>
                        </div>
                        <button type="submit"
                            class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-sm font-medium">Search</button>
                    </form>

                    {{-- POSTS LIST --}}
                    @forelse($posts as $post)
                        <x-network.post-card :post="$post" />
                    @empty
                        <div class="bg-white rounded-xl border border-gray-200 p-12 text-center">
                            <div class="w-16 h-16 bg-gray-50 rounded-xl flex items-center justify-center mx-auto mb-4">
                                <svg class="w-8 h-8 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
                                </svg>
                            </div>
                            <h3 class="text-lg font-medium text-gray-900">
                                {{ request('search') ? 'No posts found matching "' . request('search') . '"' : 'No posts yet' }}
                            </h3>
                            <p class="text-gray-500 mt-1">
                                {{ request('search') ? 'Try searching for something else.' : 'Be the first to share your salary journey in this community.' }}
                            </p>
                            @if(!request('search'))
                                <br>
                                <button @click="showCreatePostModal = true"
                                    class="px-6 py-2.5 text-sm font-medium text-white bg-[#2F3D7E] rounded-lg hover:bg-[#232d5e] focus:outline-none shadow-sm disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
                                    Share Your Post
                                </button>
                            @else
                                <br>
                                {{-- Clear Search Button (Uses ID) --}}
                                <a href="{{ route('communities.show', $community->id) }}" class="px-6 py-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200 font-medium">
                                    Clear Search
                                </a>
                            @endif
                        </div>
                    @endforelse

                    {{-- Pagination --}}
                    <div class="mt-6">
                        {{ $posts->links() }}
                    </div>
                </div>

                {{-- RIGHT COLUMN: SIDEBAR --}}
                <div class="space-y-6">
                    {{-- About Card --}}
                    <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                        <h3 class="font-bold text-gray-900 mb-4">About Community</h3>
                        <p class="text-sm text-gray-600 leading-relaxed mb-6">
                            {{ $community->description ?? 'No description provided.' }}
                        </p>

                        <div class="border-t border-gray-100 pt-4 space-y-3">
                            <div class="flex items-center justify-between text-sm">
                                <span class="text-gray-500">Created</span>
                                <span class="font-medium text-gray-900">{{ $community->created_at->format('M Y') }}</span>
                            </div>
                            <div class="flex items-center justify-between text-sm">
                                <span class="text-gray-500">Privacy</span>
                                <span
                                    class="font-medium text-gray-900 capitalize">{{ $community->type ?? 'Public' }}</span>
                            </div>
                        </div>
                    </div>

                    {{-- Members Preview --}}
                    <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                        <div class="flex items-center justify-between mb-4">
                            <h3 class="font-bold text-gray-900">Members</h3>
                        </div>

                        <div class="flex flex-wrap gap-2">
                            @foreach ($members->take(10) as $member)
                                <div class="group relative" title="{{ $member->name }}">
                                    <img src="https://ui-avatars.com/api/?name={{ urlencode($member->name) }}&background=random&color=fff"
                                        alt="{{ $member->name }}"
                                        class="w-8 h-8 rounded-full ring-2 ring-white group-hover:ring-[#2F3D7E] transition-all">
                                </div>
                            @endforeach
                        </div>
                    </div>
                </div>

            </div>
        </div>
        {{-- INCLUDE MODAL --}}
        <x-network.create-post-modal 
            trigger="showCreatePostModal"
            :experiences="$experiences" 
            :communities="$joinedCommunities"
            :currentCommunityId="$community->id" 
        />
    </div>
@endsect