package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.RecruitPostData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RecruitPostsMapper {

    @Select("""
            SELECT
                r.id,
                r.user_id AS userId,
                u.display_name AS creator,
                r.project_name AS projectName,
                r.genre,
                r.summary,
                r.role,
                r.project_status AS projectStatus,
                r.participation_type AS participationType,
                r.expected_period AS expectedPeriod,
                r.team_members AS teamMembers,
                r.contact,
                r.description,
                r.reference_url AS referenceUrl,
                r.deadline_at AS deadlineAt,
                r.is_visible AS visible,
                r.sort_order AS sortOrder,
                r.created_at AS createdAt,
                r.updated_at AS updatedAt
            FROM recruit_posts r
            JOIN users u ON u.id = r.user_id
            WHERE r.id = #{id}
              AND r.is_delete IS NOT TRUE
              AND u.is_delete IS NOT TRUE
            """)
    RecruitPostData getRecruitPost(@Param("id") long id);

    @Select("""
            SELECT
                r.id,
                r.user_id AS userId,
                u.display_name AS creator,
                r.project_name AS projectName,
                r.genre,
                r.summary,
                r.role,
                r.project_status AS projectStatus,
                r.participation_type AS participationType,
                r.expected_period AS expectedPeriod,
                r.team_members AS teamMembers,
                r.contact,
                r.description,
                r.reference_url AS referenceUrl,
                r.deadline_at AS deadlineAt,
                r.is_visible AS visible,
                r.sort_order AS sortOrder,
                r.created_at AS createdAt,
                r.updated_at AS updatedAt
            FROM recruit_posts r
            JOIN users u ON u.id = r.user_id
            WHERE r.is_visible IS NOT FALSE
              AND r.is_delete IS NOT TRUE
              AND u.is_delete IS NOT TRUE
            ORDER BY r.sort_order ASC, r.created_at DESC, r.id DESC
            """)
    List<RecruitPostData> getVisibleRecruitPosts();

    @Insert("""
            INSERT INTO recruit_posts (
                user_id,
                project_name,
                genre,
                summary,
                role,
                project_status,
                participation_type,
                expected_period,
                team_members,
                contact,
                description,
                reference_url,
                is_visible,
                sort_order
            ) VALUES (
                #{userId},
                #{projectName},
                #{genre},
                #{summary},
                #{role},
                #{projectStatus},
                #{participationType},
                #{expectedPeriod},
                #{teamMembers},
                #{contact},
                #{description},
                #{referenceUrl},
                #{visible},
                #{sortOrder}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addRecruitPost(RecruitPostData post);

    @Select("""
            SELECT COALESCE(MAX(sort_order), 0) + 1
            FROM recruit_posts
            WHERE is_delete IS NOT TRUE
            """)
    int nextSortOrder();
}
