<?php

namespace App\Http\Controllers\Api;

use App\Models\Post;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostController extends ApiBaseController
{

    public function index()
    {
        $post = Post::with('user')->get();
        $response = [];

        foreach ($post as $key => $data) {
            $response[$key] = [
                "post_id" => $data->id,
                "user_id" => $data->user->id,
                "nama" => $data->user->nama,
                "role" => $data->user->role->role,
                "title" => $data->title,
                "content" => $data->content,
                "read"=> $data->read,
                "time" => Carbon::createFromFormat('Y-m-d H:i:s', $data->created_at)->format('Y-m-d H:i:s'),

            ];
        }

        return $this->successResponse("Data Post", $response);
    }

   public function store(Request $request)
    {
        $validator = validator($request->all(), [
            'user_id' => ['required', 'integer'],
            'title' => ['required', 'string'],
            'content' => ['string'],
        ]);
    
        if ($validator->fails()) {
            return $this->errorValidationResponse("Input Masih Tidak Sesuai", $validator->errors());
        }
    
        $user = DB::table('users')->where('id', $request->user_id)->get();
    
        if ($user->isEmpty()) {
            return $this->errorNotFound("User Tidak Ditemukan");
        } else if (!$user->where('id_role', 3)->first()) {
            return $this->errorUnauthorizedResponse("User Tidak Memiliki Hak Akses");
        }
    
        $post = new Post();
    
        $post->fill([
            'user_id' => $request->user_id,
            'title' => $request->title,
            'content' => $request->content,
            'read' => 0, // Set a default value for the 'read' column (e.g., 0 for unread)
        ]);
    
        $post->save();
    
        return $this->successResponse("Data Post Berhasil Disimpan");
    }


    public function showByOrangTua($id)
    {
        $posts = Post::with('user')->where('user_id', $id)->get();
    
        if ($posts->isEmpty()) {
            return $this->errorNotFound("Data Orang Tua tidak ditemukan");
        }
    
        $response = [];
    
        foreach ($posts as $key => $data) {
            $response[$key] = [
                "post_id" => $data->id,
                "user_id" => $data->user->id,
                "nama" => $data->user->nama,
                "role" => $data->user->role->role,
                "title" => $data->title,
                "content" => $data->content,
                 "read"=> $data->read,
                "time" => Carbon::createFromFormat('Y-m-d H:i:s', $data->created_at)->format('Y-m-d H:i:s'),
            ];
        }
    
        return $this->successResponse("Data Post", $response);
    }
    public function showByTenagaKesehatan($id)
    {
        $user = User::find($id);
    
        if (!$user || !$user->id_desa) {
            return $this->errorNotFound("Data Pengguna atau Desa tidak ditemukan");
        }
    
        $tenagaKesehatanUsers = User::where('id_desa', $user->id_desa)
            ->whereHas('role', function ($query) {
                $query->where('role', 'ORANG_TUA');
            })
            ->pluck('id');
    
        $posts = Post::with(['user.role', 'user.posyandu', 'comments.user.role'])
            ->whereIn('user_id', $tenagaKesehatanUsers)
            ->get();
    
        if ($posts->isEmpty()) {
            return $this->errorNotFound("Data Post dari Tenaga Kesehatan tidak ditemukan");
        }
    
        $response = [];
    
        foreach ($posts as $key => $data) {
            $commentsFromTenagaKesehatan = [];
    
            foreach ($data->comments as $comment) {
                if ($comment->user && $comment->user->role && $comment->user->role->role === 'TENAGA_KESEHATAN') {
                    $commentsFromTenagaKesehatan[] = [
                        'comment_id' => $comment->id,
                        'user_id' => $comment->user->id,
                        'nama' => $comment->user->nama,
                        'role' => $comment->user->role->role,
                        'content' => $comment->content,
                        'time' => Carbon::parse($comment->created_at)->format('Y-m-d H:i:s'),
                    ];
                }
            }
    
            $response[$key] = [
                "post_id" => $data->id,
                "user_id" => $data->user->id ?? null,
                "nama" => $data->user->nama ?? "-",
                "role" => $data->user->role->role ?? "-",
                "posyandu" => $data->user->posyandu->nama ?? "-",
                "title" => $data->title,
                "content" => $data->content,
                "read" => $data->read,
                "time" => Carbon::parse($data->created_at)->format('Y-m-d H:i:s'),
                "jawaban_tenaga_kesehatan" => $commentsFromTenagaKesehatan, // hanya komentar dari tenaga kesehatan
            ];
        }
    
        return $this->successResponse("Data Post Tenaga Kesehatan", $response);
    }
    public function show($id)
    {
        $post = Post::find($id);

        if (empty($post)) {
            return $this->errorNotFound("Data Post tidak ditemukan");
        }

        $user = User::find($post->user_id);

        $response = [
            "user_id" => $user->id,
            "nama" => $user->nama,
            "role" => $user->role->role,
            "title" => $post->title,
            "content" => $post->content,
            "time" => Carbon::createFromFormat('Y-m-d H:i:s', $post->created_at)->format('Y-m-d H:i:s')
        ];

        return $this->successResponse("Data Post", $response);
    }


    public function update(Request $request, $id)
    {
        $post = Post::findOrFail($id);

        if (empty($post)) {
            return $this->errorNotFound("Data Post tidak ditemukan");
        }

        $validator = validator($request->all(), [
            'user_id' => ['required', 'integer'],
            'title' => ['required', 'string'],
            'content' => ['string'],
        ]);

        if ($validator->fails()) {
            return $this->errorValidationResponse("Input Masih Tidak Sesuai", $validator->errors());
        }

        if (User::where('id', $request->user_id)->doesntExist()) {
            return $this->errorNotFound("User Tidak Ditemukan");
        }

        $post->fill([
            'user_id' => $request->user_id,
            'title' => $request->title,
            'content' => $request->content,
        ]);

        $post->save();

        return $this->successResponse("Success Update Data Post");
    }

    public function destroy($id)
    {
        $post = Post::findOrFail($id);

        if (empty($post)) {
            return $this->errorNotFound("Data Post tidak ditemukan");
        }

        $post->delete();

        return $this->successResponse("Success Delete Data Post");
    }

    public function changeobesitas(){
        $obe = DB::table('data_statistik_anak')->where('status_berat_badan', 'Obesitas')->update(array('status_berat_badan' => 'Gemuk'));

        return $this->successResponse("Success Data");
    }

    public function changestatusLK(){
        $LK1 = DB::table('data_statistik_anak')->where('status_lingkar_kepala', 'Makrosefalus')->update(array('status_lingkar_kepala' => 'Makrosefali'));
        $LK2 = DB::table('data_statistik_anak')->where('status_lingkar_kepala', 'Microcephaly')->update(array('status_lingkar_kepala' => 'Mikrosefali'));

        return $this->successResponse("Success Data");
    }
}
