-- Kurva depresiasi khusus flock 5 (house_types=open_house, effective_date=2026-06-03).
-- Override hanya multiplication_percentage; house_type & standard_week diwarisi dari baris global.
-- depreciation_percent diturunkan = (1 - multiplication_percentage) * 100.
-- Lookup engine: ? = ANY(project_flock_ids) — satu baris dipakai semua flock.

-- Hapus custom curve untuk array ini agar INSERT idempoten.
DELETE FROM house_depreciation_standards
WHERE project_flock_ids = ARRAY[5]::bigint[]
  AND house_type = 'open_house'::house_type_enum
  AND effective_date = DATE '2026-06-03';

-- house_type: open_house
INSERT INTO house_depreciation_standards
    (project_flock_ids, house_type, day, effective_date,
     multiplication_percentage, depreciation_percent, standard_week, name)
SELECT
    ARRAY[5]::bigint[], g.house_type, g.day, DATE '2026-06-03',
    v.mult, (1 - v.mult) * 100, g.standard_week,
    'Custom flocks 5 (eff 2026-06-03)'
FROM (VALUES
    (1, 0.9976::numeric), (2, 0.9972), (3, 0.9972), (4, 0.9972), (5, 0.9972),
    (6, 0.9971), (7, 0.9971), (8, 0.9971), (9, 0.9975), (10, 0.9975),
    (11, 0.9975), (12, 0.9975), (13, 0.9975), (14, 0.9975), (15, 0.9975),
    (16, 0.9971), (17, 0.9971), (18, 0.9971), (19, 0.997), (20, 0.997),
    (21, 0.997), (22, 0.997), (23, 0.997), (24, 0.997), (25, 0.997),
    (26, 0.997), (27, 0.997), (28, 0.997), (29, 0.997), (30, 0.9974),
    (31, 0.9974), (32, 0.9974), (33, 0.9974), (34, 0.9974), (35, 0.9973),
    (36, 0.9973), (37, 0.9969), (38, 0.9969), (39, 0.9969), (40, 0.9969),
    (41, 0.9968), (42, 0.9968), (43, 0.9968), (44, 0.9973), (45, 0.9973),
    (46, 0.9973), (47, 0.9972), (48, 0.9972), (49, 0.9972), (50, 0.9972),
    (51, 0.9968), (52, 0.9967), (53, 0.9967), (54, 0.9967), (55, 0.9967),
    (56, 0.9967), (57, 0.9967), (58, 0.9972), (59, 0.9971), (60, 0.9971),
    (61, 0.9971), (62, 0.9971), (63, 0.9971), (64, 0.9971), (65, 0.9966),
    (66, 0.9966), (67, 0.9966), (68, 0.9966), (69, 0.9966), (70, 0.9966),
    (71, 0.9965), (72, 0.997), (73, 0.997), (74, 0.997), (75, 0.997),
    (76, 0.997), (77, 0.997), (78, 0.997), (79, 0.9965), (80, 0.9964),
    (81, 0.9964), (82, 0.9964), (83, 0.9964), (84, 0.9964), (85, 0.9964),
    (86, 0.9969), (87, 0.9969), (88, 0.9969), (89, 0.9969), (90, 0.9968),
    (91, 0.9968), (92, 0.9968), (93, 0.9968), (94, 0.9968), (95, 0.9968),
    (96, 0.9968), (97, 0.9968), (98, 0.9968), (99, 0.9968), (100, 0.9962),
    (101, 0.9962), (102, 0.9962), (103, 0.9962), (104, 0.9961), (105, 0.9961),
    (106, 0.9961), (107, 0.9967), (108, 0.9966), (109, 0.9966), (110, 0.9966),
    (111, 0.9966), (112, 0.9966), (113, 0.9966), (114, 0.9966), (115, 0.9966),
    (116, 0.9965), (117, 0.9965), (118, 0.9965), (119, 0.9965), (120, 0.9965),
    (121, 0.9959), (122, 0.9959), (123, 0.9959), (124, 0.9959), (125, 0.9958),
    (126, 0.9958), (127, 0.9958), (128, 0.9964), (129, 0.9964), (130, 0.9964),
    (131, 0.9963), (132, 0.9963), (133, 0.9963), (134, 0.9963), (135, 0.9963),
    (136, 0.9963), (137, 0.9963), (138, 0.9962), (139, 0.9962), (140, 0.9962),
    (141, 0.9962), (142, 0.9962), (143, 0.9962), (144, 0.9962), (145, 0.9961),
    (146, 0.9961), (147, 0.9961), (148, 0.9961), (149, 0.9961), (150, 0.9961),
    (151, 0.9961), (152, 0.996), (153, 0.996), (154, 0.996), (155, 0.996),
    (156, 0.9953), (157, 0.9953), (158, 0.9953), (159, 0.9952), (160, 0.9952),
    (161, 0.9952), (162, 0.9952), (163, 0.9958), (164, 0.9958), (165, 0.9958),
    (166, 0.9958), (167, 0.9958), (168, 0.9958), (169, 0.9957), (170, 0.9957),
    (171, 0.9957), (172, 0.9957), (173, 0.9957), (174, 0.9956), (175, 0.9956),
    (176, 0.9956), (177, 0.9956), (178, 0.9956), (179, 0.9955), (180, 0.9955),
    (181, 0.9955), (182, 0.9955), (183, 0.9955), (184, 0.9954), (185, 0.9954),
    (186, 0.9954), (187, 0.9954), (188, 0.9954), (189, 0.9953), (190, 0.9953),
    (191, 0.9953), (192, 0.9953), (193, 0.9952), (194, 0.9952), (195, 0.9952),
    (196, 0.9952), (197, 0.9952), (198, 0.9951), (199, 0.9951), (200, 0.9951),
    (201, 0.9951), (202, 0.995), (203, 0.995), (204, 0.995), (205, 0.995),
    (206, 0.9949), (207, 0.9949), (208, 0.9949), (209, 0.9949), (210, 0.9948),
    (211, 0.9948), (212, 0.9948), (213, 0.9947), (214, 0.9947), (215, 0.9947),
    (216, 0.9947), (217, 0.9946), (218, 0.9946), (219, 0.9955), (220, 0.9955),
    (221, 0.9954), (222, 0.9954), (223, 0.9954), (224, 0.9954), (225, 0.9954),
    (226, 0.9944), (227, 0.9944), (228, 0.9943), (229, 0.9943), (230, 0.9943),
    (231, 0.9942), (232, 0.9942), (233, 0.9942), (234, 0.9941), (235, 0.9941),
    (236, 0.9941), (237, 0.994), (238, 0.994), (239, 0.994), (240, 0.9939),
    (241, 0.9939), (242, 0.9938), (243, 0.9938), (244, 0.9938), (245, 0.9937),
    (246, 0.9937), (247, 0.9937), (248, 0.9936), (249, 0.9936), (250, 0.9935),
    (251, 0.9935), (252, 0.9934), (253, 0.9934), (254, 0.9945), (255, 0.9944),
    (256, 0.9944), (257, 0.9944), (258, 0.9943), (259, 0.9943), (260, 0.9943),
    (261, 0.9931), (262, 0.993), (263, 0.993), (264, 0.9929), (265, 0.9929),
    (266, 0.9928), (267, 0.9928), (268, 0.9939), (269, 0.9939), (270, 0.9939),
    (271, 0.9938), (272, 0.9938), (273, 0.9938), (274, 0.9937), (275, 0.9924),
    (276, 0.9924), (277, 0.9923), (278, 0.9922), (279, 0.9922), (280, 0.9921),
    (281, 0.9921), (282, 0.992), (283, 0.9919), (284, 0.9919), (285, 0.9918),
    (286, 0.9917), (287, 0.9917), (288, 0.9916), (289, 0.9929), (290, 0.9929),
    (291, 0.9928), (292, 0.9928), (293, 0.9927), (294, 0.9927), (295, 0.9926),
    (296, 0.9911), (297, 0.991), (298, 0.9909), (299, 0.9908), (300, 0.9907),
    (301, 0.9907), (302, 0.9906), (303, 0.9921), (304, 0.992), (305, 0.9919),
    (306, 0.9919), (307, 0.9918), (308, 0.9917), (309, 0.9917), (310, 0.9916),
    (311, 0.9915), (312, 0.9915), (313, 0.9914), (314, 0.9913), (315, 0.9912),
    (316, 0.9912), (317, 0.9893), (318, 0.9892), (319, 0.9891), (320, 0.9889),
    (321, 0.9888), (322, 0.9887), (323, 0.9885), (324, 0.9903), (325, 0.9903),
    (326, 0.9902), (327, 0.9901), (328, 0.99), (329, 0.9899), (330, 0.9898),
    (331, 0.9896), (332, 0.9895), (333, 0.9894), (334, 0.9893), (335, 0.9892),
    (336, 0.9891), (337, 0.989), (338, 0.9888), (339, 0.9887), (340, 0.9886),
    (341, 0.9885), (342, 0.9883), (343, 0.9882), (344, 0.988), (345, 0.9855),
    (346, 0.9853), (347, 0.985), (348, 0.9848), (349, 0.9846), (350, 0.9843),
    (351, 0.9841), (352, 0.9865), (353, 0.9863), (354, 0.9861), (355, 0.986),
    (356, 0.9858), (357, 0.9855), (358, 0.9853), (359, 0.9851), (360, 0.9849),
    (361, 0.9847), (362, 0.9844), (363, 0.9842), (364, 0.9839), (365, 0.9837),
    (366, 0.9834), (367, 0.9831), (368, 0.9828), (369, 0.9825), (370, 0.9822),
    (371, 0.9819), (372, 0.9815), (373, 0.9812), (374, 0.9808), (375, 0.9805),
    (376, 0.9801), (377, 0.9797), (378, 0.9793), (379, 0.9788), (380, 0.9784),
    (381, 0.9779), (382, 0.9774), (383, 0.9769), (384, 0.9763), (385, 0.9757),
    (386, 0.9751), (387, 0.9745), (388, 0.9738), (389, 0.9731), (390, 0.9724),
    (391, 0.9716), (392, 0.9708), (393, 0.9699), (394, 0.9752), (395, 0.9745),
    (396, 0.9739), (397, 0.9732), (398, 0.9724), (399, 0.9716), (400, 0.9708),
    (401, 0.9624), (402, 0.9609), (403, 0.9593), (404, 0.9576), (405, 0.9558),
    (406, 0.9537), (407, 0.9515), (408, 0.949), (409, 0.9462), (410, 0.9432),
    (411, 0.9398), (412, 0.9359), (413, 0.9315), (414, 0.9265), (415, 0.9206),
    (416, 0.9138), (417, 0.9057), (418, 0.8958), (419, 0.8837), (420, 0.8684),
    (421, 0.8485), (422, 0.8571), (423, 0.8333), (424, 0.8), (425, 0.75),
    (426, 0.6667), (427, 0.5), (428, 0)
) AS v(day, mult)
JOIN LATERAL (
    SELECT DISTINCT ON (day) house_type, day, standard_week
    FROM house_depreciation_standards
    WHERE project_flock_ids IS NULL
      AND house_type = 'open_house'::house_type_enum
      AND day = v.day
    ORDER BY day, effective_date DESC NULLS LAST
) g ON TRUE;

-- Recompute snapshot depresiasi untuk semua flock yang dipetakan.
DELETE FROM farm_depreciation_snapshots WHERE project_flock_id IN (5);
