var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { injectable, inject } from 'tsyringe';
import { randomUUID, createHash } from 'node:crypto';
let RegisterUserUseCase = class RegisterUserUseCase {
    userRepo;
    constructor(userRepo) {
        this.userRepo = userRepo;
    }
    async execute(input) {
        const trimmedEmail = input.email.trim().toLowerCase();
        if (!trimmedEmail?.includes('@')) {
            return { ok: false, error: 'A valid email address is required.' };
        }
        const trimmedName = input.displayName.trim();
        if (!trimmedName) {
            return { ok: false, error: 'Display name is required.' };
        }
        if (input.password.length < 8) {
            return { ok: false, error: 'Password must be at least 8 characters.' };
        }
        const existing = await this.userRepo.findByEmail(trimmedEmail);
        if (existing) {
            return { ok: false, error: 'An account with this email already exists.' };
        }
        const passwordHash = hashPassword(input.password);
        const now = new Date();
        const user = {
            id: randomUUID(),
            email: trimmedEmail,
            passwordHash,
            displayName: trimmedName,
            isSystemUser: false,
            createdAt: now,
            updatedAt: now,
        };
        await this.userRepo.create(user);
        const { passwordHash: _hash, ...safeUser } = user;
        return { ok: true, user: safeUser };
    }
};
RegisterUserUseCase = __decorate([
    injectable(),
    __param(0, inject('IPmUserRepository')),
    __metadata("design:paramtypes", [Object])
], RegisterUserUseCase);
export { RegisterUserUseCase };
/**
 * Simple password hashing using SHA-256 with a salt.
 * In production, bcrypt/scrypt/argon2 would be preferred.
 * For a local single-developer tool this is sufficient.
 */
function hashPassword(password) {
    const salt = randomUUID();
    const hash = createHash('sha256')
        .update(salt + password)
        .digest('hex');
    return `${salt}:${hash}`;
}
/**
 * Verify a password against a stored hash.
 * Exported for use by the login use case.
 */
export function verifyPassword(password, storedHash) {
    const [salt, hash] = storedHash.split(':');
    if (!salt || !hash)
        return false;
    const computed = createHash('sha256')
        .update(salt + password)
        .digest('hex');
    return computed === hash;
}
