const std = @import("std");
const Tensor = @import("tensor.zig").Tensor;

pub const Neuron = struct {
    weights: Tensor,
    bias: f32,

    pub fn init(allocator: std.mem.Allocator, input_size: usize) !Neuron {
        const weights = try Tensor.init(allocator, &[_]usize{input_size});

        var seed: u64 = undefined;
        try std.posix.getrandom(std.mem.asBytes(&seed));

        var prng = std.Random.DefaultPrng.init(seed);
        const rand = prng.random();

        for (weights.data) |*w| {
            w.* = rand.float(f32) * 2.0 - 1.0;
        }

        return Neuron{
            .weights = weights,
            .bias = 0.0,
        };
    }

    pub fn deinit(self: *Neuron) void {
        self.weights.deinit();
    }

    pub fn forward(self: *Neuron, inputs: Tensor) f32 {
        std.debug.assert(inputs.data.len == self.weights.data.len);

        var sum: f32 = 0.0;
        for (inputs.data, self.weights.data) |x, w| {
            sum += x * w;
        }

        const z = sum + self.bias;

        return if (z > 0) z else 0;
    }
};
