-- 프로젝트별 일일공수 테이블 생성
CREATE TABLE project_daily_works (
    id SERIAL PRIMARY KEY,
    date DATE NOT NULL,
    project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
    hours FLOAT NOT NULL,
    created_by_id INTEGER NOT NULL REFERENCES users(id),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- 인덱스 생성
CREATE INDEX idx_project_daily_works_date ON project_daily_works(date);
CREATE INDEX idx_project_daily_works_project_id ON project_daily_works(project_id);
CREATE INDEX idx_project_daily_works_date_project ON project_daily_works(date, project_id);

-- 기존 일일공수 데이터를 프로젝트별로 마이그레이션 (M Project로)
INSERT INTO project_daily_works (date, project_id, hours, created_by_id, created_at)
SELECT 
    date::date,
    1, -- M Project ID
    total_hours,
    created_by_id,
    created_at
FROM daily_works
WHERE total_hours > 0;
