package com.nursinghome.mapper;

import com.nursinghome.entity.Schedule;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ScheduleMapper {
    @Select("SELECT * FROM schedule WHERE nurse_id = #{nurseId} AND date = #{date} ORDER BY id DESC")
    List<Schedule> listByNurseAndDate(@Param("nurseId") Long nurseId, @Param("date") LocalDate date);

    @Select("SELECT * FROM schedule WHERE date = #{date} ORDER BY id DESC")
    List<Schedule> listByDate(LocalDate date);

    @Select("SELECT * FROM schedule WHERE date BETWEEN #{start} AND #{end} ORDER BY date ASC")
    List<Schedule> listByDateRange(@Param("start") LocalDate start, @Param("end") LocalDate end);

    @Select("SELECT * FROM schedule WHERE nurse_id = #{nurseId} AND date BETWEEN #{start} AND #{end} ORDER BY date ASC")
    List<Schedule> listByNurseAndDateRange(@Param("nurseId") Long nurseId, @Param("start") LocalDate start, @Param("end") LocalDate end);

    @Insert("INSERT INTO schedule(nurse_id, date, shift, task, created_at, updated_at) VALUES(#{nurseId}, #{date}, #{shift}, #{task}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Schedule schedule);

    @Update("UPDATE schedule SET nurse_id=#{nurseId}, date=#{date}, shift=#{shift}, task=#{task}, updated_at=NOW() WHERE id=#{id}")
    int update(Schedule schedule);

    @Delete("DELETE FROM schedule WHERE id=#{id}")
    int delete(Long id);
}
