<?php

namespace Modules\Period\Http\Controllers;

use Carbon\Carbon;
use App\Functions\ResponseHelper;
use App\Http\Controllers\BasicController;
use Modules\Period\Entities\Model;

class APIController extends BasicController
{
    public function dates()
    {
        return ResponseHelper::make(Model::select('date')->distinct()->limit(40)->where('date', '>', now()->format('Y-d-m'))->pluck('date'));
    }
    
    
    
    public function times()
    {
        $times = Model::select('from', 'to')
            ->where('date', request('date'))
            ->get()
            ->map(function ($item) {
                // 24-hour format
                $from_24 = $item->from;
                $to_24   = $item->to;
    
                // 12-hour format with localization
                $from_time = Carbon::parse($item->from);
                $to_time   = Carbon::parse($item->to);
    
                if (lang('ar')) {
                    $from_12 = str_replace(['AM','PM'], ['ص','م'], $from_time->format('h:i A'));
                    $to_12   = str_replace(['AM','PM'], ['ص','م'], $to_time->format('h:i A'));
    
                    // optional: convert digits to Arabic
                    $from_12 = num2arabic($from_12);
                    $to_12   = num2arabic($to_12);
                } else {
                    $from_12 = $from_time->format('h:i A');
                    $to_12   = $to_time->format('h:i A');
                }
    
                return [
                    'from_24' => $from_24,
                    'to_24'   => $to_24,
                    'from_12' => $from_12,
                    'to_12'   => $to_12,
                ];
            });
    
        return ResponseHelper::make($times);
    }


}
