package com.nerv.server.alarm.zone;

import com.nerv.server.database.gen.jooq.tables.daos.AlarmZoneDao;
import com.nerv.server.database.gen.jooq.tables.pojos.AlarmZone;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.jooq.DSLContext;
import org.jooq.Record2;
import org.jooq.impl.DefaultConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import static com.nerv.server.database.gen.jooq.Tables.ALARM_ZONE;

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Repository
public class AlarmZoneRepository {

    private final @NonNull DefaultConfiguration config;
    private final @NonNull DSLContext ctx;

    public AlarmZone getById(Integer panelId, Integer zoneId) {
        Record2 idRecord = ctx.newRecord(ALARM_ZONE.ID, ALARM_ZONE.ID_PANEL)
            .values(zoneId, panelId);

        AlarmZoneDao dao = new AlarmZoneDao(config);
        return dao.findById(idRecord);
    }

    public void insert(AlarmZone alarmZone) {
        new AlarmZoneDao(config).insert(alarmZone);
    }

    public void update(AlarmZone alarmZone) {
        new AlarmZoneDao(config).update(alarmZone);
    }
}
