<?php

namespace App\Actions\Fortify;

use App\Concerns\PasswordValidationRules;
use App\Concerns\ProfileValidationRules;
use App\Models\TeamInvitation;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules, ProfileValidationRules;

    /**
     * Validate and create a newly registered user.
     *
     * @param  array<string, string>  $input
     */
    public function create(array $input): User
    {
        Validator::make($input, [
            ...$this->profileRules(),
            'password' => $this->passwordRules(),
        ])->validate();

        $user = User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => $input['password'],
        ]);

        if (! empty($input['invitation'])) {
            $invitation = TeamInvitation::where('token', $input['invitation'])->first();

            if ($invitation && $invitation->isValid() && strtolower($user->email) === strtolower($invitation->email)) {
                DB::transaction(function () use ($user, $invitation) {
                    $user->workspaces()->attach($invitation->workspace_id, [
                        'role' => $invitation->role,
                        'permissions' => json_encode(config("permissions.defaults.{$invitation->role}", [])),
                    ]);

                    $invitation->update(['accepted_at' => now()]);
                });

                session(['current_workspace_id' => $invitation->workspace_id]);
                session(['invitation_accepted' => true]);
            }
        }

        return $user;
    }
}
