<?php

namespace App\Http\Controllers;

use App\Http\Resources\ActivityReportResource;
use App\Models\activity_report;
use App\Models\periodic_activity;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ActivityReportController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $data= [];
        if ($request->type =="today") {
            $data= ActivityReportResource::collection(activity_report::whereDate("created_at",Carbon::today())->get());
        }
        return response()->json(// todo : see macro vidoes
            [
                "data" => $data,
                "message" => "success",
                "status" => true,
            ]
        );
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    // public function store(Request $request, $id)
    // {
    //     //  return $request->users;
    //       $report_number = strtotime(date('YmdHis'));
    //     $response =[];
    //     foreach ($request->users as $key => $value) {
    //         $activity = new activity_report();
    //         $activity->activity_id = $id;
    //         $activity->user_id = $value;
    //         $activity->date = $request->date;
    //         $activity->report_number = $report_number;
    //         $activity->save();
    //         $response[]=$activity;
    //     }
    //     return response()->json(// todo : see macro vidoes
    //         [
    //             "data" => $response,
    //             "message" => "success",
    //             "status" => true,
    //         ]
    //     );
    // }
public function store(Request $request, $id)
    {
        // return $request;
        $request->validate([
            'users' => 'required',
            'users.*' => 'required',
            'date' => 'required|date',
        ], [
            'users.required' => 'Please select at least one user.',
            // 'users.array' => 'The users format must be an array.',
            // 'users.*.required' => 'Each user must have an ID.',
            // 'users.*.exists' => 'The user ID is not valid.',
            'date.required' => 'Please enter a date.',
            'date.date' => 'The entered date must be a valid date.',
        ]);

        $report_number = strtotime(date('YmdHis'));
        $response = [];

        foreach ($request->users as $key => $value) {
            $activity = new activity_report();
            $activity->activity_id = $id;
            $activity->user_id = auth()->id();
            $activity->date = $request->date;
            $activity->report_number = $report_number;
            $activity->save();
            $response[] = $activity;
        }

        $periodicActivity = periodic_activity::find($id); // Assuming the id corresponds to periodic_activity
        if ($periodicActivity) {
            $periodicActivity->last_date = $request->date; // Update last_date with the new date
            $periodicActivity->save();
        }

        return response()->json([
            "data" => $response,
            "message" => "success",
            "status" => true,
        ]);
    }
    /**
     * Display the specified resource.
     */
    public function show(activity_report $activity_report)
    {
        return ActivityReportResource::collection(activity_report::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(activity_report $activity_report)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, activity_report $activity_report)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(activity_report $activity_report)
    {
        //
    }
}
