<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\V1;

use App\Enums\ExportFormat;
use App\Enums\Role;
use App\Enums\TimeEntryType;
use App\Exceptions\Api\FeatureIsNotAvailableInFreePlanApiException;
use App\Exceptions\Api\OverlappingTimeEntryApiException;
use App\Exceptions\Api\PdfRendererIsNotConfiguredException;
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
use App\Exceptions\Api\TimeEntryStillRunningApiException;
use App\Http\Requests\V1\TimeEntry\TimeEntryAggregateExportRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryAggregateRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryDestroyMultipleRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryIndexExportRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryIndexRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryStoreRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryUpdateMultipleRequest;
use App\Http\Requests\V1\TimeEntry\TimeEntryUpdateRequest;
use App\Http\Resources\V1\TimeEntry\TimeEntryCollection;
use App\Http\Resources\V1\TimeEntry\TimeEntryResource;
use App\Jobs\RecalculateSpentTimeForProject;
use App\Jobs\RecalculateSpentTimeForTask;
use App\Models\Member;
use App\Models\Organization;
use App\Models\Project;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Service\LocalizationService;
use App\Service\ReportExport\TimeEntriesDetailedCsvExport;
use App\Service\ReportExport\TimeEntriesDetailedExport;
use App\Service\ReportExport\TimeEntriesReportExport;
use App\Service\TimeEntryAggregationService;
use App\Service\TimeEntryFilter;
use App\Service\TimeEntryService;
use App\Service\TimezoneService;
use Gotenberg\Exceptions\GotenbergApiErrored;
use Gotenberg\Exceptions\NoOutputFileInResponse;
use Gotenberg\Gotenberg;
use Gotenberg\Stream;
use GuzzleHttp\Client;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\File;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class TimeEntryController extends Controller
{
    private function assertNoOverlap(Organization $organization, Member $member, Carbon $start, ?Carbon $end, ?TimeEntry $exclude = null): void
    {
        if (! $organization->prevent_overlapping_time_entries) {
            return;
        }

        $query = TimeEntry::query()
            ->where('organization_id', $organization->getKey())
            ->where('member_id', $member->getKey())
            ->when($exclude !== null, function (Builder $q) use ($exclude): void {
                $q->where('id', '!=', $exclude->getKey());
            })
            ->where(function (Builder $q) use ($start, $end): void {
                $q->where(function (Builder $q2) use ($start): void {
                    $q2->where('end', '>', $start)
                        ->where('start', '<', $start);
                });

                if ($end !== null) {
                    $q->orWhere(function (Builder $q4) use ($end): void {
                        $q4->where('start', '<', $end)
                            ->where('end', '>', $end);
                    });
                    // Check if the new entry completely surrounds an existing entry
                    $q->orWhere(function (Builder $q6) use ($start, $end): void {
                        $q6->where('start', '>=', $start)
                            ->where('end', '<=', $end);
                    });
                }

            });

        if ($query->exists()) {
            throw new OverlappingTimeEntryApiException;
        }
    }

    protected function checkPermission(Organization $organization, string $permission, ?TimeEntry $timeEntry = null): void
    {
        parent::checkPermission($organization, $permission);
        if ($timeEntry !== null && $timeEntry->organization_id !== $organization->getKey()) {
            throw new AuthorizationException('Time entry does not belong to organization');
        }
    }

    /**
     * Get time entries in organization
     *
     * If you only need time entries for a specific user, you can filter by `member_id`.
     * Users with the permission `time-entries:view:own` can only use this endpoint with their own member ID in the member_id filter.
     *
     * @return TimeEntryCollection<TimeEntryResource>
     *
     * @throws AuthorizationException
     *
     * @operationId getTimeEntries
     */
    public function index(Organization $organization, TimeEntryIndexRequest $request): JsonResource
    {
        $member = $this->member($organization);
        /** @var Member|null $memberFilter */
        $memberFilter = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : null;
        if ($memberFilter !== null && $memberFilter->getKey() === $member->getKey()) {
            $this->checkPermission($organization, 'time-entries:view:own');
        } else {
            $this->checkPermission($organization, 'time-entries:view:all');
        }

        $canAccessPremiumFeatures = $this->canAccessPremiumFeatures($organization);
        $timeEntriesQuery = $this->getTimeEntriesQuery($organization, $request, $memberFilter, $canAccessPremiumFeatures);

        $totalCount = $timeEntriesQuery->count();

        $limit = $request->getLimit();
        if ($limit > 1000) {
            $limit = 1000;
        }
        $timeEntriesQuery->limit($limit);
        $timeEntriesQuery->skip($request->getOffset());

        $timeEntries = $timeEntriesQuery->get();

        if ($timeEntries->count() === $limit && $request->getOnlyFullDates()) {
            $user = $this->user();
            $timezone = app(TimezoneService::class)->getTimezoneFromUser($user);
            $lastDate = null;
            /** @var TimeEntry $timeEntry */
            foreach ($timeEntries as $timeEntry) {
                if ($lastDate === null || abs($lastDate->diffInDays($timeEntry->start->toImmutable()->timezone($timezone)->startOfDay())) > 0) {
                    $lastDate = $timeEntry->start->toImmutable()->timezone($timezone)->startOfDay();
                }
            }

            $timeEntries = $timeEntries->filter(function (TimeEntry $timeEntry) use ($lastDate, $timezone): bool {
                return $timeEntry->start->toImmutable()->timezone($timezone)->toDateString() !== $lastDate->toDateString();
            });

            if ($timeEntries->count() === 0) {
                Log::warning('User has has more than '.$limit.' time entries on one date', [
                    'date' => $lastDate->toDateString(),
                    'member_id' => $request->input('member_id'),
                    'auth_user_id' => Auth::id(),
                    'limit' => $limit,
                ]);
                $timeEntries = $timeEntriesQuery
                    ->limit(5000)
                    ->where('start', '>=', $lastDate->copy()->startOfDay()->utc())
                    ->where('start', '<=', $lastDate->copy()->endOfDay()->utc())
                    ->get();
            }
        }

        return (new TimeEntryCollection($timeEntries))
            ->additional([
                'meta' => [
                    'total' => $totalCount,
                ],
            ]);
    }

    /**
     * @return Builder<TimeEntry>
     */
    private function getTimeEntriesQuery(Organization $organization, TimeEntryIndexRequest|TimeEntryIndexExportRequest $request, ?Member $member, bool $canAccessPremiumFeatures): Builder
    {
        $select = TimeEntry::SELECT_COLUMNS;
        $roundingType = $canAccessPremiumFeatures ? $request->getRoundingType() : null;
        $roundingMinutes = $canAccessPremiumFeatures ? $request->getRoundingMinutes() : null;
        if ($roundingType !== null && $roundingMinutes !== null) {
            $select = array_diff($select, ['start', 'end']);
            $select[] = DB::raw(app(TimeEntryService::class)->getStartSelectRawForRounding($roundingType, $roundingMinutes).' as start');
            $select[] = DB::raw(app(TimeEntryService::class)->getEndSelectRawForRounding($roundingType, $roundingMinutes).' as end');
        }
        $timeEntriesQuery = TimeEntry::query()
            ->whereBelongsTo($organization, 'organization')
            ->select($select)
            ->orderBy('time_entries.start', 'desc')
            ->orderBy('time_entries.id');

        $filter = new TimeEntryFilter($timeEntriesQuery);
        $filter->addStartFilter($request->input('start'));
        $filter->addEndFilter($request->input('end'));
        $filter->addActiveFilter($request->input('active'));
        $filter->addMemberIdFilter($member);
        $filter->addMemberIdsFilter($request->input('member_ids'));
        $filter->addProjectIdsFilter($request->input('project_ids'));
        $filter->addTagIdsFilter($request->input('tag_ids'), $request->getTagMatchType());
        $filter->addTaskIdsFilter($request->input('task_ids'));
        $filter->addClientIdsFilter($request->input('client_ids'));
        $filter->addBillableFilter($request->input('billable'));
        $filter->addTypeFilter($request->input('type'));

        return $filter->get();
    }

    /**
     * Export time entries in organization
     *
     * @throws AuthorizationException|PdfRendererIsNotConfiguredException|FeatureIsNotAvailableInFreePlanApiException
     *
     * @operationId exportTimeEntries
     */
    public function indexExport(Organization $organization, TimeEntryIndexExportRequest $request, TimeEntryAggregationService $timeEntryAggregationService): JsonResponse
    {
        $member = $this->member($organization);
        /** @var Member|null $memberFilter */
        $memberFilter = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : null;
        if ($memberFilter !== null && $memberFilter->getKey() === $member->getKey()) {
            $this->checkPermission($organization, 'time-entries:view:own');
        } else {
            $this->checkPermission($organization, 'time-entries:view:all');
        }
        $canAccessPremiumFeatures = $this->canAccessPremiumFeatures($organization);
        $debug = $request->getDebug();
        $format = $request->getFormatValue();
        if ($format === ExportFormat::PDF && ! $canAccessPremiumFeatures) {
            throw new FeatureIsNotAvailableInFreePlanApiException;
        }
        $user = $this->user();
        $timezone = $user->timezone;
        $showBillableRate = $this->member($organization)->role !== Role::Employee->value || $organization->employees_can_see_billable_rates;
        $roundingType = $canAccessPremiumFeatures ? $request->getRoundingType() : null;
        $roundingMinutes = $canAccessPremiumFeatures ? $request->getRoundingMinutes() : null;

        $timeEntriesQuery = $this->getTimeEntriesQuery($organization, $request, $memberFilter, $canAccessPremiumFeatures);
        $timeEntriesQuery->with([
            'task',
            'client',
            'project',
            'user',
            'tagsRelation',
        ]);
        $filename = 'time-entries-export-'.now()->format('Y-m-d_H-i-s').'-'.Str::uuid().'.'.$format->getFileExtension();
        $folderPath = 'exports';
        $path = $folderPath.'/'.$filename;
        $localizationService = LocalizationService::forOrganization($organization);
        if ($format === ExportFormat::CSV) {
            $export = new TimeEntriesDetailedCsvExport(config('filesystems.private'), $folderPath, $filename, $timeEntriesQuery, 1000, $timezone);
            $export->export();
        } elseif ($format === ExportFormat::PDF) {
            if (config('services.gotenberg.url') === null && ! $debug) {
                throw new PdfRendererIsNotConfiguredException;
            }
            $viewFile = file_get_contents(resource_path('views/reports/time-entry-index/pdf.blade.php'));
            if ($viewFile === false) {
                throw new \LogicException('View file not found');
            }
            $timeEntriesAggregateQuery = $this->getTimeEntriesAggregateQuery($organization, $request, $memberFilter);
            $aggregatedData = $timeEntryAggregationService->getAggregatedTimeEntries(
                $timeEntriesAggregateQuery,
                null,
                null,
                $user->timezone,
                $user->week_start,
                false,
                null,
                null,
                $showBillableRate,
                $roundingType,
                $roundingMinutes,
            );
            $html = Blade::render($viewFile, [
                'timeEntries' => $timeEntriesQuery->get(),
                'aggregatedData' => $aggregatedData,
                'timezone' => $timezone,
                'currency' => $organization->currency,
                'start' => $request->getStart()->timezone($timezone),
                'end' => $request->getEnd()->timezone($timezone),
                'localization' => $localizationService,
                'showBillableRate' => $showBillableRate,
            ]);
            $footerViewFile = file_get_contents(resource_path('views/reports/time-entry-index/pdf-footer.blade.php'));
            if ($footerViewFile === false) {
                throw new \LogicException('View file not found');
            }
            $footerHtml = Blade::render($footerViewFile);
            if ($debug) {
                return response()->json([
                    'html' => $html,
                    'footer_html' => $footerHtml,
                ]);
            }

            $client = new Client([
                'auth' => config('services.gotenberg.basic_auth_username') !== null && config('services.gotenberg.basic_auth_password') !== null ? [
                    config('services.gotenberg.basic_auth_username'),
                    config('services.gotenberg.basic_auth_password'),
                ] : null,
            ]);
            $request = Gotenberg::chromium(config('services.gotenberg.url'))
                ->pdf()
                ->assets(
                    Stream::path(resource_path('pdf/Outfit-VariableFont_wght.ttf'), 'outfit.ttf'),
                )
                ->margins(0.39, 0.78, 0.39, 0.39)
                ->paperSize('8.27', '11.7') // A4
                ->footer(Stream::string('footer', $footerHtml))
                ->html(Stream::string('body', $html));
            $tempFolder = TemporaryDirectory::make();
            $filenameTemp = Gotenberg::save($request, $tempFolder->path(), $client);
            Storage::disk(config('filesystems.private'))
                ->putFileAs($folderPath, new File($tempFolder->path($filenameTemp)), $filename);
        } else {
            Excel::store(
                new TimeEntriesDetailedExport($timeEntriesQuery, $format, $timezone, $localizationService),
                $path,
                config('filesystems.private'),
                $format->getExportPackageType(),
                [
                    'visibility' => 'private',
                ]
            );
        }

        return response()->json([
            'download_url' => Storage::disk(config('filesystems.private'))
                ->temporaryUrl($path, now()->addMinutes(5)),
        ]);
    }

    /**
     * Get aggregated time entries in organization
     *
     * This endpoint allows you to filter time entries and aggregate them by different criteria.
     * The parameters `group` and `sub_group` allow you to group the time entries by different criteria.
     * If the group parameters are all set to `null` or are all missing, the endpoint will aggregate all filtered time entries.
     *
     * @operationId getAggregatedTimeEntries
     *
     * @return array{
     *     data: array{
     *          grouped_type: string|null,
     *          grouped_data: null|array<array{
     *              key: string|null,
     *              seconds: int,
     *              cost: int|null,
     *              grouped_type: string|null,
     *              grouped_data: null|array<array{
     *                  key: string|null,
     *                  seconds: int,
     *                  cost: int|null,
     *                  grouped_type: null,
     *                  grouped_data: null
     *              }>
     *          }>,
     *          seconds: int,
     *          cost: int|null
     *      }
     * }
     *
     * @throws AuthorizationException
     */
    public function aggregate(Organization $organization, TimeEntryAggregateRequest $request, TimeEntryAggregationService $timeEntryAggregationService): array
    {
        $member = $this->member($organization);
        /** @var Member|null $memberFilter */
        $memberFilter = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : null;
        if ($memberFilter !== null && $memberFilter->getKey() === $member->getKey()) {
            $this->checkPermission($organization, 'time-entries:view:own');
        } else {
            $this->checkPermission($organization, 'time-entries:view:all');
        }
        $canAccessPremiumFeatures = $this->canAccessPremiumFeatures($organization);
        $user = $this->user();
        $showBillableRate = $this->member($organization)->role !== Role::Employee->value || $organization->employees_can_see_billable_rates;

        $group1Type = $request->getGroup();
        $group2Type = $request->getSubGroup();
        $timeEntriesAggregateQuery = $this->getTimeEntriesAggregateQuery($organization, $request, $memberFilter);
        $roundingType = $canAccessPremiumFeatures ? $request->getRoundingType() : null;
        $roundingMinutes = $canAccessPremiumFeatures ? $request->getRoundingMinutes() : null;

        $aggregatedData = $timeEntryAggregationService->getAggregatedTimeEntries(
            $timeEntriesAggregateQuery,
            $group1Type,
            $group2Type,
            $user->timezone,
            $user->week_start,
            $request->getFillGapsInTimeGroups(),
            $request->getStart(),
            $request->getEnd(),
            $showBillableRate,
            $roundingType,
            $roundingMinutes
        );

        return [
            'data' => $aggregatedData,
        ];
    }

    /**
     * Export aggregated time entries in organization
     *
     * @operationId exportAggregatedTimeEntries
     *
     * @throws AuthorizationException
     * @throws PdfRendererIsNotConfiguredException
     * @throws GotenbergApiErrored
     * @throws NoOutputFileInResponse
     * @throws FeatureIsNotAvailableInFreePlanApiException
     */
    public function aggregateExport(Organization $organization, TimeEntryAggregateExportRequest $request, TimeEntryAggregationService $timeEntryAggregationService): JsonResponse
    {
        $member = $this->member($organization);
        /** @var Member|null $memberFilter */
        $memberFilter = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : null;
        if ($memberFilter !== null && $memberFilter->getKey() === $member->getKey()) {
            $this->checkPermission($organization, 'time-entries:view:own');
        } else {
            $this->checkPermission($organization, 'time-entries:view:all');
        }
        $canAccessPremiumFeatures = $this->canAccessPremiumFeatures($organization);
        $format = $request->getFormatValue();
        if ($format === ExportFormat::PDF && ! $this->canAccessPremiumFeatures($organization)) {
            throw new FeatureIsNotAvailableInFreePlanApiException;
        }
        $debug = $request->getDebug();
        $user = $this->user();
        $showBillableRate = $this->member($organization)->role !== Role::Employee->value || $organization->employees_can_see_billable_rates;

        $group = $request->getGroup();
        $subGroup = $request->getSubGroup();
        $timeEntriesAggregateQuery = $this->getTimeEntriesAggregateQuery($organization, $request, $memberFilter);
        $roundingType = $canAccessPremiumFeatures ? $request->getRoundingType() : null;
        $roundingMinutes = $canAccessPremiumFeatures ? $request->getRoundingMinutes() : null;

        $aggregatedData = $timeEntryAggregationService->getAggregatedTimeEntriesWithDescriptions(
            $timeEntriesAggregateQuery->clone(),
            $group,
            $subGroup,
            $user->timezone,
            $user->week_start,
            false,
            $request->getStart(),
            $request->getEnd(),
            $showBillableRate,
            $roundingType,
            $roundingMinutes
        );
        $dataHistoryChart = $timeEntryAggregationService->getAggregatedTimeEntries(
            $timeEntriesAggregateQuery->clone(),
            $request->getHistoryGroup(),
            null,
            $user->timezone,
            $user->week_start,
            true,
            $request->getStart(),
            $request->getEnd(),
            $showBillableRate,
            $roundingType,
            $roundingMinutes
        );
        $currency = $organization->currency;
        $timezone = app(TimezoneService::class)->getTimezoneFromUser($this->user());
        $localizationService = LocalizationService::forOrganization($organization);

        $filename = 'time-entries-report-'.now()->format('Y-m-d_H-i-s').'-'.Str::uuid().'.'.$format->getFileExtension();
        $folderPath = 'exports';
        $path = $folderPath.'/'.$filename;

        if ($format === ExportFormat::PDF) {
            if (config('services.gotenberg.url') === null && ! $debug) {
                throw new PdfRendererIsNotConfiguredException;
            }
            $client = new Client([
                'auth' => config('services.gotenberg.basic_auth_username') !== null && config('services.gotenberg.basic_auth_password') !== null ? [
                    config('services.gotenberg.basic_auth_username'),
                    config('services.gotenberg.basic_auth_password'),
                ] : null,
            ]);
            $viewFile = file_get_contents(resource_path('views/reports/time-entry-aggregate/pdf.blade.php'));
            if ($viewFile === false) {
                throw new \LogicException('View file not found');
            }
            $html = Blade::render($viewFile, [
                'aggregatedData' => $aggregatedData,
                'dataHistoryChart' => $dataHistoryChart,
                'currency' => $currency,
                'group' => $group,
                'subGroup' => $subGroup,
                'timezone' => $timezone,
                'start' => $request->getStart()->timezone($timezone),
                'end' => $request->getEnd()->timezone($timezone),
                'debug' => $debug,
                'localization' => $localizationService,
                'showBillableRate' => $showBillableRate,
            ]);
            $footerViewFile = file_get_contents(resource_path('views/reports/time-entry-aggregate/pdf-footer.blade.php'));
            if ($footerViewFile === false) {
                throw new \LogicException('View file not found');
            }
            $footerHtml = Blade::render($footerViewFile);
            if ($debug) {
                return response()->json([
                    'html' => $html,
                    'footer_html' => $footerHtml,
                ]);
            }
            $request = Gotenberg::chromium(config('services.gotenberg.url'))
                ->pdf()
                ->waitForExpression("window.status === 'ready'")
                ->margins(0.39, 0.78, 0.39, 0.39)
                ->paperSize('8.27', '11.7') // A4
                ->footer(Stream::string('footer', $footerHtml))
                ->assets(Stream::path(resource_path('pdf/echarts.min.js'), 'echarts.min.js'),
                    Stream::path(resource_path('pdf/Outfit-VariableFont_wght.ttf'), 'outfit.ttf'),
                )
                ->html(Stream::string('body', $html));
            $tempFolder = TemporaryDirectory::make();
            $filenameTemp = Gotenberg::save($request, $tempFolder->path(), $client);
            Storage::disk(config('filesystems.private'))
                ->putFileAs($folderPath, new File($tempFolder->path($filenameTemp)), $filename);
        } else {
            Excel::store(
                new TimeEntriesReportExport($aggregatedData, $format, $currency, $group, $subGroup, $showBillableRate),
                $path,
                config('filesystems.private'),
                $format->getExportPackageType(),
                [
                    'visibility' => 'private',
                ]
            );
        }

        return response()->json([
            'download_url' => Storage::disk(config('filesystems.private'))
                ->temporaryUrl($path, now()->addMinutes(5)),
        ]);
    }

    /**
     * @return Builder<TimeEntry>
     */
    private function getTimeEntriesAggregateQuery(Organization $organization, TimeEntryAggregateRequest|TimeEntryAggregateExportRequest|TimeEntryIndexExportRequest $request, ?Member $member): Builder
    {
        $timeEntriesQuery = TimeEntry::query()
            ->whereBelongsTo($organization, 'organization');

        $filter = new TimeEntryFilter($timeEntriesQuery);
        $filter->addEndFilter($request->input('end'));
        $filter->addStartFilter($request->input('start'));
        $filter->addActiveFilter($request->input('active'));
        $filter->addMemberIdFilter($member);
        $filter->addMemberIdsFilter($request->input('member_ids'));
        $filter->addProjectIdsFilter($request->input('project_ids'));
        $filter->addTagIdsFilter($request->input('tag_ids'), $request->getTagMatchType());
        $filter->addTaskIdsFilter($request->input('task_ids'));
        $filter->addClientIdsFilter($request->input('client_ids'));
        $filter->addBillableFilter($request->input('billable'));
        $filter->addTypeFilter($request->input('type'));

        return $filter->get();
    }

    /**
     * Create time entry
     *
     * @throws AuthorizationException
     * @throws TimeEntryStillRunningApiException
     *
     * @operationId createTimeEntry
     */
    public function store(Organization $organization, TimeEntryStoreRequest $request): JsonResource
    {
        /** @var Member $member */
        $member = Member::query()->findOrFail($request->input('member_id'));
        if ($member->getKey() === $this->member($organization)->getKey()) {
            $this->checkPermission($organization, 'time-entries:create:own');
        } else {
            $this->checkPermission($organization, 'time-entries:create:all');
        }

        if ($request->input('end') === null && TimeEntry::query()->whereBelongsTo($member, 'member')->where('end', null)->exists()) {
            throw new TimeEntryStillRunningApiException;
        }

        // Overlap check for create
        $start = Carbon::parse($request->input('start'));
        $end = $request->input('end') !== null ? Carbon::parse($request->input('end')) : null;
        $this->assertNoOverlap($organization, $member, $start, $end);

        $project = $request->input('project_id') !== null ? Project::findOrFail((string) $request->input('project_id')) : null;
        $client = $project?->client;
        $task = $request->input('task_id') !== null ? $project->tasks()->findOrFail((string) $request->input('task_id')) : null;

        $timeEntry = new TimeEntry;
        $timeEntry->fill($request->validated());
        $timeEntry->client()->associate($client);
        $timeEntry->user_id = $member->user_id;
        $timeEntry->description = $request->input('description') ?? '';
        $timeEntry->organization()->associate($organization);
        $timeEntry->setComputedAttributeValue('billable_rate');
        $timeEntry->save();

        if ($project !== null) {
            RecalculateSpentTimeForProject::dispatch($project);
        }
        if ($task !== null) {
            RecalculateSpentTimeForTask::dispatch($task);
        }

        return new TimeEntryResource($timeEntry);
    }

    /**
     * Update time entry
     *
     * @throws AuthorizationException|TimeEntryCanNotBeRestartedApiException
     *
     * @operationId updateTimeEntry
     */
    public function update(Organization $organization, TimeEntry $timeEntry, TimeEntryUpdateRequest $request): JsonResource
    {
        $member = $this->member($organization);
        /** @var Member|null $newMember */
        $newMember = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : null;
        if ($timeEntry->member_id === $member->getKey() && ($newMember === null || $newMember->getKey() === $member->getKey())) {
            $this->checkPermission($organization, 'time-entries:update:own', $timeEntry);
        } else {
            $this->checkPermission($organization, 'time-entries:update:all', $timeEntry);
        }

        if ($timeEntry->end !== null && $request->has('end') && $request->input('end') === null) {
            throw new TimeEntryCanNotBeRestartedApiException;
        }

        // Overlap check for update (exclude current)
        /** @var Member $effectiveMember */
        $effectiveMember = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : $timeEntry->member;
        $effectiveStart = $request->has('start') ? Carbon::parse($request->input('start')) : $timeEntry->start;
        $effectiveEnd = $request->has('end') ? ($request->input('end') !== null ? Carbon::parse($request->input('end')) : null) : $timeEntry->end;
        $this->assertNoOverlap($organization, $effectiveMember, $effectiveStart, $effectiveEnd, $timeEntry);

        $oldProject = $timeEntry->project;
        $oldTask = $timeEntry->task;

        $project = null;
        if ($request->has('project_id')) {
            $project = $request->input('project_id') !== null ? Project::findOrFail((string) $request->input('project_id')) : null;
            $client = $project?->client;
            $timeEntry->client()->associate($client);
        }
        $task = null;
        if ($request->has('task_id')) {
            $task = $request->input('task_id') !== null ? Task::findOrFail((string) $request->input('task_id')) : null;
        }

        $timeEntry->fill($request->validated());
        if ($newMember !== null) {
            $timeEntry->member()->associate($newMember);
            $timeEntry->user()->associate($newMember->user);
        }
        $timeEntry->description = $request->input('description', $timeEntry->description) ?? '';
        $timeEntry->setComputedAttributeValue('billable_rate');
        $timeEntry->save();

        if ($oldProject !== null) {
            RecalculateSpentTimeForProject::dispatch($oldProject);
        }
        if ($oldTask !== null) {
            RecalculateSpentTimeForTask::dispatch($oldTask);
        }
        if ($project !== null && ($oldProject === null || $project->isNot($oldProject))) {
            RecalculateSpentTimeForProject::dispatch($project);
        }
        if ($task !== null && ($oldTask === null || $task->isNot($oldTask))) {
            RecalculateSpentTimeForTask::dispatch($task);
        }

        return new TimeEntryResource($timeEntry);
    }

    /**
     * Update multiple time entries
     *
     * @operationId updateMultipleTimeEntries
     *
     * @throws AuthorizationException
     */
    public function updateMultiple(Organization $organization, TimeEntryUpdateMultipleRequest $request): JsonResponse
    {
        $member = $this->member($organization);
        $this->checkAnyPermission($organization, ['time-entries:update:all', 'time-entries:update:own']);
        $canAccessAll = $this->hasPermission($organization, 'time-entries:update:all');

        $ids = $request->validated('ids');

        $timeEntries = TimeEntry::query()
            ->whereBelongsTo($organization, 'organization')
            ->with([
                'project',
                'task',
            ])
            ->whereIn('id', $ids)
            ->get();

        $changes = $request->validated('changes');

        if ($request->has('changes.description')) {
            $changes['description'] = $request->input('changes.description') ?? '';
        }

        if (isset($changes['member_id']) && ! $canAccessAll && $this->member($organization)->getKey() !== $changes['member_id']) {
            throw new AuthorizationException;
        }

        /** @var Member|null $newMember */
        $newMember = isset($changes['member_id']) ? Member::query()->findOrFail($changes['member_id']) : null;

        $project = null;
        $client = null;
        $overwriteClient = false;
        if ($request->has('changes.project_id')) {
            $project = $request->input('changes.project_id') !== null ? Project::findOrFail((string) $request->input('changes.project_id')) : null;
            $client = $project?->client;
            $overwriteClient = true;
        }

        $task = null;
        if ($request->has('changes.task_id')) {
            $task = $request->input('changes.task_id') !== null ? Task::findOrFail((string) $request->input('changes.task_id')) : null;
        }

        $success = new Collection;
        $error = new Collection;

        foreach ($ids as $id) {
            /** @var TimeEntry|null $timeEntry */
            $timeEntry = $timeEntries->firstWhere('id', $id);
            if ($timeEntry === null) {
                // Note: ID wrong or time entry in different organization
                $error->push($id);

                continue;
            }
            if (! $canAccessAll && $timeEntry->member_id !== $member->getKey()) {
                $error->push($id);

                continue;

            }
            // Changing time entries to Break entries is only allowed when breaks are enabled in the org settings
            $resultingType = isset($changes['type']) ? TimeEntryType::from($changes['type']) : $timeEntry->type;
            if ($resultingType === TimeEntryType::Break && $timeEntry->type !== TimeEntryType::Break && ! $organization->breaks_enabled) {
                $error->push($id);

                continue;
            }
            // Break entries can not be billable, have tags or belong to a project/task (see TimeEntry::booted)
            if ($resultingType === TimeEntryType::Break && ($project !== null || $task !== null || $request->boolean('changes.billable') || count($changes['tags'] ?? []) > 0)) {
                $error->push($id);

                continue;
            }
            $oldProject = $timeEntry->project;
            $oldTask = $timeEntry->task;

            $timeEntry->fill($changes);
            if ($newMember !== null) {
                $timeEntry->member()->associate($newMember);
                $timeEntry->user_id = $newMember->user_id;
            }
            // If project is changed, but task is not, we remove the old task from the time entry
            if ($oldProject !== null && $project !== null && $oldProject->isNot($project) && $task === null) {
                $timeEntry->task()->disassociate();
            }
            if ($overwriteClient) {
                $timeEntry->client()->associate($client);
            }
            $timeEntry->setComputedAttributeValue('billable_rate');
            $timeEntry->save();
            if ($oldTask !== null) {
                RecalculateSpentTimeForTask::dispatch($oldTask);
            }
            if ($oldProject !== null) {
                RecalculateSpentTimeForProject::dispatch($oldProject);
            }
            if ($project !== null && ($oldProject === null || $project->isNot($oldProject))) {
                RecalculateSpentTimeForProject::dispatch($project);
            }
            if ($task !== null && ($oldTask === null || $task->isNot($oldTask))) {
                RecalculateSpentTimeForTask::dispatch($task);
            }

            $success->push($id);
        }

        return response()->json([
            'success' => $success->toArray(),
            'error' => $error->toArray(),
        ]);
    }

    /**
     * Delete time entry
     *
     * @throws AuthorizationException
     *
     * @operationId deleteTimeEntry
     */
    public function destroy(Organization $organization, TimeEntry $timeEntry): JsonResponse
    {
        $member = $this->member($organization);
        if ($timeEntry->member_id === $member->getKey()) {
            $this->checkPermission($organization, 'time-entries:delete:own', $timeEntry);
        } else {
            $this->checkPermission($organization, 'time-entries:delete:all', $timeEntry);
        }

        $project = $timeEntry->project;
        $task = $timeEntry->task;

        $timeEntry->delete();

        if ($project !== null) {
            RecalculateSpentTimeForProject::dispatch($project);
        }
        if ($task !== null) {
            RecalculateSpentTimeForTask::dispatch($task);
        }

        return response()
            ->json(null, 204);
    }

    /**
     * Delete multiple time entries
     *
     * @throws AuthorizationException
     *
     * @operationId deleteTimeEntries
     */
    public function destroyMultiple(Organization $organization, TimeEntryDestroyMultipleRequest $request): JsonResponse
    {
        $this->checkAnyPermission($organization, ['time-entries:delete:all', 'time-entries:delete:own']);
        $canDeleteAll = $this->hasPermission($organization, 'time-entries:delete:all');

        $ids = $request->validated('ids');
        $timeEntries = TimeEntry::query()
            ->whereBelongsTo($organization, 'organization')
            ->with([
                'project',
                'task',
            ])
            ->whereIn('id', $ids)
            ->get();

        $success = new Collection;
        $error = new Collection;

        foreach ($ids as $id) {
            /** @var TimeEntry|null $timeEntry */
            $timeEntry = $timeEntries->firstWhere('id', $id);
            if ($timeEntry === null) {
                // Note: ID wrong or time entry in different organization
                $error->push($id);

                continue;
            }

            if (! $canDeleteAll && $timeEntry->member_id !== $this->member($organization)->getKey()) {
                $error->push($id);

                continue;

            }

            $project = $timeEntry->project;
            $task = $timeEntry->task;

            $timeEntry->delete();

            if ($project !== null) {
                RecalculateSpentTimeForProject::dispatch($project);
            }
            if ($task !== null) {
                RecalculateSpentTimeForTask::dispatch($task);
            }
            $success->push($id);
        }

        return response()->json([
            'success' => $success->toArray(),
            'error' => $error->toArray(),
        ]);
    }
}
