<?php
namespace App\Http\Controllers;
use App\Models\{Activity,Letter,News,Organization,PopupAnnouncement,Program};
use Illuminate\Http\Request;
class PublicSiteController extends Controller {
    public function home(){
        $mwc=Organization::where('type','mwcnu')->first();
        $units=Organization::whereIn('type',['banom','lembaga'])->where('is_active',true)->where('public_page_enabled',true)->where('show_in_main_menu',true)->orderBy('sort_order')->orderBy('name')->get();
        $rantings=Organization::where('type','ranting')->where('is_active',true)->where('public_page_enabled',true)->orderBy('name')->get();
        $aggregateOrgIds=Organization::where('is_active',true)->where('aggregate_news',true)->pluck('id');
        $news=News::with('organization')->where('status','published')->where('public_visible',true)->where(function($q)use($mwc,$aggregateOrgIds){$q->where('organization_id',$mwc?->id)->orWhere(fn($x)=>$x->whereIn('organization_id',$aggregateOrgIds)->where('aggregate_to_main',true));})->latest('published_at')->limit(8)->get();
        $eventOrgIds=Organization::where('is_active',true)->where('aggregate_events',true)->pluck('id');
        $activities=Activity::with('organization')->where('public_visible',true)->where('start_at','>=',now()->subDay())->where(function($q)use($mwc,$eventOrgIds){$q->where('organization_id',$mwc?->id)->orWhere(fn($x)=>$x->whereIn('organization_id',$eventOrgIds)->where('aggregate_to_main',true));})->orderBy('start_at')->limit(6)->get();
        $popup=PopupAnnouncement::currentlyActive()->orderBy('sort_order')->first();
        return view('public.home',compact('mwc','units','rantings','news','activities','popup'));
    }
    public function unit(string $slug){ $unit=Organization::where('slug',$slug)->whereIn('type',['banom','lembaga'])->where('is_active',true)->where('public_page_enabled',true)->firstOrFail(); return $this->unitView($unit); }
    public function ranting(string $slug){ $unit=Organization::where('slug',$slug)->where('type','ranting')->where('is_active',true)->where('public_page_enabled',true)->firstOrFail(); return $this->unitView($unit); }
    private function unitView(Organization $unit){
        $news=News::where('organization_id',$unit->id)->where('status','published')->where('public_visible',true)->latest('published_at')->limit(6)->get();
        $programs=Program::where('organization_id',$unit->id)->where('public_visible',true)->orderByDesc('progress')->limit(6)->get();
        $activities=Activity::where('organization_id',$unit->id)->where('public_visible',true)->where('start_at','>=',now()->subMonths(1))->orderByDesc('start_at')->limit(6)->get();
        $members=$unit->members()->where('is_active',true)->limit(12)->get(); return view('public.unit',compact('unit','news','programs','activities','members'));
    }
    public function news(){ $items=News::with('organization')->where('status','published')->where('public_visible',true)->latest('published_at')->paginate(16); return view('public.news',compact('items')); }
    public function article(string $slug){ $item=News::with('organization')->where('slug',$slug)->where('status','published')->where('public_visible',true)->firstOrFail(); $item->increment('views'); return view('public.article',compact('item')); }
    public function agenda(){ $items=Activity::with('organization')->where('public_visible',true)->where('start_at','>=',now()->subDay())->orderBy('start_at')->paginate(20); return view('public.agenda',compact('items')); }
    public function verify(string $token){ $letter=Letter::with('organization')->where('qr_token',$token)->firstOrFail(); return view('public.verify',compact('letter')); }
}
